Original Post
I've been playing around with collision detection and I'm having nothing but problems. This is the code I'm using.. where x,y is the upper left corner and x2,y2 is the bottom right corner of the objects. A is object one, B is object 2. What I've noticed from the debugger is that the only time the main if statement evaluates to false is when there's a collision between the top right of objectA and the bottom left of objectB, which for some reason makes the first if statement in the else block go to the first return true. Can someone go through this quickly or point me to a working algorithm?
bool HVSTEVENT::collision(float xA1, float yA1, float xA2, float yA2, float xB1, float yB1, float xB2, float yB2)
{
if ( ((xA2 < xB1) || (xB1 > xA2) || (yA1 < yB2) || (yA2 > yB1)) )
return false; //no possible collisions
else
{
//all of the above are FALSE, there may be a collision.
//upper left corner collision
if ((xA2 > xB1) && (xA2 < xB2))
if ((yA2 > yB2) && (yA2 < yB1))
return true;
//bottom left corner collision
if ((xA2 > xB1) && (xA2 < xB2))
if (yA1 > yB2 && yA1 < yB1)
return true;
//upper right corner collision
if ((xA1 > xB1) && (xA1 < xB2))
if ((yA2 > yB2) && (yA2 < yB1))
return true;
//bottom right corner collision
if ((xA1 > xB1) && (xA1 < xB2))
if ((yA1 > yB2) && (yA1 < yB1))
return true;
//overlapping
if ((xA1 == xB1) && (xA2 == xB2) && (yA1 == yB1) && (yA2 == yB2))
return true;
//no collision
return false;
}
}