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

Author Topic: Trouble with the collision code from the wiki  (Read 1803 times)

0 Members and 1 Guest are viewing this topic.

FerrisWheel

  • Newbie
  • *
  • Posts: 1
    • View Profile
Trouble with the collision code from the wiki
« on: July 28, 2010, 11:32:55 pm »
I'm using the code somebody has kindly written for us on the wiki (link), and am having trouble with the collisions. I'm trying to get the collision between two squares, yet using both BoundingBox and PixelPerfect collision tests, the rectangle always seems move a few pixels into the box.

Code: [Select]
if(Collision::PixelPerfectTest(sprite, collidables[i]))
{
      sprite.SetPosition(sprite.GetPosition().x + 100 * time_step, sprite.GetPosition().y); //move back to original position
      while(Collision::PixelPerfectTest(sprite, collidables[collided_with]))
      {
           sprite.SetPosition(sprite.GetPosition().x - 1, sprite.GetPosition().y);
      }
}


Before this code I move the player by -100, then test collision, if there is a collision I move it +100 to it's original position and loop through moving one pixel at a time, testing collision at each. Yet as it stops they just intersect slightly. Why does this happen?

Dig

  • Newbie
  • *
  • Posts: 31
    • View Profile
Trouble with the collision code from the wiki
« Reply #1 on: July 31, 2010, 04:25:30 am »
Hard to tell without seeing what you mean by 'slightly'.  Both collision tests return true when the box or pixels actually overlap.  So if you mean intersect 'slightly' by 1 pixel then that is the collision you are looking for.

Also in my code the pixel perfect test takes three parameters, the thrid being the alpha limit to register a hit, if your sprites have antialiasing its worth tweaking this to get the result you are looking for.

Hope that helps, post some more code or some screenshots to help illustrate the problem further if you need.