Page 1 of 1

Camera tracking functionality

Posted: 20 Jul 2018, 15:27
by atoz
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

Re: Camera tracking functionality

Posted: 24 Jul 2018, 12:48
by coppelia
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

Re: Camera tracking functionality

Posted: 26 Jul 2018, 23:06
by atoz
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?

Thanks in advance

Re: Camera tracking functionality

Posted: 27 Jul 2018, 14:15
by coppelia
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