Source code incorrect comment on rotation order, or my misunderstanding?

packages/dev/core/src/Maths/math.vector.ts

    public static RotationYawPitchRollToRef<T extends Quaternion>(yaw: number, pitch: number, roll: number, result: T): T {
        // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
        const halfRoll = roll * 0.5;
        const halfPitch = pitch * 0.5;
        const halfYaw = yaw * 0.5;

        const sinRoll = Math.sin(halfRoll);
        const cosRoll = Math.cos(halfRoll);
        const sinPitch = Math.sin(halfPitch);
        const cosPitch = Math.cos(halfPitch);
        const sinYaw = Math.sin(halfYaw);
        const cosYaw = Math.cos(halfYaw);

        result._x = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll;
        result._y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll;
        result._z = cosYaw * cosPitch * sinRoll - sinYaw * sinPitch * cosRoll;
        result._w = cosYaw * cosPitch * cosRoll + sinYaw * sinPitch * sinRoll;
        result._isDirty = true;
        return result;
    }

Hi, in the source code I see this comment that claims z-y-x orientation but I think the math here indicates YXZ (which is what the docs say as well). Is the comment wrong or am I missing something?

q = (x,y,z,w) = ( cy*sx*cz + sy*cx*sz, sy*cx*cz - cy*sx*sz, cy*cx*sz - sy*sx*cz, cy*cx*cz + sy*sx*sz ) where sy=sin(yaw/2), cy=cos(yaw/2), sx=sin(pitch/2), cx=cos(pitch/2), sz=sin(roll/2), cz=cos(roll/2). extrinsic YXZ, intrinsic ZXY, not zyx

I think you are correct, lets double check either @Deltakosh ?

Yes, I also think you’re right….

If this is the case, though, then I wonder if our docs have got some things swapped as well?

If the intrinsic sequence of rotations is z - x' - y'' for the following two code snippets,

mesh.rotate(BABYLON.Axis.Y, yaw, BABYLON.Space.LOCAL);
mesh.rotate(BABYLON.Axis.X, pitch, BABYLON.Space.LOCAL);
mesh.rotate(BABYLON.Axis.Z, roll, BABYLON.Space.LOCAL);
const yprQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw, pitch, roll);

whereby roll is actually applied first, followed by pitch, and finally yaw,

then we should switch YXZ Local Space => ZXY Local Space and ZXY World Axes => YXZ World Axes, etc. throughout the docs. Right?

Someone please correct me if I made a wrong step somewhere :smiley:

This is totally correct. We historically work with YXZ :smiley:

I agree with YXZ, but the source code comment claims z-y-x

i’ll fix that comment :wink:

1 Like

done and merged :slight_smile:

1 Like