Original Post
Is anyone able to explain how you separate two bounding rectangles without going into some super complex axis thereom that just goes way over my head? Here's what I have now, and it works if you're heading straight (not diagonal, just straight in any direction): It doesn't work for diagonals, and I understand why, it's just testing for an overlap but not an overlap with any sort of directional meaning. It works for going straight though. It'll move you away from the edge depending on what way you're going. I can fix it by moving by X, testing for overlap, moving X, then moving by Y, testing for overlap, moving Y but that doesn't seem very efficient. So is there some "easy" separating axis theoreom just for rectangles? cuz for this overhead shooter, thats all I care about. Thank you very much.
// I already moved the objects by their velocities * deltaTime.
foreach (CollisionRectangle body in _bodies)
{
foreach (CollisionRectangle other in _bodies)
{
// We don't collide with eachother.
if (body == other)
continue;
if (body.Intersects(other))
{
// Draw them as red.
if (body is RectangularCollisionRectangle)
g.DrawRectangle(new Pen(Color.Red), new System.Drawing.Rectangle((int)body.Position.X, (int)body.Position.Y, (int)body.Width, (int)body.Height));
Vector2 bodyPosition = body.Position;
Vector2 bodyVelocity = body.Velocity;
Vector2 otherPosition = other.Position;
Vector2 otherVelocity = other.Velocity;
if (body.Velocity.X < 0)
{
bodyPosition.X = other.Position.X + other.Width;
bodyVelocity.X = 0;
}
else if (body.Velocity.X > 0)
{
bodyPosition.X = other.Position.X - body.Width;
bodyVelocity.X = 0;
}
if (body.Velocity.Y < 0)
{
bodyPosition.Y = other.Position.Y + other.Height;
bodyVelocity.Y = 0;
}
else if (body.Velocity.Y > 0)
{
bodyPosition.Y = other.Position.Y - body.Height;
bodyVelocity.Y = 0;
}
body.Position = bodyPosition;
body.Velocity = body.Velocity;
}
}
}