View Full Version : move camera in the direction its looking

05-31-2014, 01:55 PM
I am rotating the camera around itself (when the camera is located at (0,0,0)) using the following:

glRotatef(x_camera_angle, 1.0, 0.0, 0.0);
glRotatef(y_camera_angle, 0.0, 1.0, 0.0);

I now want to move the camera in the direction its looking. For example, I want to move the camera 5 units right and 3 units forward. How can this be done?

I've searched a lot and couldn't find a satisfying answer. Any help would be highly appreciated!

Ilian Dinev
05-31-2014, 06:00 PM
Get the modelview matrix, see the numbers. 3 of the numbers (a column or a row) from the 3x3 sub-section (of the 4x4 matrix) are what you're looking for. Right now I don't remember which row/column.

06-01-2014, 02:42 AM
I tried achieving it using the modelview matrix as shown here: http://stackoverflow.com/questions/16136466/how-can-i-make-my-camera-move-in-the-direction-im-facing
However, this just doesn't seem to work:

float mview[16];
float front[4], up[4], left[4];
glGetFloatv(GL_MODELVIEW_MATRIX, mview);
left[0] = mview[0]; left[1] = mview[1]; left[2] = mview[2]; left[3] = 1.0;
up[0] = mview[4]; up[1] = mview[5]; up[2] = mview[6]; up[3] = 1.0;
front[0] = mview[8]; front[1] = mview[9]; front[2] = mview[10]; front[3] = 1.0;

glTranslatef(front[0] * units_forward, front[1] * units_forward, front[2] * units_forward);

06-01-2014, 07:42 PM
Try with some trigonometry. If you know the angles you can simply use sin and cos to move camera.