Camera tracking functionality

Typically: "How do I... ", "How can I... " questions
atoz
Posts: 55
Joined: 18 Oct 2013, 09:02

Camera tracking functionality

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

coppelia
Posts: 6736
Joined: 14 Dec 2012, 00:25

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:

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);
}``````
and for step 2:

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);
}
``````
Cheers

atoz
Posts: 55
Joined: 18 Oct 2013, 09:02

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
orient the camera so that its Z-axis points towards the tracked object
this done?

coppelia