forked from Mirrorlandia_minetest/minetest
Remove vector metatable setting
This not only makes the vector functions faster, but also makes them more consistent with other functions.
This commit is contained in:
parent
b3591019ad
commit
12504a18ec
@ -2,22 +2,12 @@
|
|||||||
vector = {}
|
vector = {}
|
||||||
|
|
||||||
function vector.new(a, b, c)
|
function vector.new(a, b, c)
|
||||||
v = {x=0, y=0, z=0}
|
|
||||||
if type(a) == "table" then
|
if type(a) == "table" then
|
||||||
v = {x=a.x, y=a.y, z=a.z}
|
return {x=a.x, y=a.y, z=a.z}
|
||||||
elseif a and b and c then
|
elseif a and b and c then
|
||||||
v = {x=a, y=b, z=c}
|
return {x=a, y=b, z=c}
|
||||||
end
|
end
|
||||||
setmetatable(v, {
|
return {x=0, y=0, z=0}
|
||||||
__add = vector.add,
|
|
||||||
__sub = vector.subtract,
|
|
||||||
__mul = vector.multiply,
|
|
||||||
__div = vector.divide,
|
|
||||||
__umn = function(v) return vector.multiply(v, -1) end,
|
|
||||||
__len = vector.length,
|
|
||||||
__eq = vector.equals,
|
|
||||||
})
|
|
||||||
return v
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector.equals(a, b)
|
function vector.equals(a, b)
|
||||||
@ -85,57 +75,49 @@ end
|
|||||||
|
|
||||||
function vector.add(a, b)
|
function vector.add(a, b)
|
||||||
if type(b) == "table" then
|
if type(b) == "table" then
|
||||||
return vector.new(
|
return {x = a.x + b.x,
|
||||||
a.x + b.x,
|
y = a.y + b.y,
|
||||||
a.y + b.y,
|
z = a.z + b.z}
|
||||||
a.z + b.z)
|
|
||||||
else
|
else
|
||||||
return vector.new(
|
return {x = a.x + b,
|
||||||
a.x + b,
|
y = a.y + b,
|
||||||
a.y + b,
|
z = a.z + b}
|
||||||
a.z + b)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector.subtract(a, b)
|
function vector.subtract(a, b)
|
||||||
if type(b) == "table" then
|
if type(b) == "table" then
|
||||||
return vector.new(
|
return {x = a.x - b.x,
|
||||||
a.x - b.x,
|
y = a.y - b.y,
|
||||||
a.y - b.y,
|
z = a.z - b.z}
|
||||||
a.z - b.z)
|
|
||||||
else
|
else
|
||||||
return vector.new(
|
return {x = a.x - b,
|
||||||
a.x - b,
|
y = a.y - b,
|
||||||
a.y - b,
|
z = a.z - b}
|
||||||
a.z - b)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector.multiply(a, b)
|
function vector.multiply(a, b)
|
||||||
if type(b) == "table" then
|
if type(b) == "table" then
|
||||||
return vector.new(
|
return {x = a.x * b.x,
|
||||||
a.x * b.x,
|
y = a.y * b.y,
|
||||||
a.y * b.y,
|
z = a.z * b.z}
|
||||||
a.z * b.z)
|
|
||||||
else
|
else
|
||||||
return vector.new(
|
return {x = a.x * b,
|
||||||
a.x * b,
|
y = a.y * b,
|
||||||
a.y * b,
|
z = a.z * b}
|
||||||
a.z * b)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector.divide(a, b)
|
function vector.divide(a, b)
|
||||||
if type(b) == "table" then
|
if type(b) == "table" then
|
||||||
return vector.new(
|
return {x = a.x / b.x,
|
||||||
a.x / b.x,
|
y = a.y / b.y,
|
||||||
a.y / b.y,
|
z = a.z / b.z}
|
||||||
a.z / b.z)
|
|
||||||
else
|
else
|
||||||
return vector.new(
|
return {x = a.x / b,
|
||||||
a.x / b,
|
y = a.y / b,
|
||||||
a.y / b,
|
z = a.z / b}
|
||||||
a.z / b)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1037,22 +1037,12 @@ vector.length(v) -> number
|
|||||||
vector.normalize(v) -> vector
|
vector.normalize(v) -> vector
|
||||||
vector.round(v) -> vector
|
vector.round(v) -> vector
|
||||||
vector.equal(v1, v2) -> bool
|
vector.equal(v1, v2) -> bool
|
||||||
|
For the folowing x can be either a vector or a number.
|
||||||
vector.add(v, x) -> vector
|
vector.add(v, x) -> vector
|
||||||
^ x can be annother vector or a number
|
|
||||||
vector.subtract(v, x) -> vector
|
vector.subtract(v, x) -> vector
|
||||||
vector.multiply(v, x) -> vector
|
vector.multiply(v, x) -> vector
|
||||||
vector.divide(v, x) -> vector
|
vector.divide(v, x) -> vector
|
||||||
|
|
||||||
You can also use Lua operators on vectors.
|
|
||||||
For example:
|
|
||||||
v1 = vector.new()
|
|
||||||
v1 = v1 + 5
|
|
||||||
v2 = vector.new(v1)
|
|
||||||
v1 = v1 * v2
|
|
||||||
if v1 == v2 then
|
|
||||||
error("Math broke")
|
|
||||||
end
|
|
||||||
|
|
||||||
Helper functions
|
Helper functions
|
||||||
-----------------
|
-----------------
|
||||||
dump2(obj, name="_", dumped={})
|
dump2(obj, name="_", dumped={})
|
||||||
|
Loading…
Reference in New Issue
Block a user