views:

338

answers:

3

Hopefully easy answer, but I cannot get it.

I have a 3D render engine I have written.

I have the camera position, the lookat position and the up vector.

I want to be able to "tilt" the camera left, right, up and down. Like a camera on a fixed tripod that you can grab the handle and tilt it it up, down, left right etc.

The maths stumps me. I have been able to do forwards/backwards dolly and up/down/left/right panning, but cannot work out the vector math to get it to tilt.

For left and right tilt I want to rotate the lookat position around the camera position, but I need to take into account the up vector, otherwise the rotation doesn't know which axis to to turn around.

The maths/algorithm I need is along the lines of...

Camera=(cx,cy,cz) Lookat=(lx,ly,lz) Up=(ux,uy,uz)

RotatePointAroundVector(lx,ly,lz,ux,uy,uz,amount)

Can anyone assist with the maths involved?

Many thanks.

A: 

It may not be mathematically optimal, but you could de-compose the camera to look-at vector into three vectors - two that define the horizontal plane, and the up vector.

To get the horizontal plane, you can get one vector as the cross product of the up vector and the camer to look-at vector. You can then get the other orthogonal vector by taking the dot product of up and the vector just found. Don't forget to normalize your three vectors.

Next take the dot product of your camera to look-at vector with your three vectors, and now do the rotation about the horizontal plane (treating your vectors as though they were the coordinate axis).

Sorry for putting it all in words. I hope this helps.

Matthias Wandel
+1  A: 

You could make a point infront of the camera, then rotate that around the camera (using trig) and use your lookat() to rotate the camera.

This wont work once you have to start using roll (rotation around the Z). So you might wanna create a quaternion based camera class. This link helped me: gpwiki.org/Quaternion...

tm1rbrt
A: 

Basically you visualize a local coordinate system for your camera.

The local Z is the lookat vector (from the camera center to the lookat point) and is "into the screen". The local Y will be toward the up of the screen. If you maintain your camera up vector perpendicular to the camera lookat vector, then the up vector is also the local Y. The local X will be normal to the two other axes (it is their cross product after their normalization).

Rotating left-right is a matter of rotating the lookat vector about the local Y axis (yaw). Rotating up-down is a matter of rotating the up vector and the lookat vector about the local X axis (pitch). "Tilting the head" left-right is a matter of rotating up vector about the local Z axis (bank).

Each time you change the lookat vector or the up vector, you need to recompute the local axes.

742