Moving in 3D space
--------------------
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
Whoa.. not to be ignorant or anything as such, but im not going to try to understand that code.
What i use for my movement, is points, vectors and _parametric equations_ (just finished learning them @ school )
Take the camera''s position (Cx,Cy,Cz), the camera''s view point (Vx,Vy,Vz), find the direction vector between the two, turn it into a unit vector, & put it all into something like this:
[x,y,z] = [Cx,Cy,Cz] + t[(Vx-Cx)/length,(Vy-Cy)/Length,(Vz-Cz)/Length)]
then all you have to do is find the point on that line which is t units from the camera''s current position.
-- Wav
What i use for my movement, is points, vectors and _parametric equations_ (just finished learning them @ school )
Take the camera''s position (Cx,Cy,Cz), the camera''s view point (Vx,Vy,Vz), find the direction vector between the two, turn it into a unit vector, & put it all into something like this:
[x,y,z] = [Cx,Cy,Cz] + t[(Vx-Cx)/length,(Vy-Cy)/Length,(Vz-Cz)/Length)]
then all you have to do is find the point on that line which is t units from the camera''s current position.
-- Wav
Don''t worry wAVaRiaN, it took me a lot of time to understand and write those 5 lines of code... I''m at university, and none of my Math courses showed something like this
But, I''ll try your idea... Maybe I''m to far from the real answer Anybody else what to try ?
Guillaume, a.k.a Yogui
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement