# Changing the rotation matrix

From an external application I get the rotation matrix and convert it to angles. Then I change the angles.
How do I get a rotation matrix with changed angles?

rot_matrix = new BABYLON.Matrix();
rot_matrix = BABYLON.Matrix.FromValues(0.99966622845961317, 0.023500156383954435, 0.010731930267321466, 0,
0.023739394969467975, -0.99945958249453704, -0.022737284057907529, 0,
0.010191800813230373, 0.022984464530983913, -0.99968387082437604, 0);
sphere.rotation.x = 0;
sphere.rotation.z = 0;

Thanks for the help!

There are a few ways to do that using babylonâ€™s math functions.

The first is to use quaternions, as quaternions have `toRotationMatrix` function. That would be the simplest (i guess), though it depend on your implementation. What you can always do is get the rotation from the objectâ€™s world matrix. The matrixâ€™s class has the function `getRotationMatrix` (and `...ToRef` as well) that can be called on the objectâ€™s world matrix (after computing it, if you just changed the rotation).

If you want to show a playground of how you use the object, it will be simpler to provide some code to show you how to do it.

1 Like

Iâ€™m trying to get a matrix in the same format as in the example (a set of digital values)

A matrix is a matrix. We have 4x4 matrix support, which means you get 16 values. But just like the matrix you provide, the last values can be â€śignoredâ€ť, since, in a rotation-only matrix, they are not really used.

A playground would be helpful if you want some code examples

toRotationMatrix is what I needed, thanks!
I havenâ€™t found a way to convert the current matrix, but it is possible to create a new one with the angles calculated from the current matrix.

newQuaternion = new BABYLON.Quaternion.RotationYawPitchRoll(sphere.rotation.x sphere.rotation.y, sphere.rotation.z);
newMatrix = newQuaternion .toRotationMatrix(BABYLON.Matrix.Zero());

1 Like