Search code examples
2dphysicscollisionaabb

Physics - How do you adjust after an AABB collision?


When a player collides with a block, I move them based on the smallest axis change, which makes sense.

However, this gets confusing when many blocks interact with each other.

Box Collisions

  • Black Square - Player, Moving
  • Red/Blue Squares - Blocks, Static

From this example, the player obviously should move up, but the boxes individually have different viewpoints

  • The blue box is collided more on the Y axis, so the black box should move left. You can see this easier if you pretend the red box doesn't exist
  • The red box is collided more on the X axis, so the black box should move up

How do I know which box is "correct"?


Solution

  • The solution is to find the closest blocks for X and Y

    In this situation, the Red block is closer on the X axis, so it should be used to fix the Y axis

    Also, to prevent a block from moving the player to a corner, add a grace buffer. In the image below, the purple and green areas are slightly smaller then the size of the player.

    Box Collisions

    If a block is the closest on one of the axes, and it is in that area, then it should be used for collisions