Matrix.multiply does premultiplication

When reading the documentation you would think a.multiply(b) will be the result of multiplying a by b, but you get b x a. Wouldn’t it make more sense to call the method something like premultiply (like in Three.js) or maybe have both methods?

Is there a difference?
a x b
b x a

Surely the result should be the same at least? :slight_smile:

they are not, matrix order is important. We have this notation which mean a multiplied by b (or b applied to A) hence b.a

for instance when you say worldviewmatrix you mean (vector x world x view) which in the shader is written view * world * position: Babylon.js/default.vertex.fx at master · BabylonJS/Babylon.js · GitHub

maybe we can improve the doc?

1 Like

You say that as if it makes sense. :smiley: To me it seems insane to have a.multiply(b) mean the complete opposite. It took me hours to track down a bug in code ported from Three.js, matrix multiplication is the last place you look. I had to look at the implementation of multiplyToArray to figure out why it wasn’t working.

Improving the documentation would be a good start though. I prefer having clearly named methods though, but I guess that’s not in the cards as it would break to much existing code silently? One option could be deprecating multiply and introducing two new methods preMultiply (or transformMatrix) and postMultiply (or multiplyMatrix) that are very explicit.

I have to say that the Babylon.js API is often confusing and inconsistent (why is cross a method and Dot a static method, why do some methods return the thing you’re passing in and some don’t, why does creating a mesh without passing in a scene automatically add it to the most recently created scene (this resulted in another bug that took some time to figure out),…), this was the most egregious case. It’s a very comprehensive library though and everything I’ve tried works perfectly, so kudos for that.

I recently ported some 3js style code, seemed a little strange but made just as much sense in the end. I’m not much for math though.