# Can a quaternion be used to convert between coordinate systems?

I'm trying to convert hierachical rotation matrices (from a Kinect) to manipulate a mesh in the game engine.

I was trying to change the Quaternion values like:

  qt.w = qr.w
qt.x = qr.x
qt.y = -qr.z
qt.z = qr.y


Should this work mathematicaly?

My models joints jump randomly around (this with having only a few matrices changed from idendity to have a chance while debugging):

• My intuition is that's not mathematically correct. Did you tried using the to_euler() and to_quaternion() functions ? – Polosson Dec 1 '13 at 23:13
• Are you adjusting for the fact that the bones x,y,z are local to the bone, not matching global x,y,z? – sambler Dec 3 '13 at 3:55

I'm not into quats at all, but here's something I noticed, which may or may not be of any use.

Swap y and z in a matrix and look at the quat:

>>> Matrix(((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1))).to_quaternion()
Quaternion((1.0, 0.0, 0.0, 0.0))

>>> Matrix(((1,0,0,0),(0,0,1,0),(0,1,0,0),(0,0,0,1))).to_quaternion()
Quaternion((0.9999999403953552, 0.0, 0.0, 0.0))

# both matrices give different orientations, but not the quats!


And here's a custom space matrix:

>>> Matrix(((1,0,0,0),(0,0,-1,0),(0,1,0,0),(0,0,0,1))).to_quaternion()
Quaternion((0.7071068286895752, 0.7071068286895752, 0.0, 0.0))

>>> Matrix(((1,0,0,0),(0,0,1,0),(0,-1,0,0),(0,0,0,1))).to_quaternion()
Quaternion((0.7071068286895752, -0.7071068286895752, 0.0, 0.0))


Note the difference 1.0 / 0.0 vs. 0.7 / 0.7 (whereas the matrices are normalized, with using zeroes and ones only) - it might not be enough to negate the sign of a quat component.

I suggest you create a custom matrix for the space conversion and cast it to quat.