- Home /
Quaternion inverses
Let's say I have a GameObject A
with a child B
. B
has a local position B.localPosition
and local rotation B.localRotation
. Now let's say I want to modify the forward axis of B
such that it gets rotated by some new Quaternion c
so that it points in rotation d
.
To achieve this, I want to modify the rotation of A
Instead of directly modifying the rotation of B
. How would I calculate the necessary rotation to set to A
to achieve this?
My guess would be: (A.rotation * B.localRotation) * c = d
. I know B.localRotation
, c
, and d
. However, I'm not sure how to calculate A.rotation
given the non-commutative property and ordering of quaternion multiplication.
Answer by Bunny83 · Sep 18, 2021 at 09:47 PM
Well, quaternions are always relative rotations, always. When we used them as "absolute" rotations, it just means it relatively rotates from a given initial rotation. A.rotation * B.localRotation
is the same as B.rotation
since B.localRotation is a child of A. So if your "d" is a worldspace orientation, all you need is to calculate the difference between B.rotation and "d". This difference can simply be applied to A.rotation
Something like this:
var q = d * Quaternion.Inverse(B.rotation);
Here q is the relative worldspace rotation that is required to rotate B from its current worldspace orientation to the new one. This relative rotation can be applied to A to achieve the same effect, since A and B are part of the same kinematic chain. So any relative rotation change that is applied to A will also be applied to B.
So doing:
A.rotation = q * A.rotation;
Will orient A such that B.rotation == d.
Just want to say that you are a life saver! You don't even know how much frustration you saved me! Thank you!!!