views:

853

answers:

3

Hi.

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.

A: 

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

Raibaz
+4  A: 

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).
stusmith
Thanks, when going through it on paper it seems like it will work. So I can't wait trying to implement it.
martiert
I have stumbled across a problem here, how can I find the angle between the view vector and the vector for the mouse on the unit spear around the up vector? I can't just use the dot product of the two vectors, since that would give it in the world space coordinates instead of the camera coordinates.
martiert
Can you clarify... I'm not sure what you mean by "the vector for the mouse on the unit spear". The mouse and keyboard simply modify the three camera vectors.
stusmith
PS I've found this article that might help:http://www.gamedev.net/community/forums/topic.asp?topic_id=440585
stusmith
Aaaah... cleared it a bit upp now. I were just still stuck in my way of thinking, which didn't go to well:P Thanks for the help.
martiert
A: 

I have stumbled across a problem in the implementation. The problem is the step where I update the up and view vector. How can I find the angles for the two updating steps? I can't just use the angle between the view and mousevector (?) since that will be the angle for the world-space coordinates, and not the angle in the cameras x,z plane. So I wonder how I can find the two angles I need for rotating the two vectors. I have googled it, but can't find an answer.

martiert