PDA

View Full Version : Move the camera to the direction it points

deshan
05-08-2011, 06:22 AM
Hi
This may be a very basic question, but I do not know how to do this.
I am trying to make a fps type camera and I am using following code to move and setting orientation of the camera

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glRotatef(rollValue, 0, 0, 1); // roll
glRotatef(yawValue, 0, 1, 0); // yaw
glRotatef(pitchValue, 1, 0, 0); // pitch
glTranslatef(x, y, .z); // to move

// then draw shapes

The problem is I can move the camera but not to the direction it points to.
Could you please tell me how to do that?

zeoverlord
05-08-2011, 12:23 PM
there are basically two ways, the first one is to use basic trigonometry like this

#define PI_OVER_180 0.017453292519943295769236907684886

x=((float)sin(-yawValue/PI_OVER_180)*forward)-((float)sin(+(yawValue+90)/PI_OVER_180)*strafe);
y=((float)sin(pitchValue/PI_OVER_180)*forward)+up;
z=(-((float)cos(-yawValue/PI_OVER_180)*forward))-((float)cos(+(yawValue+90)/PI_OVER_180)*strafe);

or you could get the modelview matrix and then multiply it with the vector you wish to move in (with the fourth value being 0) and get the result directly.