--------------------
Moving in 3D space
--------------------
The question is simple: How can I move a camera (like a spaceship) in 3D space with opengl without using glu... Well, I know, this needs maths... And I know maths and programming: I''m not a newbie... But don''t worry, I''ve made almost all the code... There is just 1 little thing that I can''t figure out... The translation... So, here''s what I''ve done:
- I get the speed, from the keys the user pressed ;
- The speed only applies to a Z translation (For now, you can only move to the direction you are facing) ;
- I translate the other way: glTranslatef(0.0f, 0.0f, -Zposition) ;
- I get the 3 rotations (ax, ay, az) from the keys the user pressed ;
- I build a quaternion from the 3 rotations ;
- I get a vector and a rotation from the quaternion and I call glRotatef with those arguments ;
- I draw my objects
This is the code ("camera" is a Quaternion object):
// Translate
Zposition += speed;
glTranslatef(0.0f, 0.0f, -Zposition);
// Rotate
camera.setRotation(ax, ay, az);
VECTOR rotation;
float angle;
// Get the Angle Axis representation
camera.getAxis(rotation, angle);
glRotatef(angle, rotation.x, rotation.y, rotation.z);
// Draw
// ...
This code work... But it does not do what I want... This code moves my camera away from the center, and then rotates my camera around the central point. But you can never look somewhere else then at the "center" of the screen. So... I must change the translation code... I''m almost shure of what I must do but I''m unable to write it... I think that I should rotate the vector representing the position of the camera from the same amount of the rotation that I will do and then apply the translation... Like if I wrote:
// Translate
Zposition += speed;
// Create the position vector
VECTOR position;
position.x = position.y = 0.0f;
position.z = -Zposition;
// Rotate the camera
camera.setRotation(ax, ay, az);
// Rotate the vector
camera.applyToVector(position);
glTranslatef(position.x, position.y, position.z);
// Rotate and draw
// ...
... Or maybe I''m totally wrong... because it doesn''t seem to work... or maybe my code for "applyToVector()" is bugged...
Well... you can see that I''ve done the biggest part of the work (I think

... But I can''t translate correctly... I would really appreciate it if you could:
- Answer my question, or
- Seen the URL of a site which explains how to move like this (like a spaceship...), or
- Seen the URL of some code that does this, or
- All of them

Hoping my question was clear enough

Thanks!
Guillaume a.k.a. Yogui