martiert
martiert

Reputation: 1686

Best way to make a camera both move and rotate

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.

Upvotes: 3

Views: 3038

Answers (2)

stusmith
stusmith

Reputation: 14103

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).

Upvotes: 5

Raibaz
Raibaz

Reputation: 9710

I don't really have a solution for your problem, but i know the open-source 3d engine Irrlicht has a FPS camera that does exactly what you're looking for and, usually, it has a well-documented source.

You could try looking at how the FPS camera is implemented in Irrlicht or you could even use Irrlicht itself for your project; i used it for a couple of mine when i was in college and it always worked like a charm :)

Upvotes: 0

Related Questions