Search code examples
c#rotationxnagame-enginequaternions

Switch axes and handedness of a quaternion?


I have a quaternion, coming from a system with the following:

Right handed.
Forward direction: Y axis
Right direction: X axis
Up direction: Z axis

I need to convert this into a coordinate system that is:

left-handed.
Forward direction: X axis
Right direction: Y axis
Up direction: Z axis

I have tried negating the axis, and angle, I have tried switching values, i cannot get this to work. All help greatly appreciated! I am working in C#, with

Microsoft.Xna.Quaternion.

Solution

  • A quaternion is a structure of four values (w, x, y, z). If it represents a rotation, then w = cos(phi/2) (phi being the rotation angle) and (x, y, z) = sin(phi/2) * (ax, ay, az) ((ax, ay, az) being the rotation axis).

    In order to convert the quaternion to another system, it is sufficient to transform the rotation axis. For your example, the transform is:

        / 0  1  0 \
    T = | 1  0  0 |
        \ 0  0  1 /
    

    Finally, since you are changing handedness, you must invert the quaternion or it will rotate in the wrong direction. Summarizing, the transformed quaternion is:

    (w*, x*, y*, z*) = (w, -y, -x, -z)
    

    In general:

    (x*, y*, z*) = det(T) T (x, y, z) //Assuming column vectors