Search code examples
javascriptmaththree.jsquaternions

calculating delta Quaternion with threejs


I am trying to calculate the delta quaternion of the startOrientation and the endOrientation. This fiddle demonstrates my problem.

First, I calculate the delta quaternion and then it is multiplied with the endOrientation to get the startOrientation but its not working. The rotation of the cube is always wrong.

Uncomment line 37 to see the correct rotation of the cube. Any ideas how to compute the delta?


Solution

  • Let's examine what you (seemingly want to) compute. You start with

    gyro = start^(-1)*end
    

    and then set the scene rotation to

    scene = end*gyro = end*start^(-1)*end
    

    As you can see, this product is far from being the desired start quaternion. From the expectation that

    start == scene = end*gyro
    

    you would need to compute

    gyro = end^(-1)*start
    

    that is, exchange end and start in your current computation of gyro.


    Also explore which of the quaternion methods are self-modifying (they all are if the result is a quaternion, with the exception of .clone() naturally).

    The .inverse() method equal to .conjugate().normalize() falls into that class so that

    var gyroTrackingDelta=endOrientation.inverse();
    

    first inverts endOrientation and then shares the reference with gyroTrackingDelta introducing strange side effects in the further computation. A working variant that should avoid unwanted self-modification is

    var gyroTrackingDelta=endOrientation.clone().inverse();
    gyroTrackingDelta.multiply(startOrientation);
    
    scene.quaternion.copy(endOrientation).multiply(gyroTrackingDelta);