Matrix multiplication and inversion

Hi all,

I was playing with my scene graph, and I faced something really strange.
Here is my PG:, 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


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

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

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


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!