Need a little help with 3D movement.

Can someone help me on a 3D movement problem…I got my code to move me in whatever direction I’m facing, however, when I turn, “I” rotate around my 3d world instead of it rotating around me. (Does that make sense?) I don’t pivot in place…I’m struggling with the Trig for this so any help would be greatly appreciated…Thanks again…My code for what I have is below…

if(GetAsyncKeyState(VK_UP)) {
xPos -= (float) sin(yRot * pi/180)* 1.5;
zPos -= (float) cos(yRot * pi/180) * 1.5;

if(GetAsyncKeyState(VK_DOWN)) {
xPos += (float) sin(yRot * pi/180) *1.5;
zPos += (float) cos(yRot * pi/180) * 1.5;

if(GetAsyncKeyState(VK_RIGHT)) {
yRot -= 1.5;

if(GetAsyncKeyState(VK_LEFT)) {
yRot += 1.5;

and in my drawing method…
void drawPicture() {

float scene = 360 - yRot;
glRotatef(scene, 0, 1, 0);
glTranslatef(-xPos, 0, -zPos);

...Draw my picture...


Hmm, are you writing this post because something is wrong, or just to ask if this is the way to go?

Can answer both questions if you like

Yes, this is about the way yo go to move the camera relative to it’s direction.

I can’t see any problems with your code, so it should work.

Moving the camera to the left, is like moving the world to the right. So this way to go is all right.

Obviously, like in everything in this world, there are two ways to do something: the one you want and the opposite.

Murphy says that the first one you’ll find is the one you’re not looking for.

Your code is right, except for a detail: you are not moving what you would want. Maybe I’m a bit slow early in the morning, but xPos and yPos are in your code coords of each object in your world, or rather of the world transform matrix, effecting over everything at once? Both are going to work, but the ‘pivot’ isn’t gonna be the same.

I use the following set of calls to position the “Camera” (ie. Me) and then I draw the objects…

fZoom is just a way of changing the “Zoom” of the camera (typically set to 0.5).

// Set up the projection matrix.
glFrustum(-fZoom, fZoom, -fZoom, fZoom, 2.0, 2000);
glRotatef(Rot.xAng, 1.0f, 0.0f, 0.0f);
glRotatef(Rot.yAng, 0.0f, 1.0f, 0.0f);
glTranslatef(Pos.xPos, Pos.yPos, Pos.zPos);

If I draw all my objects now (with their transformations) then they appear in the correct position. It is IMPORTANT that I use glLoadIdentity() (or glPush/PopMatrix) to reset the transformation back to the center of the OpenGL universe before drawing each object. The critical call may be the glMatrixMode(GL_MODELVIEW);.