Rotations don't seem to be reversible

If I rotate a vector then rotate it back again I don’t seem to to get back to the same value.

I extracted a minimal reproduction of this issue in this PG.

Note that this PG just outputs to the console log, so you need to open the dev tools to see the output. The code transforms a vector to spherical coordinates and back again, but the final value is not the same as the original value.

The reason that I think the vector rotation is the issue, is because if you swap these two lines:

const forwardQuaternion = BABYLON.Quaternion.FromEulerAngles(-1.445, 0, Math.PI / 2);
const reverseQuaternion = BABYLON.Quaternion.FromEulerAngles(1.445, 0, -Math.PI / 2);

with these two lines:

const forwardQuaternion = BABYLON.Quaternion.FromEulerAngles(0, 0, 0);
const reverseQuaternion = BABYLON.Quaternion.FromEulerAngles(0, 0, 0);

then the conversion to and from spherical coordinates round trips correctly and the final values match the original ones.

I am not listing this as a bug, just trying to understand why this doesn’t work the way I expected, and trying to find a solution to this problem.

Thanks.

Well, I solved the problem even though I don’t understand what the issue was.
See this new version of the PG for the solution.

1 Like

The inverse quaternion you want is not the quaternion built with the opposite euler angles because of the order the rotations are applied: x, y then z.

When you build a quaternion with angles -x, -y and -z, the rotations are still applied in the order -x, -y then -z whereas you would want the order to be -z, -y and -x to get the true inverse. So, taking the inverse quaternion is really what you want to do because q*inverse(q)=Identity

1 Like

Thanks for the explanation. That makes sense.

1 Like