user130955
user130955

Reputation: 75

Move according to camera direction

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 wish to move the camera in the direction its looking, and to move objects according to the camera direction (for example, to move object to the camera and away from camera).

I've tried achieving this using the modelview matrix as specified in here: https://stackoverflow.com/a/16137191/3362159 , but this doesn't seem to work. For example, I tried moving the camera forward (according to its direction) using the following code:

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

where "front" is the matrix specified in the answer. The camera doesn't move forward, It moves differently depending on its direction.

What am I doing wrong? Any help would be appreciated.

Upvotes: 0

Views: 1262

Answers (1)

starmole
starmole

Reputation: 5068

Did you try just glTranslatef(0,0,units_forward)? Is that maybe what you expect?

The root cause of your confusion is how matrices work. gl(Matrix) functions all multiply the current matrix with the matrix they build. As keltar corrected on the left side. One strange thing about matrices is that A*B != B*A. Usually the right side of a product "happens first". So if A is a rotation and B is moving forward, A*B means "move forward then rotate around the point I moved to" while B*A means "rotate and then move forward where we are pointing to ahead now".

The answer you reference takes a not very helpful shortcut.

You always have two option:

  • gl(Operation) multiplies left
  • glGet(tempmatrix), glLoadIdentity() gl(Operation) glMultMatrix(tempmatrix) multiplies right

Try both! See how they are different. One of them will do what you want with just Operation=Translate and (0,0,amount).

Upvotes: 1

Related Questions