2D Rectangle Collision Response with Multiple Rectangles
Posted
by
Justin Skiles
on Game Development
See other posts from Game Development
or by Justin Skiles
Published on 2012-09-28T15:20:42Z
Indexed on
2012/09/28
15:51 UTC
Read the original article
Hit count: 318
Similar to: Collision rectangle response
I have a level made up of tiles where the edges of the level are made up of collidable rectangles. The player's collision box is represented by a rectangle as well. The player can move in 8 directions. The player's velocity is equal in X and Y directions and constant.
Each update, I am checking the player's collision against all tiles that are a certain distance away. When the player collides with a rectangle, I am finding the intersection depth and resolving along the most shallow axis followed by the other axis. This resolution happens for both axes simultaneously.
See below for two examples of situations where I am having trouble.
Moving up-left against the left wall
In the scenario below, the player is colliding with two tiles. The tile intersection depth is equal on both axes for the top tile and more shallow in the X axis for the middle tile. Because the player is moving up the wall, the player should slide in an upward direction along the wall. This works properly as long as the rectangle with the more shallow depth is evaluated first. If the equal intersection depth rectangle is evaluated first, there is a chance the player becomes stuck.
Moving up-left against the top wall
Here is an identical scenario with the exception that the collision is with the top wall. The same problem occurs at the corners when intersection depth is equal for both axes.
I guess my overall question is: How can I ensure that collision response occurs on tiles that have non-equal intersection depth before tiles that have equal intersection depth in order to get around the weirdness that occurs at these corners.
Sean's answer in the linked question was good, but his solution required having different velocity components in a certain direction. My situation has equal velocities, so there's no good way to tell which direction to resolve at corners.
I hope I have made my explanation clear.
© Game Development or respective owner