Search code examples
unity-game-enginequaternions

Rotate Quaternion by an offset?


I'm trying to add an offset to the rotation of a quaternion, according to the Unity documentation multiplying two Quaternions is the same as doing both rotations in sequence.

This is what I'm trying now:

If the offset Quaternion is (0, 0, 0, 0) multiplying the first quaternion by it, shouldn't have any effect. However in my tests multiplying any Quaternion by (0, 0, 0, 0) results in (0, 0, 0, 0), so this is not working correctly.

    Quaternion q = new Quaternion (-0.7f, 0f, 0f, -0.7f);
    Quaternion rotationZero = new Quaternion (0f, 0f, 0f, 0f); // offset

    Quaternion result = q * rotationZero;

    print (result); // (0.0, 0.0, 0.0, 0.0)

Solution

  • Here the issue is not is C# or in Unity, the values you are using causes the output values to be (0.0, 0.0, 0.0, 0.0). It can be mathematically explained as below.

    The quaternions have the form of

    q = q0 + iq1 + jq2 + kq3 and r = r0 + ir1 + jr2 + kr3

    The quaternion product has the form of

    t= q×r = t0 + it1 + jt2 + kt3, where

    t0 = (r0q0−r1q1−r2q2−r3q3) t1 = (r0q1+r1q0−r2q3+r3q2) t2 = (r0q2+r1q3+r2q0−r3q1) t3 = (r0q3−r1q2+r2q1+r3q0)