Thread: GL/GLUT - Update direction vectors for movement after rotating camera?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    GL/GLUT - Update direction vectors for movement after rotating camera?

    Hey everyone,

    Currently working on a project with a 3d interactive scene of a low poly spaceship model. I have the perspective camera rotating on mouse movement.

    My question is this: I am using the ASDW keys to control walking around the scene. However, when the camera is rotated, its still moving around in global coordinates and not in the correct vectors in relation to the camera. For example, if i rotate the camera 90 deg. to the left, and press the W key to travel forward, it still strafes to the positive X direction (to the right of the camera)

    Here are bits of my code:

    For the rotation on mouse movement:
    Code:
    void myMouseMove(int xw, int yw){
    int xpos,ypos;  // mouse position in coords with origin at lower left.
    
        xpos = xw;
        ypos = getDisplayHeight() - yw;
    
        yrot += 0.3*(xpos-xclik);     // change rot. angle by drag distance
        xrot -= 0.3*(ypos-yclik);     // (drag in X direction rotates on Y axis)
        xclik = xpos;                 // (drag in Y direction rotates on X axis)
        yclik = ypos;                 // update current mouse position.
    }
    Display function piece for drawing the scene:
    Code:
        glMatrixMode (GL_MODELVIEW);    glLoadIdentity ();
        glClear(GL_DEPTH_BUFFER_BIT );
    
        glRotatef(xrot, 1.0f, 0.0f, 0.0f);
        glRotatef(yrot, 0.0f, 1.0f, 0.0f);
        glRotatef(zrot, 0.0f, 0.0f, 1.0f);
        gluLookAt (0.0, 2.0, -10.0,
                   0.0, 2.0, 0.0,
                   0.0, 1.0, 0.0);
        glPushMatrix();
                glTranslated(-movex,movey,-movez);
                drawScene();
        glPopMatrix();

    I've tried several different strategies for correcting the movement but havent gotten anywhere :/

    I'd appreciate any help i can get. Thanks!

    And... theres my first post on this forum! bam!

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Looking at some other sites, I think i see the problem. I need to create a camera class in order to be able to do this. Does this sound correct?

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Figured it out. Here it is for reference if anyone else needs it:
    Code:
     
    
        if(key_state['a'] == true) {
            float yRotRad = (float) (rad*yaw);
            movex -= -moveSpeed * cos(yRotRad);
            movez -= -moveSpeed * sin(yRotRad);
            }
        if(key_state['d'] == true) {
            float yRotRad = (float) (rad*yaw);
            movex -= moveSpeed * cos(yRotRad);
            movez -= moveSpeed * sin(yRotRad);
            }
        if(key_state['w'] == true) {
            float yRotRad = (float) (rad*yaw);
            movex -= moveSpeed * sin(yRotRad);
            movez -= -moveSpeed * cos(yRotRad);
            }
        if(key_state['s'] == true) {
            float yRotRad = (float) (rad*yaw);
            movex -= -moveSpeed * sin(yRotRad);
            movez -= moveSpeed * cos(yRotRad);
            }
    where rad = pi/180.0

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That only works for a '2D' camera.

    What you are looking for is:

    Code:
    void Object::Strafe(float units)
    {
        m_vecPosition += m_vecRight * units;
    }
    
    void Object::Walk(float units)
    {
        m_vecPosition += m_vecLook * units;
    }
    
    void Object::Fly(float units)
    {
       m_vecPosition += m_vecUp * units;
    }
    Those three vectors are rotated via Yaw, Pitch, Roll using axis angle rotation or quaternion rotation. From that a rotation matrix is computed and the vectors are transformed by this matrix.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Hey VirtualAce, thanks for your reply.

    I see what you're doing there. Looks good.
    The thing is, what I did is working for what i wanted because I only want 2D movement around on the ground plane in the 3D scene. While retaining the ability to look around with the mouse. I should have specified that.

    Ill try it with your way as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direction Pad - movement
    By samuelmoneill in forum C Programming
    Replies: 1
    Last Post: 04-30-2009, 05:28 AM
  2. RTS camera movement
    By blurrymadness in forum C++ Programming
    Replies: 0
    Last Post: 04-22-2007, 10:37 PM
  3. OpenGL, glut, and rotating
    By Zalbor in forum C Programming
    Replies: 3
    Last Post: 01-08-2007, 04:12 AM
  4. rotating to a target direction
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 06-24-2005, 03:11 PM
  5. OGL camera movement and vector trouble
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 05-12-2004, 05:33 PM

Tags for this Thread