Camera Math Problem

Started by
2 comments, last by timw 18 years, 7 months ago
Hey guys, I'm trying to create a camera class that will move the camera exactly as the camera moves in games like Unreal Tournament. The pitch is always calculated first, and the yaw is always rotated around the Y axis. Assume that if the camera is pointing straight down, it is at a 180 degree angle. If it is pointing straight up, it is at a 0 degree angle. If the pitch is at 160 degrees (almost facing directly down), then the pitch should always remain at 160 degrees while you yaw left and right with the camera. This is how the Unreal Tournament camera works. My camera class has three 3D vectors: LookAt, EyePosition, and UpVector. I've tried applying a rotation matrix to the LookAt position and then rotating that. Here's my math: 1) Assume in this example that the Eye is at [10,10,10] and LookAt is at [0,0,0]. You first move the eye to the origin temporarily for the rotation of the yaw.

TempAt        = LookAt   -   EyePosition;
  |               |            |
[-10,-10,-10] = [0,0,0]  -   [10,10,10]
2) Secondly, convert TempAt into a matrix and then rotate it. 'r' in this example is the angle (theta) in radians to which you wish to rotate the camera around the Y axis

     TempAt

        |

   [-10,-10,-10]

        |

[-10  0    0    0]   [cos(r)  0       -sin(r) 0]
[0    -10  0    0] * [0       1       0       0]
[0    0    -10  0]   [sin(r)  0       cos(r)  0]
[0    0    0    1]   [0       0       0       1]
3) Thirdly, I convert the resulting product of the above matrix multiplication back into a vector, which is my new rotated lookat point. Also I move the look at point back in its original position. In this example, 'r' is the new value received from the rotation. 'nr' is the new absolute lookat point.

LookAt      = TempAt   +   EyePosition;
  |             |            |
[nr,nr,nr]  = [r,r,r]  +   [10,10,10]
----------- I am using OpenGL. I can't seem to figure out how to do this, and so far the camera isn't doing what I want it to do. IF anyone could help I would greatly appreciate it. So far the X and Z values of the yaw always come out to be the same exact point after concatenating the rotation matrix. This results in a straight up and down movement of the camera (pitch) instead of a right and left movement (yaw). Thank you!
Advertisement
I didn't look for the mistake in your example, but I'll just say that unless you have some compelling reason for using your current method, you should probably choose a different fundamental representation for your camera.

As to what that representation might be, there are several options. For FPS camera movement like you describe, the most practical options would probably be:

a) Construct the basis vectors directly using trig and load them to OpenGL via glMultMatrix()
b) Use Euler angles and glRotate()

The advantage of a is that you have the basis vectors available for movement, firing weapons, etc. The advantage of b is that you can use glRotate(), which is convenient.

I'll be glad to provide further details if you need them. Also, I have some tutorial code on this subject that I could post if you think it would be helpful.

(If you do need more help, you might mention which axes you consider to be up and forward for your objects.)
using gluLookAt ???

However if your camera does not roll ( that is local z-axis rotation ) you can use simply polar coordinates (longitude and latitude).
when I think of a camara I think it best to conceptually visualize it as an object. every object has a position/orientation matrix M and the camara is no exception. so we pretend the camara is an object and the matrix M positions the camara in worldspace. that is to say M takes a camara from camara local space to global space. thus the inverse of M takes global space to camara local space. so the inverse of M is the matrix we use to transform to camara space.
a really simple example consider a camara object with eye at (0,0,-3) looking in the direction (0,0,-1). to position this camara in world space we use the matrix

[1 0 0 0
0 1 0 0
0 0 1 -3
0 0 0 1]

now to adjust the world to be in the camras point of view we simply translate the entire world +3 in the z direction that is we multiply it by the inverse of the camara object matrix

[1 0 0 0
0 1 0 0
0 0 1 3
0 0 0 1]

Tim

This topic is closed to new replies.

Advertisement