# WASD movement with OGL.

This topic is 5121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have built my scene and loads so I said why not put some movement with keys. So I built the following functions to be used by glut: void MoveForward() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, angle); angle=angle + 0.1; } void MoveBack() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -angle); angle=angle + 0.1; } void RotateRight() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(angle, 0.0, 1.0, 0.0); angle=angle + 0.1; } void RotateLeft() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(-angle, 0.0, 1.0, 0.0); angle=angle + 0.1; } void keyboard ( unsigned char key, int x, int y ) // Create Keyboard Function { switch ( key ) { case 'w': MoveForward(); glutPostRedisplay(); break; case 's': MoveBack(); glutPostRedisplay(); break; case 'd': RotateRight(); glutPostRedisplay(); break; case 'a': RotateLeft(); glutPostRedisplay(); break; case 'q': exit(0); break; default: break; } } The problem is that if I hit only the W button the movement is succesfull. When I hit the S instead moving back from where it was it jumos way back. The rotate does the same thing but instead it brings everything bak to the origin. I know the problem is with the matrices. I understand that in order to work I must use the last matrix transformation right? Can you help me? Thanks goes to posit and mikeman for their ideas and recommendations. [Edited by - Stranger on December 4, 2004 7:57:41 PM]

##### Share on other sites
I think your method of approaching this needs some rethinking.

Generally speaking, it's best to keep track of camera position and orientation separately (for example, float cam_x, cam_y, cam_angle; or something) and to set up the view transform from scratch each frame, eg:

glPushMatrix ();

glRotatef (-cam_angle, 0.0f, 0.0f, 1.0f);

glTranslatef (-cam_x, -cam_y, 0.0f);

// do rendering...

glPopMatrix ();

##### Share on other sites
My friend thank you. I thought what you said and instead thought of moving the projection and not the model and worked. I didnt use exactly your example but your idea helped me. THANK YOU.

##### Share on other sites
Quote:
 Original post by StrangerMy friend thank you. I thought what you said and instead thought of moving the projection and not the model and worked. I didnt use exactly your example but your idea helped me. THANK YOU.

DON'T DO THAT!

Do not use the projection matrix for camera movement. You must do that using the modelview matrix. Fog will definately look wrong, and I think so is lighting(especially specular) or reflections. GL makes the calculations for all that in eye-space, so the modelview matrix must contain the camera too.

Change that immediately!

##### Share on other sites
Ok ok dont shout:) i changed it and works with model view as well but even with projection I didnt have a problem though. Thanks for the help anyway:).

[Edited by - Stranger on December 4, 2004 6:58:43 PM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013300
×