Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make a camera both move and rotate

Tags:

opengl

I'm learning some OpenGL game programing, and I'm stuck in how to implement so the camera follows the mousepointer. Like in a fps game you want to look where your mouse is pointing, but I can't find a nice solution to this while I'm moving. I was thinking of saving one matrix for the move, like walkking and strafing, while using quaternions to handle the rotation. Then make the quaternion to a rotationmatrix, load the identity for the modelview matrix and time this matrix with both matrixes.

There is ofcourse some problems with this, like which matrix we should use in the multiplication first, and the code will be ugly.

So I'm wondering if anyone have a good solution to solving this, so I don't have to find out which matrix to use first, and which gives cleaner code.

like image 645
martiert Avatar asked Nov 24 '25 06:11

martiert


1 Answers

Store the camera's view details as a position vector, a view-vector and an up-vector (think of pointing with your thumb stuck out: your finger is the view-vector, and your thumb is the up-vector). Keep these vectors normalized and at 90 degrees to each other. You should be able to see that these three vectors are sufficient to represent any camera position and orientation.

You can use these vectors to transform world-coordinates to camera-coordinates:

  • Translate by -position;
  • Rotate around (up-vector 'cross' y-axis) by -(angle between up-vector and y-axis);
  • Rotate around up-vector by -(angle between view-vector and z-axis).

(I might have got some of my signs the wrong way around there).

You can transform these vectors as the user moves the mouse:

  • As the mouse moves sideways, rotate the view-vector around the up-vector (or rotate both view-vector and up-vector around y-axis, if you prefer).
  • As the mouse moves back/forwards, rotate both the view-vector and up-vector around (view-vector 'cross' up-vector).
like image 140
stusmith Avatar answered Nov 27 '25 00:11

stusmith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!