toji / gl-matrix

Javascript Matrix and Vector library for High Performance WebGL apps
glmatrix.net
MIT License
5.36k stars 720 forks source link

Inconsistent order or arguments #468

Open PauliusLiekis opened 4 months ago

PauliusLiekis commented 4 months ago

I'm confused about the order of arguments, as it doesn't follow mathematical logic. This code specifically:

mat4.multiply(M_, M1, M2);
vec3.transformMat4(p_, p, M_);

if I inline this code I end up with this order: p_ = p * M1 * M2, which would imply that p is first multiplied by M1 and then M2, but that's not what happens. The result M1 * M2 * p, i.e. it's multiplied by M2 and then by M1.

Wouldn't this order vec3.transformMat4(p_, M_, p); be less confusing? Is it just an oversight or am I missing something?


For example, writing this in CoreGraphics would look like this:

p_ = p.applying(M1.concatenating(M2))

and mathematically equivalent to p_ = p * M1 * M2, i.e. the consistent order, although a bit unconventional use of row-vectors for someone coming from a math background, but at least they are super consistent about that when it comes to the order of arguments in API or any explanations in the documentation.

PauliusLiekis commented 4 months ago

BTW.: having a bit of math in the documentation might clear up this:

(static) transformMat4(out, a, m) → {vec4}
Transforms the vec4 with a mat4, i.e. "m * a".