Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Rule of the thumb: do as little work as you can get away with. –  Jari Komppa Nov 23 '12 at 12:46
    
Have you looked at "farming out" the problem to a simple physics engine such as Box2D? –  Neil Coffey Nov 25 '12 at 1:17
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
Well what if the wall is not facing on an axis, I think the best solution is Eric's one. –  marcg11 Nov 25 '12 at 19:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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