For about a week I've been trying to grasp the basics of collision, but I've been driven to the point of wanting to just give up on everything when it comes to collision. I just don't understand it. Here's my code so far:
public static void ObjectToObjectResponseTopDown(GameObject actor1, GameObject actor2)
{
if (CollisionDetection2D.BoundingRectangle(actor1.Bounds.X, actor1.Bounds.Y, actor1.Bounds.Width, actor1.Bounds.Height,
actor2.Bounds.X, actor2.Bounds.Y, actor2.Bounds.Width, actor2.Bounds.Height)) //Just a Bounding Rectangle Collision Checker
{
if (actor1.Bounds.Top > actor2.Bounds.Bottom) //Hit From Top
{
actor1.y_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
return;
}
if (actor1.Bounds.Bottom > actor2.Bounds.Top) //Hit From Bottom
{
actor1.y_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Height;
return;
}
if (actor1.Bounds.Left > actor2.Bounds.Right)
{
actor1.x_position += Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
return;
}
if (actor1.Bounds.Right > actor2.Bounds.Left)
{
actor1.x_position -= Rectangle.Intersect(actor1.Bounds, actor2.Bounds).Width;
return;
}
}
}
Essentially what it does so far is correctly collides when the bottom of the first rectangle collides with the top of the second rectangle, but with the left an right sides it corrects it either above or below the tile, and when the top of the first rectangle collides with the bottom of the second rectangle, it slides right through the second rectangle.
I'm really not sure what to do at this point.