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

Printable View

• 03-06-2012
sbzenth
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!
• 03-06-2012
sbzenth
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?
• 03-06-2012
sbzenth
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:)
• 03-07-2012
VirtualAce
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.
• 03-07-2012
sbzenth
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.