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.

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

share|improve this question

closed as too localized by Sean Middleditch, Trevor Powell, Nick Wiggill, Josh Petrie, michael.bartnett Jan 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.

    
    
That is possibly the most annoying thing you could say. ITS NOT. –  opiop65 Dec 29 '12 at 12:45
1  
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

1 Answer 1

up vote 1 down vote accepted

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.