# Quaternions multiplication order (to rotate & unrotate)

Say, I have parent object rotation quaternion Qp & child object rotation (local - relative to parent) quternion Qch.

1) Which order should I multiply them to get child object world(total) rotation QW? QW = Qp * Qch or QW = Qch * Qp ? And what geometric interpretation of this order - same or reverse order of rotation execution?

2) And one more question: If I already have result total rotation of child object QW (calculated in proper way (see #1), I also know Qp & want to calculate Qch. Which order should I multiply Qp.Inverse & QW ?

3) And last - if we have situation #2, but opposite: QW & Qch are known, & Qp we need to get, what order of QW & Qch.Inverse multiplication should we use?

Thanks a lot!

• I think the order of multiplication would depend on whether you perform rotations by multiplying on the right or on the left. As for geometric interpretations, the order depends on the interpretation. Interpreting rotations relative to the object gives a reverse order compared to rotations relative to fixed spatial axes. -- In short, there's no one simple answer to the question; it depends on things you haven't told us. – David K Feb 1 '17 at 18:16
• @DavidK, could you please answer the questions, making assumptions where given information is not enough? I hope, it'll suffice to understand the gist & adopt answer to concrete situation by myself. – user1234567 Feb 2 '17 at 9:21

I'll assume the convention used in this earlier answer and this earlier answer about the use of quaternions for rotation, that is, for an initial object vector $v$ and total rotation quaternion $q_w$ you get the rotated object vector by multiplying $q_w v q_w^{-1}.$

## Part 1

Suppose you have a "parent" rotation with quaternion $q_p,$ and you want to combine this with a "child" rotation with quaternion $q_c$ that will rotate an object relative to the object's orientation after the "parent" rotation was performed.

Multiplication by quaternions in the conventional way transforms the world coordinates of whatever object you apply it to. In order to achieve the effect of first performing the parent rotation, then performing the child rotation relative to the rotated coordinate system that resulted from the "parent" rotation, you should apply the "child" rotation first in world coordinates.

Using the convention $q_w v q_w^{-1},$ whichever quaternion is on the right when we compute $q_w$ is the rotation that is performed first, so you want $q_w = q_p q_c.$ That way, when you compute $q_w v q_w^{-1},$ it is actually $$q_w v q_w^{-1} = (q_p q_c) v (q_p q_c)^{-1} = (q_p q_c) v (q_c^{-1} q_p^{-1}) = q_p (q_c v q_c^{-1}) q_p^{-1}.$$

## Part 2

If you have the total rotation quaternion $q_w$ and you also know the parent rotation quaternion $q_p,$ to recover the child rotation quaternion you can multiply on the left by $q_p^{-1},$ because then $$q_p^{-1} q_w = q_p^{-1} (q_p q_c) = (q_p^{-1} q_p) q_c = q_c.$$

## Part 3

If you have the total rotation quaternion $q_w$ and you also know the child rotation quaternion $q_c,$ to recover the parent rotation quaternion you can multiply on the right by $q_c^{-1},$ because then $$q_w q_c^{-1} = (q_p q_c) q_c^{-1} = q_p (q_c^{-1} q_c) = q_p.$$

## Checking the algorithm

After constructing a set of formulas like this, it's a good policy to verify that you put them together properly by taking some examples of "object" vectors and applying some examples of rotations to them.

A nice simple example is a parent rotation of $\frac\pi2$ ($90$ degrees) around the $z$ axis that takes points on the positive $x$ axis to points on the positive $y$ axis, and a child rotation of $\frac\pi2$ ($90$ degrees) around the $x$ axis that takes points on the positive $y$ axis to points on the positive $x$ axis. If you perform the parent rotation first, then perform the child rotation relative to the result of the parent rotation, the parent rotation will leave the $z$ axis where it was, but the child rotation will be about the $y$ axis in world coordinates (because that's the rotated position of the original $x$ axis), so it will take points on the positive $z$ axis to the positive $x$ axis. If you apply these rotations to a point on the positive $z$ axis and it ends up somewhere other than the positive $x$ axis, you will know you need to fix your implementation of the rotations somehow.

You can look at the rotated positions of some vectors after any one of the rotations in question (the parent rotation alone, the child rotation alone, or the combined parent-child rotation) to verify that each one is doing what you expect.

• Thank you, @DavidK, a lot! I deal with quaternions in 3D-engines (like Unreal or Unity). & here their high-level implementation is slightly different. But! I grateful to you for your answer - I found my answers similarly to yours. I'll make it as separate answer just for concise fast help to those, who like me, encounter quaternions in 3D-engines. – user1234567 Feb 6 '17 at 8:24

1): QW == Qp * Qch It means we apply Qch 1st, & Qp then. So order of rotations applying is always from right to left.

2): Qch == Qp.Inversed * QW So we apply QW 1st, then unrotate it by Qp back.

3): Qp == QW * Qch.Inversed So we apply inverse Qch rotation. Then total QW. It yield Qp.

Note: #2 & #3 work so, provided that QW is obtained by #1 formula.

Ps: This my answer is just concise translation to 3D-engines implementation the @DavidK's answer. See it for more fundamental understanding of, what is happened here under the hood.

• As a confirmation to this: I've found a comment in Unreal Engine: "Order matters when composing quaternions: C = A * B will yield a quaternion C that logically first applies B then A to any subsequent transformation (right first, then left)." – user1234567 Feb 6 '17 at 8:44