Collision Resolution Help
Posted: Thu Jan 05, 2012 12:35 pm
I'm trying to implement the method of collision resolution described here and I can't get it to work.
Here's the code:
Here's the code:
Code: Select all
bool IsCollision(Rectangle rectangleA, Rectangle rectangleB)
{
if (rectangleA.x < rectangleB.x + rectangleB.w &&
rectangleA.x + rectangleA.w > rectangleB.x &&
rectangleA.y < rectangleB.y + rectangleB.h &&
rectangleA.y + rectangleA.h > rectangleB.y)
{
return true;
}
return false;
}
...
if (IsCollision(rectangleA, rectangleB))
{
Rectangle overlap;
overlap.h = rectangleB.y + rectangleA.h- rectangleB.x;
overlap.w = rectangleA.x + rectangleA.w - rectangleA.y;
Rectangle centerPointA;
centerPointA.x = rectangleA.x + rectangleA.w / 2;
centerPointA.y = rectangleA.y + rectangleA.h / 2;
Rectangle centerPointB;
centerPointB.x = rectangleB.x + rectangleB.w / 2;
centerPointB.y = rectangleB.y + rectangleB.h / 2;
if (overlap.w < overlap.h)
{
if (centerPointA.x < centerPointB.x)
{
rectangleA.x -= overlap.w;
}
else
{
rectangleA.x += overlap.w;
}
}
else
{
if (centerPointA.y > centerPointB.y)
{
rectangleA.y -= overlap.h;
}
else
{
rectangleA.y += overlap.h;
}
}
}