Quaternion to EulerXYZ, how to differentiate the negative and positive quaternion

4.2k views Asked by At

I've been trying to figure out the difference between these, and why ToEulerXYZ does not get the right rotation.

Using MathGeoLib:

axisX:

x   0.80878228  float
y   -0.58810818 float
z   0.00000000  float

axisY:

x   0.58811820  float
y   0.80877501  float
z   0.00000000  float

axisZ:

x   0.00000000  float
y   0.00000000  float
z   1.0000000   float

Code:

Quat aQ = Quat::RotateAxisAngle(axisX, DegToRad(30)) * Quat::RotateAxisAngle(axisY, DegToRad(60)) * Quat::RotateAxisAngle(axisZ, DegToRad(40));
float3 eulerAnglesA = aQ.ToEulerXYZ();

Quat bQ = Quat::RotateAxisAngle(axisX, DegToRad(-150)) * Quat::RotateAxisAngle(axisY, DegToRad(120)) * Quat::RotateAxisAngle(axisZ, DegToRad(-140));
float3 eulerAnglesB = bQ.ToEulerXYZ();

Both to ToEulerXYZ get {x=58.675510 y=33.600880 z=38.327244 ...} (when converted to degrees).

The only difference I can see, is the quaternions are identical, but one is negative. The ToEulerXYZ is wrong though, as one should be the negative ({x=-58.675510 y=-33.600880 z=-38.327244 ...}) (bQ)

AQ is:

 x  0.52576530  float
 y  0.084034257 float
 z  0.40772036  float
 w  0.74180400  float

While bQ is:

 x  -0.52576530 float
 y  -0.084034257    float
 z  -0.40772036 float
 w  -0.74180400 float

Is this just an error with MathGeoLib, or some weird nuance, or maybe someone can explain to me what is going on logically.

There are additional scenarios that are not even negative

axisX:

-0.71492511 y=-0.69920099 z=0.00000000

axisY:

0.69920099 y=-0.71492511 z=0.00000000

axisZ:

x=0.00000000 y=0.00000000 z=1.0000000

Code:

Quat aQ = Quat::RotateAxisAngle(axisX, DegToRad(0)) * Quat::RotateAxisAngle(axisY, DegToRad(0)) * Quat::RotateAxisAngle(axisZ, DegToRad(-90));
float3 eulerAnglesA = aQ.ToEulerXYZ();

Quat bQ = Quat::RotateAxisAngle(axisX, DegToRad(-180)) * Quat::RotateAxisAngle(axisY, DegToRad(180)) * Quat::RotateAxisAngle(axisZ, DegToRad(90));
float3 eulerAnglesB = bQ.ToEulerXYZ();

These both yield the same quaternion!

x   0.00000000  float
y   0.00000000  float
z   -0.70710677 float
w   0.70710677  float
2

There are 2 answers

0
Edward Doolittle On BEST ANSWER

The quaternions -q and q are different; however, the rotations represented by the two quaternions are identical. This phenomenon is usually described by saying quaternions provide a double cover of the rotation group SO(3). The algebra to see this is very simple: given a vector represented by quaternion p, and a rotation represented represented by a quaternion q, the rotation is qpq^{-1}. On the other hand, -qp(-q)^{-1} = -1qp(q)^{-1}(-1) = q(-1)p(-1)q^{-1} = qp(-1)^2q^{-1} = qpq^{-1}, the same rotation. Quaternions normally don't commute, so pq != qp for general quaternions, but scalars like -1 do commute with quaternions.

I believe ToEulerXYZ should be the same in both cases, which it appears to be.

2
P i On

From what I remember a quaternion can be considered as a rotation around an arbitrary axis.

And this can help to understand intuitively why there will always be two quaternions to represent a given rotation.

Rotating 90° around 0,0,1 is going to be the same as rotating 270° around 0,0, -1.

I.e. A quarter turn anticlockwise around 0,0,1 is identical to a quarter turn clockwise around 0,0, -1.

You can check this out by using your thumb as the axis of rotation and do the 90° rotation in the direction your fingers curl.