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

Author Topic: Collision test - problem  (Read 1373 times)

0 Members and 1 Guest are viewing this topic.

kamac

  • Newbie
  • *
  • Posts: 19
    • View Profile
Collision test - problem
« on: October 07, 2011, 08:24:28 pm »
Hi.

I am using collision function, which is working OK, but when i try to use it with over 1 sprite, it isn't working at all.

I am checking for collision using FOR loop:

Code: [Select]
for(int n = 0; n < 2; n++)
{
if(!box_collision(person,tilesprite[n],32,32,32,32)) {                 person.SetPosition(person.GetPosition().x,person.GetPosition().y+0.002);
}
}


It isn't working, as i try to check for 2 sprites which are tilesprite[0] and tilesprite[1]...

But when i try to check only one sprite:

Code: [Select]
for(int n = 0; n < 1; n++)
{
if(!box_collision(person,tilesprite[n],32,32,32,32)) {                 person.SetPosition(person.GetPosition().x,person.GetPosition().y+0.002);
}
}


So, only tilesprite[0], it is working...


I have this collision function:

Code: [Select]
int box_collision(sf::Sprite &first,sf::Sprite &second,int first_width,int first_height,
int second_width,int second_height)
{
if( first.GetPosition().x > second.GetPosition().x + second_width ||
first.GetPosition().y > second.GetPosition().y + second_height ||
first.GetPosition().x + first_width < second.GetPosition().x ||
first.GetPosition().y + first_height < second.GetPosition().y )
{
return 0;
}

return 1;
}


What could be possibly wrong  :( ?

kamac

  • Newbie
  • *
  • Posts: 19
    • View Profile
Collision test - problem
« Reply #1 on: October 07, 2011, 08:33:23 pm »
God. I'm so dumb.

It was checking for collision with anything. If there was no collision with THIS ONE object, then it was moving the character down!

Actually, you can collide with 1 object for now, so:

-Checking for collision with sprite 1... No collision, move me down by 0.02.
-Checking for collision with sprite 2... No collision, move me down by 0.02.
-Checking for collision with sprite 3... COLLISION!, don't move me.
-Checking for collision with sprite 4... No collision, move me down by 0.02.
-Checking for collision with sprite 5... No collision, move me down by 0.02.

That's how it was.