Skip to content

Commit ae8ff4b

Browse files
ShadowNinjakwolekr
authored andcommittedJul 7, 2013
Add vector helpers
1 parent d22baa8 commit ae8ff4b

File tree

4 files changed

+192
-15
lines changed

4 files changed

+192
-15
lines changed
 

Diff for: ‎builtin/builtin.lua

+17-15
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ math.randomseed(os.time())
1111
os.setlocale("C", "numeric")
1212

1313
-- Load other files
14-
dofile(minetest.get_modpath("__builtin").."/serialize.lua")
15-
dofile(minetest.get_modpath("__builtin").."/misc_helpers.lua")
16-
dofile(minetest.get_modpath("__builtin").."/item.lua")
17-
dofile(minetest.get_modpath("__builtin").."/misc_register.lua")
18-
dofile(minetest.get_modpath("__builtin").."/item_entity.lua")
19-
dofile(minetest.get_modpath("__builtin").."/deprecated.lua")
20-
dofile(minetest.get_modpath("__builtin").."/misc.lua")
21-
dofile(minetest.get_modpath("__builtin").."/privileges.lua")
22-
dofile(minetest.get_modpath("__builtin").."/auth.lua")
23-
dofile(minetest.get_modpath("__builtin").."/chatcommands.lua")
24-
dofile(minetest.get_modpath("__builtin").."/static_spawn.lua")
25-
dofile(minetest.get_modpath("__builtin").."/detached_inventory.lua")
26-
dofile(minetest.get_modpath("__builtin").."/falling.lua")
27-
dofile(minetest.get_modpath("__builtin").."/features.lua")
28-
dofile(minetest.get_modpath("__builtin").."/voxelarea.lua")
14+
local modpath = minetest.get_modpath("__builtin")
15+
dofile(modpath.."/serialize.lua")
16+
dofile(modpath.."/misc_helpers.lua")
17+
dofile(modpath.."/item.lua")
18+
dofile(modpath.."/misc_register.lua")
19+
dofile(modpath.."/item_entity.lua")
20+
dofile(modpath.."/deprecated.lua")
21+
dofile(modpath.."/misc.lua")
22+
dofile(modpath.."/privileges.lua")
23+
dofile(modpath.."/auth.lua")
24+
dofile(modpath.."/chatcommands.lua")
25+
dofile(modpath.."/static_spawn.lua")
26+
dofile(modpath.."/detached_inventory.lua")
27+
dofile(modpath.."/falling.lua")
28+
dofile(modpath.."/features.lua")
29+
dofile(modpath.."/voxelarea.lua")
30+
dofile(modpath.."/vector.lua")

Diff for: ‎builtin/misc.lua

+10
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ function minetest.formspec_escape(str)
106106
return str
107107
end
108108

109+
function math.hypot(x, y)
110+
local t
111+
x = math.abs(x)
112+
y = math.abs(y)
113+
t = math.min(x, y)
114+
x = math.max(x, y)
115+
t = t / x
116+
return x * math.sqrt(1 + t * t)
117+
end
118+

