Add matrix application to vectors

This commit is contained in:
Lars Mueller 2023-08-26 23:50:08 +02:00
parent e064873012
commit 115f3c2a31

@ -33,8 +33,6 @@ function mat4.translation(vec)
} }
end end
-- See https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation -- See https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
function mat4.rotation(unit_quat) function mat4.rotation(unit_quat)
assert(#unit_quat == 4) assert(#unit_quat == 4)
@ -58,7 +56,22 @@ function mat4.scale(vec)
} }
end end
-- Apply `self` to a 4d modlib vector `vec`
function mat4:apply(vec)
assert(#vec == 4)
local res = {}
for i = 1, 4 do
local sum = 0
for j = 1, 4 do
sum = sum + self[i][j] * vec[j]
end
res[i] = sum
end
return vec.new(res)
end
-- Multiplication: First apply other, then self -- Multiplication: First apply other, then self
--> Matrix product `self * other`
function mat4:multiply(other) function mat4:multiply(other)
local res = {} local res = {}
for i = 1, 4 do for i = 1, 4 do
@ -164,4 +177,4 @@ do
end end
end end
return mat4 return mat4