View Full Version : Camera question

TheGecko

04-09-2000, 08:49 AM

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( +SPEED*mat[0][0], +SPEED*mat[1][0], +SPEED*mat[2][0] );

if(keyleft) glTranslatef( -SPEED*mat[0][0], -SPEED*mat[1][0], -SPEED*mat[2][0] );

if(keyup) glTranslatef( +SPEED*mat[0][1], +SPEED*mat[1][1], +SPEED*mat[2][1] );

if(keydown) glTranslatef( -SPEED*mat[0][1], -SPEED*mat[1][1], -SPEED*mat[2][1] );

if(keyfw) glTranslatef( +SPEED*mat[0][3], +SPEED*mat[1][3], +SPEED*mat[2][3] );

if(keybw) glTranslatef( -SPEED*mat[0][3], -SPEED*mat[1][3], -SPEED*mat[2][3] );

<render>

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 http://www.opengl.org/discussion_boards/ubb/smile.gif

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!!!

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.