PDA

View Full Version : Camera following an object


alkisclio
06-17-2007, 05:05 AM
Hello,

I have an object moving around a landscape and I need the camera to follow it's position and orientation. I've tried playing with the quaternion for the mesh orientation, but as I don't know a lot of quaternion and matrix math, I cannot do it alone. Considering that I have the rotation matrix and the rotation quat of the object, how can I build my view matrix so it follows it?

Thank you for your help

Wernaeh
06-17-2007, 08:24 AM
Hey there...

What do you mean by following the object ? :)

Currently, the problem is a little bit unspecific.

If you just want your camera to be statically fixed in relation to the object, it's quite simple.

First, build the absolute camera position, for example by taking the position of your object, and subtracting a fixed vector (0, heightoffset, depthoffset).

(You can also fix the camera at an absolute position by simply using the absolute position vector instead of the given subtraction result.)

Then, build the orientation of your camera: Take a vector from the camera position to the object position. This will be your new looking direction, so normalize it, and place it into the new world transform's z vector (assuming x is to the right, y up, and z into depth). Then, take the object's local x vector from the object's rotation matrix, and use it as the new world transform's x vector. Now we are only missing a new y (up) vector for your camera. This one can easily be derived from the x and z vector already existant in your transform: since y is perpendicular to x and z, use a cross product on these to get your y vector.

This camera now always is right behind the object, floats a little bit above it, and has the object at the center of the viewport.

However, especially for large distances and fast-turning objects this might produce undesireable behaviour, i.e. too fast spinning, and non-smooth camera movement. For these cases, the camera should always have some movement dampening. For this, refer to spherical-linear interpolation (slerp), and report back with questions.

Hope this helps.

Cheers,
- Wernaeh

alkisclio
06-19-2007, 12:57 AM
I will try this tonight, thank you very much :)

Yes I will use some interpolation. This is for a racing game by the way, so there is a lot of fast object movement.