# OpenGL first person camera orientation issues [closed]

I have a "camera" in my opengl program that I recently finished. However, I've noticed that whenever I rotate and then move again, the x, y, and z angles change. For example, when I press the "w" key, I move forward along the "z" axis. If I then rotate the camera 90 degrees, when I push the "W" key, I will actually be moving right, seemingly along the "x" axis. It makes sense why this happens, I'm just wondering why its happening. Here's the rotation function:

``````private void camera() {
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
}
``````

The keyboard function:

``````if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
xpos -= 0.035 * delta;
}

if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
xpos += 0.035 * delta;
}

if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
zpos += 0.03f * delta;
}

if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
zpos -= 0.035 * delta;
}
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
xrot += 0.035;
if (xrot > 360) {
xrot -= 360;
}
}

if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
xrot -= 0.035;
if (xrot > 360) {
xrot += 360;
}
}

if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
yrot += 0.035;
if (xrot > 360) {
xrot -= 360;
}
}

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
yrot -= 0.035;
if (xrot > 360) {
xrot += 360;
}

}
``````

And my translate function:

``````        glTranslated(xpos, ypos, zpos - 30);
``````

any ideas on how to solve this? I would be very grateful

-

## closed as too localized by Sean Middleditch, Trevor Powell, Arcane Engineer, Josh Petrie♦, michael.bartnettJan 9 '13 at 7:11

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

– Sean Middleditch Dec 29 '12 at 4:27
That is possibly the most annoying thing you could say. ITS NOT. – opiop65 Dec 29 '12 at 12:45
That question includes explicit code examples to do what you want. The only original bit of your question comes down to debugging your code for you, which is not what GDSE is for. If I'm wrong, which is quite possible, that's fine as nobody else will give the other 4 required votes to close, and you're good. No need to snip at me for it. :) – Sean Middleditch Dec 29 '12 at 16:40
Yeah I'm sorry, I shouldn't have snapped at you! – opiop65 Dec 29 '12 at 21:24
Note that your code is also full of elementary coding bugs: `if (xrot > 360) xrot += 360` doesn't make sense, you want `if (xrot < 0)` when you're subtracting from `xrot`. And later on in the left/right case you update `yrot` then you check `xrot` for overflow. – sam hocevar Jan 2 '13 at 9:37

What you're not doing is rotating your x- and y-positions based on which direction your player is pointed. (note that this depends solely on your z-angle, and not on the x and y axes)

You will need to perform your manipulations on a pair of variables that get rotated after all the keys are checked, which also means you'll need to calculate the rotation with good-old sines and cosines.
(note that you'll also need to handle things carefully if you have any slopes whatsoever. Having the player move smoothly up and down them is extremely important for a good user-experience)

The code is relatively simple:

``````// Process the motion commands as normal

// Now rotate the raw values:
final_dx = raw_dx*cos(rot_z) + raw_dy*sin(rot_z);
final_dy = raw_dx*sin(rot_z) + raw_dy*cos(rot_z);
``````

Note that you may have to change a few signs around (ie, multiply by -1) to make sure that the rotations line things up properly.

-
Yeah I realized this as soon as I went to bed last night :/ raw_dy is the originaly variable right? Thanks for your help! – opiop65 Dec 29 '12 at 12:47
What I meant was raw_dy is my xpos variable, which is my position varibale. – opiop65 Dec 29 '12 at 12:59
And I don't have a rot_z variable variable, only a rot_x and rot_y. Is it ok if my rot_z is just set equal to 0? – opiop65 Dec 29 '12 at 13:01
Whoops. I forgot, you're probably using Y as your vertical axis, right? Then you'll need to swap the Ys and Zs in the code I gave. – Raceimaztion Dec 29 '12 at 20:28
Thanks! I actually figured it out using a different method, but your way is by far the most simple and easiest way to do it, thank you! – opiop65 Dec 29 '12 at 21:23