Matrix multiplication and inversion

Hi all,

I was playing with my scene graph, and I faced something really strange.
Here is my PG: https://playground.babylonjs.com/#JJYWBI#1, check the F12 console.

Whatever the matrix A, I can compute its inverse with invA = A.invert(). The resulting matrix is correct, as MATLAB confirms :slight_smile:
Yet, I then try invA.multiply(A) and A.multiply(invA), where I expect the identity matrix. But the result is a complete mess, far from identity matrix. I should be missing something important regarding BabylonJS and the inner working of its matrices, could you help me figure it out?

Thanks a lot!

PS: You can figure out that it’s not even element-wise multiplication

Hi,

I think matrix multiplication is not commutative in general. So invA.multiply(A) and A.multiply(invA) do not give the same result. I think you can also verify that in MATLAB. :smiley:

A and invA are communtative

1 Like

It does work as in this example https://playground.babylonjs.com/#JJYWBI#2

The issue is that the matrix.invert() actually inverts the matrix in place, ie it changes the input matrix to its inverse. Matrix | Babylon.js Documentation

You need to use invertToRef as in https://playground.babylonjs.com/#JJYWBI#3

I was also expecting invert to create a new matrix so took a while to spot.

3 Likes

That is the right answer!! Thank you so much for digging deeper than I did :slight_smile:

Obviously, the naming of the invert function is a bit misleading, but I’ll deal with it!