# Unexpected Quaternion.toRotationMatrix behavior

**URL:** <https://forum.babylonjs.com/t/unexpected-quaternion-torotationmatrix-behavior/44361>\
**Category:** Bugs\
**Created:** [September 22, 2023, 12:10pm UTC](https://forum.babylonjs.com/t/unexpected-quaternion-torotationmatrix-behavior/44361 "2023-09-22T12:10:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7om](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/7om/32/11158_2.png) [@7om](https://forum.babylonjs.com/u/7om)\
**Post date:** [September 22, 2023, 12:10pm UTC](https://forum.babylonjs.com/t/unexpected-quaternion-torotationmatrix-behavior/44361/1 "2023-09-22T12:10:49Z")

</div>

Hi there,  
[Quaternion.toRotationMatrix](https://github.com/BabylonJS/Babylon.js/blob/db1c9762f69b4ade3d1ee26ff1b80bbce0f1d7a2/packages/dev/core/src/Maths/math.vector.ts#L4382) 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:

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

```

Maybe I’m just using it incorrectly. 🤔

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 22, 2023, 12:28pm UTC](https://forum.babylonjs.com/t/unexpected-quaternion-torotationmatrix-behavior/44361/2 "2023-09-22T12:28:06Z")

</div>

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

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 🙂 in many cases, the function is used with a “cached” matrix that is reused between calls.

---

<div class="post-metadata">

**Author:** ![7om](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/7om/32/11158_2.png) [@7om](https://forum.babylonjs.com/u/7om)\
**Post date:** [September 23, 2023, 8:10am UTC](https://forum.babylonjs.com/t/unexpected-quaternion-torotationmatrix-behavior/44361/3 "2023-09-23T08:10:46Z")

</div>

You could also do what is done [here](https://github.com/BabylonJS/Babylon.js/blob/db1c9762f69b4ade3d1ee26ff1b80bbce0f1d7a2/packages/dev/core/src/Maths/math.vector.ts#L5723):

```js
    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:

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

```
