mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 09:13:53 +01:00
Vectors: Consistent argument naming
This commit is contained in:
parent
f0f48c0fb4
commit
7507209f29
54
vector.lua
54
vector.lua
@ -47,37 +47,37 @@ function to_minetest(v)
|
|||||||
return mt_vector.new(unpack(v))
|
return mt_vector.new(unpack(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
function equals(v, other_v)
|
function equals(v, w)
|
||||||
for k, v in pairs(v) do
|
for k, v in pairs(v) do
|
||||||
if v ~= other_v[k] then return false end
|
if v ~= w[k] then return false end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
metatable.__eq = equals
|
metatable.__eq = equals
|
||||||
|
|
||||||
function less_than(v, other_v)
|
function less_than(v, w)
|
||||||
for k, v in pairs(v) do
|
for k, v in pairs(v) do
|
||||||
if v >= other_v[k] then return false end
|
if v >= w[k] then return false end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
metatable.__lt = less_than
|
metatable.__lt = less_than
|
||||||
|
|
||||||
function less_or_equal(v, other_v)
|
function less_or_equal(v, w)
|
||||||
for k, v in pairs(v) do
|
for k, v in pairs(v) do
|
||||||
if v > other_v[k] then return false end
|
if v > w[k] then return false end
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
metatable.__le = less_or_equal
|
metatable.__le = less_or_equal
|
||||||
|
|
||||||
function combine(v, other_v, f)
|
function combine(v, w, f)
|
||||||
local new_vector = {}
|
local new_vector = {}
|
||||||
for key, value in pairs(v) do
|
for key, value in pairs(v) do
|
||||||
new_vector[key] = f(value, other_v[key])
|
new_vector[key] = f(value, w[key])
|
||||||
end
|
end
|
||||||
return new(new_vector)
|
return new(new_vector)
|
||||||
end
|
end
|
||||||
@ -91,8 +91,8 @@ function apply(v, f, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function combinator(f)
|
function combinator(f)
|
||||||
return function(v, other_v)
|
return function(v, w)
|
||||||
return combine(v, other_v, f)
|
return combine(v, w, f)
|
||||||
end, function(v, ...)
|
end, function(v, ...)
|
||||||
return apply(v, f, ...)
|
return apply(v, f, ...)
|
||||||
end
|
end
|
||||||
@ -104,11 +104,11 @@ function invert(v)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
add, add_scalar = combinator(function(a, b) return a + b end)
|
add, add_scalar = combinator(function(v, w) return v + w end)
|
||||||
subtract, subtract_scalar = combinator(function(a, b) return a - b end)
|
subtract, subtract_scalar = combinator(function(v, w) return v - w end)
|
||||||
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
multiply, multiply_scalar = combinator(function(v, w) return v * w end)
|
||||||
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
divide, divide_scalar = combinator(function(v, w) return v / w end)
|
||||||
pow, pow_scalar = combinator(function(a, b) return a ^ b end)
|
pow, pow_scalar = combinator(function(v, w) return v ^ w end)
|
||||||
|
|
||||||
metatable.__add = add
|
metatable.__add = add
|
||||||
metatable.__unm = invert
|
metatable.__unm = invert
|
||||||
@ -118,8 +118,8 @@ metatable.__div = divide
|
|||||||
|
|
||||||
--+ linear interpolation
|
--+ linear interpolation
|
||||||
--: ratio number from 0 (all the first vector) to 1 (all the second vector)
|
--: ratio number from 0 (all the first vector) to 1 (all the second vector)
|
||||||
function interpolate(v, other_v, ratio)
|
function interpolate(v, w, ratio)
|
||||||
return add(multiply(v, 1 - ratio), multiply(other_v, ratio))
|
return add(multiply(v, 1 - ratio), multiply(w, ratio))
|
||||||
end
|
end
|
||||||
|
|
||||||
function norm(v)
|
function norm(v)
|
||||||
@ -135,10 +135,10 @@ function length(v)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Minor code duplication for the sake of performance
|
-- Minor code duplication for the sake of performance
|
||||||
function distance(v, other_v)
|
function distance(v, w)
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for key, value in pairs(v) do
|
for key, value in pairs(v) do
|
||||||
sum = sum + (value - other_v[key]) ^ 2
|
sum = sum + (value - w[key]) ^ 2
|
||||||
end
|
end
|
||||||
return math.sqrt(sum)
|
return math.sqrt(sum)
|
||||||
end
|
end
|
||||||
@ -159,27 +159,27 @@ function clamp(v, min, max)
|
|||||||
return apply(apply(v, math.max, min), math.min, max)
|
return apply(apply(v, math.max, min), math.min, max)
|
||||||
end
|
end
|
||||||
|
|
||||||
function cross3(v, other_v)
|
function cross3(v, w)
|
||||||
return new{
|
return new{
|
||||||
v[2] * other_v[3] - v[3] * other_v[2],
|
v[2] * w[3] - v[3] * w[2],
|
||||||
v[3] * other_v[1] - v[1] * other_v[3],
|
v[3] * w[1] - v[1] * w[3],
|
||||||
v[1] * other_v[2] - v[2] * other_v[1]
|
v[1] * w[2] - v[2] * w[1]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function dot(v, other_v)
|
function dot(v, w)
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for i, c in pairs(v) do
|
for i, c in pairs(v) do
|
||||||
sum = sum + c * other_v[i]
|
sum = sum + c * w[i]
|
||||||
end
|
end
|
||||||
return sum
|
return sum
|
||||||
end
|
end
|
||||||
|
|
||||||
--+ Angle between two vectors
|
--+ Angle between two vectors
|
||||||
--> Signed angle in radians
|
--> Signed angle in radians
|
||||||
function angle(v, other_v)
|
function angle(v, w)
|
||||||
-- Based on dot(v, w) = |v| * |w| * cos(x)
|
-- Based on dot(v, w) = |v| * |w| * cos(x)
|
||||||
return math.acos(dot(v, other_v) / length(v) / length(other_v))
|
return math.acos(dot(v, w) / length(v) / length(w))
|
||||||
end
|
end
|
||||||
|
|
||||||
function box_box_collision(diff, box, other_box)
|
function box_box_collision(diff, box, other_box)
|
||||||
|
Loading…
Reference in New Issue
Block a user