I would like to know how is the typical collision detection of most games. For example, you control a character which can move in 2 dimensional directions (except up and down). Now lets asume he walks into a wall, most of the games depending on character angle and the BB normal face will only stop the player in one axis, but will continue moving in the other along the wall axis. How is that done?

I've only managed to stop the character from going through the wall by seting the position to the last one in the past frame if the new position colllisions the bounding box. But this just makes the player stop sharply and unrealisticly.

  • \$\begingroup\$ Rule of the thumb: do as little work as you can get away with. \$\endgroup\$ – Jari Komppa Nov 23 '12 at 12:46
  • \$\begingroup\$ Have you looked at "farming out" the problem to a simple physics engine such as Box2D? \$\endgroup\$ – Neil Coffey Nov 25 '12 at 1:17

It sounds like you have collision detection working fine. What you're asking about is collision resolution. To make the character continue moving in a valid direction, don't move him back to his last valid position. That will just freeze him in place in an unnatural fashion. Instead, move him along the wall's normal until he is no longer colliding with it. This will however require you to know more data about the collision than just whether it happened or not, but if you're using bounding boxes it won't be too bad.

This is an image of a bounding box bumping a wall.

So the red dots are the points at which the player is rendered, and the grey line follows the bottom-right corner of the player's bounding box. The player is moving in the x direction and the y direction, but when he hits the wall he is only moved back in the y direction, the x direction is not affected. As a result he will "slide" along the wall. Note that he is always moved up the exact amount required to get him out of the wall.


Your question is a bit vague ("Typical collision detection"), but your concern about stopping the player in one axis can be answered.

The easiest solution (in terms of required work) is to check collisions in each axis separately. You wrote "seting the position to the last one in the past frame if the new position colllisions the bounding box" will work, but you need to check and reset the X and Y coord separately.

  • \$\begingroup\$ Well what if the wall is not facing on an axis, I think the best solution is Eric's one. \$\endgroup\$ – marcg11 Nov 25 '12 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.