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.

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.

enter image description here

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?

share|improve this question
    
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
add comment

1 Answer

up vote 4 down vote accepted

Well, File->Make a Copy doesn't work for me so let's describe the answer the old way :)

First, calculate the intersection point between your movement vector and the wall. Second, compute the wall normal (vector perpendicular to the wall) at the intersection point. Next compute the dot product of your reversed movement vector and the normal (so both vectors have their origins at the same point and the angle is between 0 and +/-90 degrees; if the angle is 0 you just pick a direction at random) -- this way you'll know if the player should slide by the wall left or right. After that it's a matter of computing a vector parallel to the wall (i.e. perpendicular to the normal) in the correct direction and moving your character along this vector.

share|improve this answer
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.