Rotate a Vector3

I like using a matrix method sometimes too:

const direction = new Vector3(0, 0, 1);
const rotation = Quaternion.FromEulerAngles(0, Math.PI * 0.5, 0);
const rotMat = Matrix.Zero();
rotation.toRotationMatrix(rotMat);
const newDirection = Vector3.TransformCoordinates(direction, rotMat);

If you are going to be doing a lot of rotations you dont have to make a new matrix Instance each time, you can cache it on your own script or use one of BJS temporary ones that they have already cached for you.

rotation.toRotationMatrix(BABYLON.TmpVectors.Matrix);
const newDirection = Vector3.TransformCoordinates(direction, BABYLON.TmpVectors.Matrix);
2 Likes