#### Archived

This topic is now archived and is closed to further replies.

# automatic camera tracking (easy maths?)

## Recommended Posts

Dark_Streak    122
Hello, This is probably an easy one for the maths gurus! I am trying to make an automatic tracking camera, but I am struggling with the maths Basically, I want the camera to rotate from its current position so that its pointing towards a defined point. This way, I will be able to track an object with the camera, since I have the objects coordinates. Now , I could really do with some help for the maths behind this one. Say I am looking (via glulookat) straight forward, and the point I want to be looking at ( a 3D vector ) is forward but slightly to the left of the camera's view. Now, because this point I want to look at is simply a 3D vector, how can I tell if my camera view is looking at it? Or how do I tell which way to swing my cameras view until it IS looking at that point? I have attempted to use cross product calculations in order to get the angle, but I am still unsure how to progress from here. Am I needing something from frustrum culling, or is there a more efficient (read less code) method? My camera is based on Gametutorials camera class, just in case you are interested. Any suggestions are greatly appreciated! [edited by - Dark_Streak on June 7, 2002 7:44:13 PM]

##### Share on other sites
Dark_Streak    122
(more specifically to the forum thread master) Would it be okay to post this question in the maths related section aswell, as I feel I would probably get a better response their, due to the subject matter?

##### Share on other sites
JuNC    236
gluLookAt is designed to allow you to point the 'camera' at whatever point you like, I can't remember the exact prototype, something like

gluLookAt(float from_x,float from_y,float from_z,float to_x,float to_y,float to_z,float up_x,float up_y,float up_z)

Simply set the to_x,to_y,to_z values to the point you want to look at and hey presto you are looking at the point (you might need to check the docs for gluLookAt coz that might not be the correct argument layout but it is similar).

If you need to make the camera swing around to look at the point, just generate a series of points between the current 'to' point and the desired 'to' point and update it each frame.

Using angles is quite complex, the actual math behind gluLookAt can be seen in this example:

    Matrix3X3 MakeLookAt(const Vector3& from,const Vector3& to,const Vector3& up){  Vector3 zAxis = Normalize( -(to - from) );  Vector3 xAxis = Normalize( Cross(up,zAxis) );  Vector3 yAxis = Cross(zAxis,xAxis);  return Matrix3X3(xAxis[0],xAxis[1],xAxis[2],                   yAxis[0],yAxis[1],yAxis[2],                   zAxis[0],zAxis[1],zAxis[2]);}

This creates a 3X3 matrix for rotating the world to look at a particular point.

[edited by - JuNC on June 9, 2002 10:46:55 AM]