Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert quaternion from right-handed to left-handed coordinate system

Tags:

math

graphics

3d

I my 3d program, the rotation of object is represented by the quaternion like [0.130526, 0.0, 0.0, 0.991445]. The program works with right-handed coordinate system with the Z axis pointing up (like in 3ds max):

enter image description here

On the other hand, my application use left-handed coordinate system and the Y axis is up:

enter image description here

How can I transform my quaternion from one coordinate system to another, with the respect for which axis is up?

like image 601
PolGraphic Avatar asked Feb 23 '15 12:02

PolGraphic


People also ask

How do you change to left-handed?

Turn your hands towards yourself so you index fingers are tip-to-tip. Now the thumbs point in the same direction and the middle fingers point in the same direction. Only the index figures go against each other. Go back to the start position, and rotate your hands so your thumbs point towards each other.

How do you transform a quaternion?

To rotate anything by quaternion q you just do q*p*q. inverse() . If p is a vector then you first convert it to "fake" quaternion by setting w=0 and x,y,z same as vector. If p is quaternion then you are good to go.

How do you change the axis of quaternion?

Either you use Z ↦ Y. In that case you change between left-handed and right-handed coordinate system, and the conversion is essentially a reflection. Or you use Z ↦ −Y, then it's just a 90° rotation about the X axis.

Do quaternions have handedness?

Ok, just to be clear, quaternions don't actually have handedness.


2 Answers

A rotation of angle x around axis (u,v,w) can be represented by quaternion with real part cos(x/2) and unreal part sin(x/2)*(u,v,w).

If axis coordinates are (u,v,w) in original trihedron, they will be (u,w,v) in your trihedron.

Thus if original quaternion was (a,b,c,d) - a+ib+jc+kd - the quaternion must be transformed to (a,b,d,c) in your trihedron.

EDIT

But because your trihedron is left handed, the angle also has to be reversed, so the same rotation can finally be expressed by the quaternion (a,-b,-d,-c) in your trihedron.

like image 81
aka.nice Avatar answered Sep 21 '22 08:09

aka.nice


This is a condensed version of an answer to a slightly different question.

The problem you ask about arises even if the two coordinate systems are same-handed; it turns out that handedness flips don't make the problem significantly harder. Here is how to do it in general. To change the basis of a quaternion, say from ROS (right-handed, Z up) to Unity (left-handed, Y up):

mat3x3 ros_to_unity = /* construct this by hand by mapping input axes to output axes */;
mat3x3 unity_to_ros = ros_to_unity.inverse();
quat q_ros = ...;
mat3x3 m_unity = ros_to_unity * mat3x3(q_ros) * unity_to_ros;
quat q_unity = mat_to_quat(m_unity);

Lines 1-4 are simply the method of https://stackoverflow.com/a/39519079/194921: "How do you perform a change-of-basis on a matrix?"

Line 5 is interesting; not all matrices convert to quats, but if ros_to_unity is correct, then this conversion will succeed.

Note that this will give you a correct result, but it goes through a lot of work -- conversion to and from a matrix, some multiplies, an inversion. But you can examine its results and then write a special-case version that rearranges or flips axes, like the one aka.nice derived.

like image 42
Paul Du Bois Avatar answered Sep 19 '22 08:09

Paul Du Bois