Diff for: ‎builtin/vector.lua

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
vector = {}
3+
4+
function vector.new(a, b, c)
5+
v = {x=0, y=0, z=0}
6+
if type(a) == "table" then
7+
v = {x=a.x, y=a.y, z=a.z}
8+
elseif a and b and c then
9+
v = {x=a, y=b, z=c}
10+
end
11+
setmetatable(v, {
12+
__add = vector.add,
13+
__sub = vector.subtract,
14+
__mul = vector.multiply,
15+
__div = vector.divide,
16+
__umn = function(v) return vector.multiply(v, -1) end,
17+
__len = vector.length,
18+
__eq = vector.equals,
19+
})
20+
return v
21+
end
22+
23+
function vector.equals(a, b)
24+
return a.x == b.x and
25+
a.y == b.y and
26+
a.z == b.z
27+
end
28+
29+
function vector.length(v)
30+
return math.hypot(v.x, math.hypot(v.y, v.z))
31+
end
32+
33+
function vector.normalize(v)
34+
return vector.divide(v, vector.length(v))
35+
end
36+
37+
function vector.round(v)
38+
return {
39+
x = math.floor(v.x + 0.5),
40+
y = math.floor(v.y + 0.5),
41+
z = math.floor(v.z + 0.5)
42+
}
43+
end
44+
45+
function vector.distance(a, b)
46+
local x = a.x - b.x
47+
local y = a.y - b.y
48+
local z = a.z - b.z
49+
return math.hypot(x, math.hypot(y, z))
50+
end
51+
52+
function vector.direction(pos1, pos2)
53+
local x_raw = pos2.x - pos1.x
54+
local y_raw = pos2.y - pos1.y
55+
local z_raw = pos2.z - pos1.z
56+
local x_abs = math.abs(x_raw)
57+
local y_abs = math.abs(y_raw)
58+
local z_abs = math.abs(z_raw)
59+
if x_abs >= y_abs and
60+
x_abs >= z_abs then
61+
y_raw = y_raw * (1 / x_abs)
62+
z_raw = z_raw * (1 / x_abs)
63+
x_raw = x_raw / x_abs
64+
end
65+
if y_abs >= x_abs and
66+
y_abs >= z_abs then
67+
x_raw = x_raw * (1 / y_abs)
68+
z_raw = z_raw * (1 / y_abs)
69+
y_raw = y_raw / y_abs
70+
end
71+
if z_abs >= y_abs and
72+
z_abs >= x_abs then
73+
x_raw = x_raw * (1 / z_abs)
74+
y_raw = y_raw * (1 / z_abs)
75+
z_raw = z_raw / z_abs
76+
end
77+
return {x=x_raw, y=y_raw, z=z_raw}
78+
end
79+
80+
81+
function vector.add(a, b)
82+
if type(b) == "table" then
83+
return vector.new(
84+
a.x + b.x,
85+
a.y + b.y,
86+
a.z + b.z)
87+
else
88+
return vector.new(
89+
a.x + b,
90+
a.y + b,
91+
a.z + b)
92+
end
93+
end
94+
95+
function vector.subtract(a, b)
96+
if type(b) == "table" then
97+
return vector.new(
98+
a.x - b.x,
99+
a.y - b.y,
100+
a.z - b.z)
101+
else
102+
return vector.new(
103+
a.x - b,
104+
a.y - b,
105+
a.z - b)
106+
end
107+
end
108+
109+
function vector.multiply(a, b)
110+
if type(b) == "table" then
111+
return vector.new(
112+
a.x * b.x,
113+
a.y * b.y,
114+
a.z * b.z)
115+
else
116+
return vector.new(
117+
a.x * b,
118+
a.y * b,
119+
a.z * b)
120+
end
121+
end
122+
123+
function vector.divide(a, b)
124+
if type(b) == "table" then
125+
return vector.new(
126+
a.x / b.x,
127+
a.y / b.y,
128+
a.z / b.z)
129+
else
130+
return vector.new(
131+
a.x / b,
132+
a.y / b,
133+
a.z / b)
134+
end
135+
end
136+

Diff for: ‎doc/lua_api.txt

+29
Original file line numberDiff line numberDiff line change
@@ -988,12 +988,41 @@ Inventory location:
988988
- "nodemeta:<X>,<Y>,<Z>": Any node metadata
989989
- "detached:<name>": A detached inventory
990990

991+
Vector helpers
992+
---------------
993+
vector.new([x[, y, z]]) -> vector
994+
^ x is a table or the x position.
995+
vector.direction(p1, p2) -> vector
996+
vector.distance(p1, p2) -> number
997+
vector.length(v) -> number
998+
vector.normalize(v) -> vector
999+
vector.round(v) -> vector
1000+
vector.equal(v1, v2) -> bool
1001+
vector.add(v, x) -> vector
1002+
^ x can be annother vector or a number
1003+
vector.subtract(v, x) -> vector
1004+
vector.multiply(v, x) -> vector
1005+
vector.divide(v, x) -> vector
1006+
1007+
You can also use Lua operators on vectors.
1008+
For example:
1009+
v1 = vector.new()
1010+
v1 = v1 + 5
1011+
v2 = vector.new(v1)
1012+
v1 = v1 * v2
1013+
if v1 == v2 then
1014+
error("Math broke")
1015+
end
1016+
9911017
Helper functions
9921018
-----------------
9931019
dump2(obj, name="_", dumped={})
9941020
^ Return object serialized as a string, handles reference loops
9951021
dump(obj, dumped={})
9961022
^ Return object serialized as a string
1023+
math.hypot(x, y)
1024+
^ Get the hypotenuse of a triangle with legs x and y.
1025+
Usefull for distance calculation.
9971026
string:split(separator)
9981027
^ eg. string:split("a,b", ",") == {"a","b"}
9991028
string:trim()

0 commit comments

Comments
 (0)
Please sign in to comment.