Camera rotation to direction vector

22 views Asked by At

I need help on something I'm struggling with very much.

I am working on a TouchDesigner patch where I have a Camera COMP with rotation on x, y and z axes. I need the formulas to convert such rotations in a direction vector, that I am going to use in other programs. The point is having camera and for instance, a microphone, pointing in the same direction.

I have implemented the rotation matrix as in: https://en.wikipedia.org/wiki/Rotation_matrix [General 3D rotation]

With this equations implemented I get the direction vector correctly but only when rotating on a single axe. But when I rotate on multiple axes it gets weird.

Example: the initial camera vector is {0,0,-1} with Y-rotate 45° the equation gives me {-0.707, 0, -0.707}, and it looks like the camera is actually pointing in that direction

now things get weird with X-rotate 90° the equation gives me {-0.707, -0.707, 0}, which is weird because the camera is now pointing in the negative Y, as if the vector was {0, -1, 0} instead of {-0.707, -0.707, 0}

I am missing something huge in this transformation. Can anybody please help me out? Thanks

*** the code that implements the matrix product is:

// input rotation values to radians
rotX    = (in1/360.0)*TWOPI;
rotY    = (in2/360.0)*TWOPI;
rotZ    = (in3/360.0)*TWOPI;

// initial direction vector

Dx = 0;
Dy = 0;
Dz = -1;

// calculate intermediate values

// GAMMA rotation on X
cosG    = cos(rotX);
sinG    = sin(rotX);

// BETA rotation on Y
cosB    = cos(rotY);
sinB    = sin(rotY);

// ALFA rotation on Z
cosA    = cos(rotZ);
sinA    = sin(rotZ);    

// Matrix product

MA1     = cosA*cosB;
MB1     = (cosA*sinB*sinG)-(sinA*cosG);
MC1     = (cosA*sinB*cosG)+(sinA*sinG);

MA2     = sinA*cosB;
MB2     = (sinA*sinB*sinG)+(cosA*cosG);
MC2     = (sinA*sinB*cosG)-(cosA*sinG);

MA3     = sinB*(-1);
MB3     = cosB*sinG;
MC3     = cosB*cosG;

// updated Direction vector

nDx     = (Dx*MA1)+(Dy*MA2)+(Dz*MA3);
nDy     = (Dx*MB1)+(Dy*MB2)+(Dz*MB3);
nDz     = (Dx*MC1)+(Dy*MC2)+(Dz*MC3);

out1    = nDx;

out2 = nDy; out3 = nDz;

0

There are 0 answers