• Create Account

# Quaternions: convert between Left-Right handed without using Euler

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Edy  Members   -  Reputation: 242

Like
0Likes
Like

Posted 21 March 2014 - 04:33 AM

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!

Edited by Edy, 21 March 2014 - 04:35 AM.

### #2apatriarca  Crossbones+   -  Reputation: 2174

Like
1Likes
Like

Posted 21 March 2014 - 06:09 AM

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.

Edited by apatriarca, 21 March 2014 - 07:25 AM.

### #3Buckeye  GDNet+   -  Reputation: 10734

Like
1Likes
Like

Posted 21 March 2014 - 09:34 AM

..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).

Edited by Buckeye, 21 March 2014 - 09:38 AM.

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.

### #4Edy  Members   -  Reputation: 242

Like
0Likes
Like

Posted 21 March 2014 - 01:00 PM

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!

### #5apatriarca  Crossbones+   -  Reputation: 2174

Like
0Likes
Like

Posted 21 March 2014 - 02:43 PM

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)

### #6Edy  Members   -  Reputation: 242

Like
0Likes
Like

Posted 21 March 2014 - 11:35 PM

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

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

Edited by Edy, 22 March 2014 - 07:19 AM.

### #7apatriarca  Crossbones+   -  Reputation: 2174

Like
0Likes
Like

Posted 22 March 2014 - 05:28 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS