3
\$\begingroup\$

I have a quaternion from an IMU that id like to represent in unity. The issue is that the sensor uses a right handed coordinate system while unity uses a left handed coordinate system. In order to have the rotations of the IMU reflect in unity correctly, I would need to remap the axis. How can I do this by altering the quaternion components?

Specifically, I would need to map

         sensor   unity
forward  x        z
up       z        y
right    -y       x

I have seen multiple questions regarding this question especially this one.

Convert quaternion to a different coordinate system

However, it only explains a specific case where its a right hand to right hand remapping.

If possible, include an explanation without mathematics equations of how you would map any coordinate system to any other coordinate system.

\$\endgroup\$
4
\$\begingroup\$

A quaternion can be thought of as an angle-axis representation:

quaternion.xyz = sin(angle/2) * axis.xyz
quaternion.w = cos(angle/2)

So, converting them between two coordinate systems can be broken down into two steps:

  1. Map the axis into the new coordinate system.
  2. If changing between left & right hand coordinates (eg. if there's an odd number of axis negations or axis exchanges between the two), negate the angle.

    Since cos(-angle) = cos(angle) and sin(-angle) = -sin(angle) this is the same as flipping the axis of rotation, negating the x, y, and z parts.

Taking your specific example:

         sensor   unity
forward    x        z
up         z        y
right     -y        x

We can combine these steps into:

Quaternion ConvertToUnity(Quaternion input) {
    return new Quaternion(
         input.y,   // -(  right = -left  )
        -input.z,   // -(     up =  up     )
        -input.x,   // -(forward =  forward)
         input.w
    );
}
\$\endgroup\$
  • \$\begingroup\$ So to simplify, remapping the axis is basically remapping the xyz components and inverting them if changing between system. Am I to say that with the property of cos, the w component will never change regardless of what you are remapping to? \$\endgroup\$ – DarkDestry Apr 28 '18 at 15:15
  • \$\begingroup\$ Yes, that's exactly right. \$\endgroup\$ – DMGregory Apr 28 '18 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.