Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Strange Collision  (Read 4050 times)

0 Members and 1 Guest are viewing this topic.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Strange Collision
« on: November 23, 2008, 06:01:00 pm »
Hi everyone,

first at all - I really like the SFML, but sometimes I got problems that gives me a headache  :(

I want to test the collosion between two sprites. Actually no problem with functions like for example Player.GetPosition().x or Player.GetSize().x.

But unfortunately it works rather complicated...

this is my code
Code: [Select]
if(Window.GetInput().IsKeyDown(sf::Key::Right))
if(Player.GetPosition().x + Player.GetSize().x <= Wall.GetPosition().x)
{
//not the important part...
if(Player.GetPosition().x <= 1024-Player.GetSize().x) Player.Move( 400*Window.GetFrameTime(), 0);
}



The red one should be the player, and the wall is the fence
There everything works perfect... I´m not able to move the "player" to the right side.


But there I´m also not able to move the player to the right side? Why?
thanks in advance
Realism is what makes games suck. - Wouter van Oortmerssen

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Strange Collision
« Reply #1 on: November 23, 2008, 06:33:59 pm »
Code: [Select]
if(Player.GetPosition().x + Player.GetSize().x <= Wall.GetPosition().x)
      {
//not the important part...
         if(Player.GetPosition().x <= 1024-Player.GetSize().x)   Player.Move( 400*Window.GetFrameTime(), 0);
      }


Here you are checking X position for a possible collision...and the Y ?

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Strange Collision
« Reply #2 on: November 23, 2008, 06:59:01 pm »
Quote from: "wizardofoz"
Here you are checking X position for a possible collision...and the Y ?


I´ll check the y position when the player moves up or down... but I had the same thought and tried a combination with GetPositon().y / ..., but I got the same result as you can see on the second screenshot, just this time with the y-axis (and of course the x-axis).
Realism is what makes games suck. - Wouter van Oortmerssen

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Strange Collision
« Reply #3 on: November 23, 2008, 10:45:18 pm »
Are you checking both X and Y values?

Try something like this:

Code: [Select]
// Object-to-object bounding-box collision detector:
short int Sprite_Collide(sprite_ptr object1, sprite_ptr object2) {
 
    int left1, left2;
    int right1, right2;
    int top1, top2;
    int bottom1, bottom2;

    left1 = object1->x;
    left2 = object2->x;
    right1 = object1->x + object1->width;
    right2 = object2->x + object2->width;
    top1 = object1->y;
    top2 = object2->y;
    bottom1 = object1->y + object1->height;
    bottom2 = object2->y + object2->height;

    if (bottom1 < top2) return(0);
    if (top1 > bottom2) return(0);

    if (right1 < left2) return(0);
    if (left1 > right2) return(0);

    return(1);

};


(Pasted from GameDev.net)

Or read the full article: http://www.gamedev.net/reference/articles/article735.asp

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Strange Collision
« Reply #4 on: November 24, 2008, 07:14:06 am »
Your code is only checking to see if the X axis from the vector of the bottom right edge of the player is colliding by more than one pixel with the top left vector of the wall. It is also doing a redundant check that will always be true. It is expected that this code will make objects collide with the wall when past the Y axis that contains the pixels of the left side of the wall, as illustrated by your images.

Here I wrote a function to detect collisions of sprites for both axises in both directions. Note that I did not use >= or <= operators, but the > and < operators (will fix the "by more than one pixel" problem, sprites should collide if any of their pixels intersect).

 

anything