Mat4 multiplication done in reverse order
Opened this issue · 3 comments
Test case:
package.path = package.path .. ';?.lua;?/init.lua'
local cpml = require 'cpml'
local a = cpml.mat4()
a[5] = 2 -- row 1 column 2
local b = cpml.mat4()
b[2] = 3 -- row 2 column 1
local c = a * b
for i = 1, 4 do
print(c[i], c[i + 4], c[i + 8], c[i + 12])
end
Expected result:
7 2 0 0
3 1 0 0
0 0 1 0
0 0 0 1
Actual result:
1 2 0 0
3 7 0 0
0 0 1 0
0 0 0 1
Multiplying b*a produces the expected result.
It's obvious from the source. For example, the second row, first column (index 2) in the product a*b
should have as a first element the sum of the products of each element in the second row of the first matrix, times each element in the first column in the second matrix. Since the order is column-major, the second row of the first matrix is a[2],a[6],a[10],a[14]
and the first column of the second matrix is b[1],b[2],b[3],b[4]
, yet the code sets it to:
tm4[2] = a[1] * b[2] + a[2] * b[6] + a[3] * b[10] + a[4] * b[14]
and so on, meaning the order is swapped.
Edit: Note that the product, as coded, would have worked in row-major order. Possibly related to #32.
I am absolutey for fixing this ASAP. In general it's maybe sometimes fine to leave erroneous behaviour for backwards compatibility, but this is a maths library.
Me (as I just did) and plenty of other people will start using it and spend at least an hour guaranteed figuring out why their stuff doesn't work. Plase just make a tag, so other people can find the last version with the wrong behaviour easily and let master be mathematically correct.