Advertisement Jump to content
Sign in to follow this  
Edy

Quaternions: convert between Left-Right handed without using Euler

This topic is 1765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Edited by apatriarca

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

 

 

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

Edited by Edy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!