# How to handle walking into walls?

Say we have a player who's walking around in first person perspective. He would like to walk forward, but that would put him inside an object, and that's no good.

Right now what I do is... (pseudocode follows)

class Player(whatever):
def __move_by(this, displacement):
for object in scene:
if(object.hitbox.collides_with(displacement)):
return #nope.avi
this.position += displacement


This isn't really good however. Often times I need to watch the console debug output to understand what I would be walking into. Also, not moving at all isn't what happens in "real world" games. I am however in trouble when putting into words, or code, what is it that happens in "real world" games.

What I do with NPCs is calculate the projection of the movement towards their goal along the direction they're heading to, as well as rotating them towards their goal in the process. However, here there's no "goal" distinct from the heading - if we ignore strafing, all the player can do is walk back and forward along his heading exactly.

So... how to handle this?

• Feel free to use my drawing for your answer - just make sure you make your own copy first via File, Make a Copy. To make things easier, P is in a 60 degrees heading and you can draw things in constrained angles by holding Shift. – badp Jan 30 '12 at 16:18
• Sees someone else who uses internet memes in code comments I want to live on this planet now. – Matt Jensen Jan 31 '12 at 0:48