user195257
user195257

Reputation: 3316

OpenGL rotate and translate problems

I have an plane object, I want it to be able to move forward and rotate left and right. I want the plane to go forward in the direction it is facing.

This is the code I have, but it's not working, where am I going wrong?

    directionX=sin(heading*M_PI/180.0);
    directionZ=-cos(heading*M_PI/180.0);

    if(keys['W']){
        eyeX += directionX;
        eyeZ += directionZ;
    }
    if(keys['S']){
        eyeX -= directionX;
        eyeZ -= directionZ;
    }
    if(keys[VK_LEFT])
        heading -= 1;
    if(keys[VK_RIGHT])
        heading += 1;

The plane is rotated using heading, and translated using eyeX & eyeZ.

//Aircraft
        glPushMatrix(); 
            glRotatef(-heading, 0,1,0);
            glTranslatef(eyeX,eyeY,eyeZ);
            model.speedDisplayFaceNormals();
            //model.drawBoundingBox();
            //model.drawOctreeLeaves();
        glPopMatrix();

Upvotes: 0

Views: 380

Answers (2)

Richard
Richard

Reputation: 21

In OpenGL the Eye is at 0,0,0 and looking down the negative Z axis. So, if you want to use and eye/camera coordiate based system then you need to do two things at the beginning, in this order:

1 - Rotate your scene based on the direction of your camera in your system 2 - glTranslatef(-EyeX,-EyeY,-EyeZ) This takes you to the 0,0,0 in your scene coordinate system.

You can then translate to the positions of your objects, rotate and render them accordingly. I made some notes on this a few years back and this is how I did it back then when looking at The Eye coordinates representing the player location first person style.

Upvotes: 0

nonVirtualThunk
nonVirtualThunk

Reputation: 2852

This is sort of a shot in the dark without knowing the actual behavior you're getting, but you might try switching the order of the calls to glRotatef and glTranslatef.

Upvotes: 2

Related Questions