SFML community forums

Help => General => Topic started by: FerrisWheel on July 28, 2010, 11:32:55 pm

Title: Trouble with the collision code from the wiki
Post by: FerrisWheel on July 28, 2010, 11:32:55 pm
I'm using the code somebody has kindly written for us on the wiki (link (http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection)), 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?
Title: Trouble with the collision code from the wiki
Post by: Dig 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.