quaternion in c4d[Solved]

On 03/02/2013 at 14:43, xxxxxxxx wrote:

Hi

i am trying to import some tracking data (faceshift) into c4d.
everything works fine, except that i can not get the head-rotation work properly.

the head rotation i get from faceshift is in quaternion format and consist of 4 float values (x,y,z,w)

i tried doing something like this:

newMatrix=c4d.utils.RotAxisToMatrix(c4d.Vector(x,y,z),w)
headTarget.SetMl(newMatrix)

i tried a lot with swapping or negating the values x,y,z and w, but had no luck so far.

i know that faceshift and c4d do not use the same coordinates systems (left handed vs right handed), because to get the head-translation correctly i need to negate the z-value.

hope someone can show me the way....

On 03/02/2013 at 15:55, xxxxxxxx wrote:

OK I solved it myself:

Here is how i got it working:

newMatrix = quaternionToMatrix(x,y,z,w)
          headTarget.SetRelRot(c4d.utils.MatrixToHPB(newMatrix))

def quaternionToMatrix(x,y,z,w) :
  xx      = x * x;
  xy      = x * y;
  xz      = x * z;
  xw      = x * w;
  yy      = y * y;
  yz      = y * z;
  yw      = y * w;
  zz      = z * z;
  zw      = z * w;
  vec1=c4d.Vector()
  vec2=c4d.Vector()
  vec3=c4d.Vector()
  vecOff=c4d.Vector()
  vec1.x  = 1 - 2 * ( yy + zz );
  vec1.y  =     2 * ( xy - zw );
  vec1.z  =     2 * ( xz + yw );
  vec2.x  =     2 * ( xy + zw );
  vec2.y  = 1 - 2 * ( xx + zz );
  vec2.z  =     2 * ( yz - xw );
  vec3.x  =     2 * ( xz - yw );
  vec3.y  =     2 * ( yz + xw );
  vec3.z = 1 - 2 * ( xx + yy );
  newMatrix=c4d.Matrix(vecOff,vec1,vec2,vec3)       
  return newMatrix

here is the link where i found the function:
http://web.archive.org/web/20041029003853/http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q54