I have a 2D game written in XNA, and I've been trying to fine-tune my collision resolution. All of my game objects are squares, which means detecting a collision is easy - use the position and the size of each entity (which is already known) to craft a corresponding Rectangle
, then compare the two with Rectangle.Intersect()
.
This works fine for the most part, but has been running into weird corner cases where the collision resolution doubles an entity's jump height, or gets caught between tiles when moving horizontally across a flat surface.
I have recently expanded my code to include a velocity for each game entity, and I thought it would be possible to avoid some of these problems by preventing the need for these resolutions in the first place.
The idea would be to use an entity's position + velocity to predict a collision, then reduce the velocity to prevent a collision from occurring in the first place.
I can still use Rectangle.Intersect()
to determine if the entity is in a collision, but I'm horribly confused on how to go from the Rectangle
returned by Rectangle.Intersect()
to an appropriate velocity counter-vector to avoid the collision.
Is there a straightforward way to go from the intersection Rectangle
to an appropriate counter-velocity?