datenwolf / linmath.h

a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions
Do What The F*ck You Want To Public License
922 stars 134 forks source link

mat4x4_mul_vec4 Correction #13

Closed ghost closed 10 years ago

ghost commented 10 years ago

I believe Line 184: r[j] += M[i][j] * v[i];

Should be: r[j] += M[j][i] * v[i];

datenwolf commented 10 years ago

Nope, the multiplication and the indices are correct. v is a column vector, M is column major (i.e. the first index designates the matrix column). When multiplying a column vector with a matrix the resulting vector is achieved by transposing the column vector into a row vector and by taking the inner product with each of the matrices rows.

If you ran into an issue using that function, then this is because you're having your matrix accidently in row major ordering. Note that when writing out a matrix in C, be it a simple or a multidimensional array, the columns are what on the screen looks like rows and vice versa. Naively one may suggest "hey make it look right in the code", but that's the wrong approach, because it has to be correct mathematically. And mathematically we need to access matrix column vectors more often than row vectors, hence the elements in a vector are to be consecutive in memory which makes them look "transposed" in the source code.