Quaternions: convert between Left-Right handed without using Euler

Started by
5 comments, last by apatriarca 10 years, 1 month ago

Hello,

I've written a tool that converts rotations from Right Handed (Blender) to Left Handed (Unity 3D). The tool runs in Unity 3D. I'm given a quaternion with the original rotation, and I calculate the quaternion with equivalent rotation as output. The only way I've found so far requires to extract and operate the Euler angles.

Actually this code runs fine:


// "rot" contains the input Quaternion with the original rotation

rot = Quaternion.Inverse(rot);

// Rename and invert the axes via Euler.

Vector3 euler = rot.eulerAngles;
float eulerY = euler.y;
euler.x = -euler.x;
euler.y = -euler.z;
euler.z = eulerY;

// If the model has been turned around (180º) then invert the rotations around the Y axis

if (m_zReverse)
{
    euler.x = -euler.x;
    euler.z = -euler.z;
}

// Convert the Euler angles back to Quaternion

rot = Quaternion.Euler(euler);

This converts the rotations properly on static objects. The problem is that this breaks the quaternion continuity when applied to rotation keys in animations. I suspect that the conversion back and forth Euler is the cause.

The question is: Could the above conversion be done without actually using Euler angles?

If you want to take a look at the tool itself, it's hosted at GitHub here.

Thank you!

Advertisement

Let M be the (3x3) transformation matrix between the Blender coordinate system and the Unity one. Then the axis is simply transformed using M and the angle should change sign (since M does not preserve the orientation). You thus have to negate the imaginary part and then apply M to it.

EDIT:

The transformation between Blender and Unity coordinate systems is, if I remember correctly, (x, y, z) -> (y, z, -x). If I thus have a quaternion q0 + q1*i + q2*j + q3*k, the corresponding quaternion should be q0 - q2*i - q3*j +q1*k.


..when applied to rotation keys in animations

If you're talking about rotations in animation keys, normally those rotations aren't with respect to the model, they're with respect to the parent of the bone/node/frame.

You shouldn't have to convert those rotations, but apply the right- to left-hand conversion at the root level only.

It likely works for static objects because the rotations are (already) with respect to the root (or world, don't know how you're implementing it).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Let M be the (3x3) transformation matrix between the Blender coordinate system and the Unity one. Then the axis is simply transformed using M and the angle should change sign (since M does not preserve the orientation). You thus have to negate the imaginary part and then apply M to it.

Thank you very much for this hint! It helped me to solve the problem. Actually the above code got simplified to this:


rot = new Quaternion (-rot.x, -rot.z, rot.y, -rot.w);

if (m_zReverse)
    rot = new Quaternion (-rot.x, rot.y, -rot.z, rot.w);


I think that the x,y,z components are those you mention as i,j,k in your edit. The transformation from Blender to Unity is (x, y, z) -> (-x, -z, y).

..when applied to rotation keys in animations

If you're talking about rotations in animation keys, normally those rotations aren't with respect to the model, they're with respect to the parent of the bone/node/frame.

You shouldn't have to convert those rotations, but apply the right- to left-hand conversion at the root level only.

It likely works for static objects because the rotations are (already) with respect to the root (or world, don't know how you're implementing it).

Yes, I must convert those rotations because I'm converting all meshes as well. The problem is described here. If I don't convert rotations then, for example, some animation that is intended to rotate around axis Y would be applied to the actual axis Z.

As long as the modifications to the rotations keys are simply changing signs and exchanging components, then I can do the same with the key's tangents and the continuity is preserved. My problem was that using Euler angles I had to apply a post-processing function to the animation curve for preserving the quaternion continuity, and this introduced noticeable smoothing to the rotations (undesirable).

After applying the apatriarca's hint to my importer then the 2nd level (children) objects applied the rotations perfectly. But still I have a problem with the 1st level (root) objects, as I must apply their rotations an additional rotation of +90º around the X axis. I can apply this rotation to the animation keys, but then the quaternion continuity is not preserved because I don't know how to re-arrange the tangents as result of this X+90 rotation.

I have a new question: How could I apply a 90º rotation around X axis to a quaternion by modifying its components directly? (q0, q1, q2, q3, or in case of Unity: w, x, y, z)

Thank you!

It is a (right or left depending on the order of transformations) multiplication by the quaternion (0, 1, 0, 0).

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

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

I've tried your solution directly but it didn't work. I did some tests and found that the quaternion to multiply by is (?2/2, ?2/2, 0, 0). I found a simple description of quaternion multiplication here. The correct result is:

(w, x, y, z) * (?2/2, ?2/2, 0, 0) = (w-x, w+x, z+y, z-y) * ?2/2

Applying this result on both values and tangents of the keyframes in a quaternion-based animation worked perfectly!!

Thank you very much for your hints! They led me to find the correct solution smile.png

EDIT: FYI, here you can see how the new code looks like.

You are right! I forgot to divide the angle by two when computing the quaternion to use.. unsure.png

This topic is closed to new replies.

Advertisement