Unexpected Quaternion.toRotationMatrix behavior

Hi there,
Quaternion.toRotationMatrix sounds like it returns the rotation matrix for a given quaternion. So I called it with something like: const rot = this.rotation.toRotationMatrix().

This breaks because the method actually expects a matrix as an argument. The function then writes the result into the given matrix (why isn’t it called ToRef then?) and returns the updated matrix as well.

The doc says, that it “returns the current unchanged quaternion”, what seems to be wrong.

Calling it like this works, but looks weird, IMHO:

const rot = this.rotation.toRotationMatrix(BABYLON.Matrix.Zero())

Maybe I’m just using it incorrectly. :thinking:

the return on code doc was wrong, fixed in: fix code doc torotationmatrix by carolhmj · Pull Request #14338 · BabylonJS/Babylon.js (github.com)

It might be a little unexpected to have to pass a matrix to the function, but there’s a reason for that as we want to avoid creating matrices when possible in the function to avoid increasing memory :slight_smile: in many cases, the function is used with a “cached” matrix that is reused between calls.


You could also do what is done here:

    public decomposeToTransformNode(node: TransformNode): boolean {
        node.rotationQuaternion = node.rotationQuaternion || new Quaternion();
        return this.decompose(node.scaling, node.rotationQuaternion, node.position);

If no matrix was given, create a new one. The code could look something like this:

    public toRotationMatrix<T extends Matrix>(result: T): T {
        result = result || Matrix.Zero();
        Matrix.FromQuaternionToRef(this, result);
        return result;