1
General / 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.
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?
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?