galacean / engine

A typescript interactive engine, support 2D, 3D, animation, physics, built on WebGL and glTF.
https://galacean.antgroup.com/
MIT License
4.04k stars 289 forks source link

Fix quaternion to euler bug #2073

Closed cptbtptpbcptdtptp closed 2 months ago

cptbtptpbcptdtptp commented 2 months ago

Please check if the PR fulfills these requirements

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

The derivation process:

  1. Use YXZ Order and expand matrix elements:https://github.com/mrdoob/three.js/blob/ce756225c802ff24cc27a1f615cc9bf420023ae8/src/math/Euler.js#L134
  2. Convert normalized version to non-normalized version :http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
cptbtptpbcptdtptp commented 2 months ago

Referenced ThreeJS processing of converting Quaternion to Euler: https://github.com/mrdoob/three.js/blob/ce756225c802ff24cc27a1f615cc9bf420023ae8/src/math/Euler.js#L200

                this._z = Math.asin( clamp( m21, - 1, 1 ) );

                if ( Math.abs( m21 ) < 0.9999999 ) {

                    this._x = Math.atan2( - m23, m22 );
                    this._y = Math.atan2( - m31, m11 );

                } else {

                    this._x = 0;
                    this._y = Math.atan2( m13, m33 );

                }

Combined with non-normalized Quaternion:

http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/