Hey all…
Ok,suppose I have a camera in my OpenGL world.Now,I use the mouse to rotate the view so I am now looking in different directions.All that stuff works but how do I rotate the camera’s translation vectors around?
(I might not be clear here.Suppose I rotate my camera 90degrees to the right.Now ,when I press the UPARROW key,my camera moves to the left instead of forward. How do I go about fixing this?)
simple fix:
-retrieve the modelview matrix
-consider only the 3x3 rotation inside
-use the three vectors to translate
in code:
float mat[4][4];
glGetFloatv(GL_MODELVIEW_MATRIX,(float *)mat);#define SPEED 10.0
if(keyright) glTranslatef( +SPEEDmat[0][0], +SPEEDmat[1][0], +SPEEDmat[2][0] );
if(keyleft) glTranslatef( -SPEEDmat[0][0], -SPEEDmat[1][0], -SPEEDmat[2][0] );if(keyup) glTranslatef( +SPEEDmat[0][1], +SPEEDmat[1][1], +SPEEDmat[2][1] );
if(keydown) glTranslatef( -SPEEDmat[0][1], -SPEEDmat[1][1], -SPEEDmat[2][1] );if(keyfw) glTranslatef( +SPEEDmat[0][3], +SPEEDmat[1][3], +SPEEDmat[2][3] );
if(keybw) glTranslatef( -SPEEDmat[0][3], -SPEEDmat[1][3], -SPEEDmat[2][3] );
with this method you consider what the modelview matrix unit vectors really are:
the coordinate frame of your world.
the first row of the matrix tells the X-axis direction, so you have
to translate across this to obtain a correct “strafe”.
the same is valid for the other rows.
maybe would be necessary to reverse the order of row and column indexes into mat:
i don’t remember wich comes first
Dolo//\ightY
I know this one. Unfortunately I can get this to work, but not rotation!
Set two variables, the X current degree and the
Z current degree (initialize Z to 0, but X to 180)
Also, define a speed factor (I’ll call it SF)
then…
You find the angle of rotation (glRotatef(ANGLE,0.0,1.0,0.0)). If you did this right when ANGLE=45 you have rotated 45 degrees from you current spot.(obviously)
Then, when VK_UP (up key) is called enter this tidbit of code:
glTranslatef(sin(X_Current_Degree*(M_PI/180))SF,
0.0,cos(Z_Current_Degree(M_PI/180))*SF);
And for VK_DOWN just do this:
glTranslatef(-sin(X_Current_Degree*(M_PI/180))SF,
0.0,-cos(Z_Current_Degree(M_PI/180))*SF);
HOPE IT WORKS!!!