How to get euler angles from a rotation matrix?

Hi there!

My use case is a little difficult to explain, but is there an opposite of BABYLON.Matrix.RotationYawPitchRoll such that given a matrix, I can derive its yaw pitch and roll?

I tried to follow an answer from SO but with no success.

            const newRotation = BABYLON.Matrix.RotationYawPitchRoll(mesh.rotation.y, mesh.rotation.x, mesh.rotation.z);
            const rotationEuler = new BABYLON.Vector3(
                Math.atan2(newRotation.getRow(2).y, newRotation.getRow(2).z),
                Math.atan2(-newRotation.getRow(2).x, Math.sqrt(Math.pow(newRotation.getRow(2).y, 2) + Math.pow(newRotation.getRow(2).z, 2))),
                Math.atan2(newRotation.getRow(1).x, newRotation.getRow(0).x)
            );

Unfortunately, rotationEuler is never equal to mesh.rotation…

Hi @blindinghues, maybe this could help?

const scale = new BABYLON.Vector3();
const rotation = new BABYLON.Quaternion();
const position = new BABYLON.Vector3();

// variable matrix is matrix you're trying to derive yaw, pitch, and roll from
matrix.decompose(scale, rotation, position);

// matrix's rotation should now be stored in the variable rotation

// convert quaternion to Euler angles
rotation.toEulerAngles();
2 Likes

This totally worked, saved me further hours of struggling, much appreciated!

2 Likes