Hi,

I wonder if you can give some details about how the camera tracking is implemented in the camera properties dialog. I am specifically wondering about how the camera always focuses on the object and stays heads up.

I've been reading around about how cameras are implemented and have come across the popular methods where the camera is manipulated using a view matrix consisting of the camera's forward,up, right vectors. Is this also the case here?

Thanks

## Camera tracking functionality

### Re: Camera tracking functionality

Hello,

the procedure is as follows: first, we orient the camera so that its Z-axis points towards the tracked object. Then we rotate around the camera's Z-axis until its Y-axis points up.

In C++, we have for step 1:
and for step 2:
Cheers

the procedure is as follows: first, we orient the camera so that its Z-axis points towards the tracked object. Then we rotate around the camera's Z-axis until its Y-axis points up.

In C++, we have for step 1:

Code: Select all

```
C7Vector tracked(tr->getCumulativeTransformation()); // tracked object transformation (position + quaternion)
C7Vector self(getCumulativeTransformation()); // camera transformation
C4Vector rot1(self.Q.getAxis(2),tracked.X-self.X);
self.Q=rot1*self.Q;
// We check if the camera looks to +Z or -Z:
C3Vector zAxis(self.Q.getAxis(2));
if ( (fabs(zAxis(0))>0.00001f)||(fabs(zAxis(1))>0.00001f) )
{ // Camera does not look to +Z or -Z:
C3Vector rotAxis(zAxis^C3Vector(0.0f,0.0f,1.0f));
C4Vector rot(piValue_f/2.0f,rotAxis);
zAxis=rot*zAxis;
C4Vector rot2(self.Q.getAxis(1),zAxis);
self.Q=rot2*self.Q;
C7Vector parentInv(getParentCumulativeTransformation().getInverse());
setLocalTransformation(parentInv*self);
}
```

Code: Select all

```
C7Vector cameraCTM(getCumulativeTransformation());
C3X3Matrix trM2(cameraCTM.Q);
if ( (fabs(trM2.axis[2](0))>0.00001f)||(fabs(trM2.axis[2](1))>0.00001f) )
{ // We have to do it:
float val=1.0f;
if (trM2.axis[1](2)<0.0f)
val=-1.0f;
C3Vector rotAx(trM2.axis[2]^C3Vector(0.0f,0.0f,val));
C3Vector target(C4Vector(piValue_f/2.0f,rotAx)*trM2.axis[2]);
C4Vector rot(trM2.axis[1],target);
cameraCTM.Q=rot*cameraCTM.Q;
setLocalTransformation(getParentCumulativeTransformation().getInverse()*cameraCTM);
}
```

### Re: Camera tracking functionality

I'm afraid I'm not able to make sense of that code as it is too advanced. I need a more simple explanation with detail. Can you give a run-down of the algorithm in pseudocode please?

Specifically how is

Thanks in advance

Specifically how is

this done?orient the camera so that its Z-axis points towards the tracked object

Thanks in advance

### Re: Camera tracking functionality

You probably have several ways of doing this. The way used in the C++ code is following:

First, a cross vector is computed between the Camera Z-axis and the vector formed by (cameraPos,trackedObjectPos). This gives you the rotation axis. Next, you can compute the angle between both vectors. With the axis and angle, you can rotate your camera, which is then pointing towards the tracked object.

Finally you need to rotate around the camera's Z-vector until its Y-vector points up (you'd use a similar approach as above).

Cheers

First, a cross vector is computed between the Camera Z-axis and the vector formed by (cameraPos,trackedObjectPos). This gives you the rotation axis. Next, you can compute the angle between both vectors. With the axis and angle, you can rotate your camera, which is then pointing towards the tracked object.

Finally you need to rotate around the camera's Z-vector until its Y-vector points up (you'd use a similar approach as above).

Cheers