## OpenGL Moving a guy around

Nibbie
Posts: 1
Joined: 2009.05
Post: #1
Hello.

This will probably sound like a pretty stupid question, but I really can't figure it out =P

I am quite new to OpenGL, and I have made a basic program with a guy standing on a flat panel that is 1000 units in all directions. Using the forwards and backwards keys, I can move this guy backwards and forwards on the Z plane. What I want to accomplish is using the left and right keys, I want to be able to rotate this guy about the Y axis, and then move him backwards or forwards based on his rotation using the forwards and backwards keys.

Can I just rotate before I translate, so that he is still technically moving on the Z plane? If so, how do I find the right axis of rotation? Or do I have to somehow figure out how much to move him on the X and Z plane based on his rotation?

Thanks.
Moderator
Posts: 1,563
Joined: 2003.10
Post: #2
Ahh, this sort of thing is much more complicated than it appears at first glance. Here's what I'd do:

Keep two state variables with your character: A quaternion for orientation and a vector for position. Update the quaternion by rotating it on the Y axis when you want your character to turn. Multiply a default forward vector (say, {0, 0, 1}, if that's the direction your character faces initially) by the quaternion, and add the result to his current position (multiplying by a speed constant if necessary).

When you draw your character, you'll want to first translate out to the current value of his position vector, then multiply the current matrix by the orientation quaternion's matrix representation.