aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSmallJoker <mk939@ymail.com>2014-12-05 23:18:52 +0100
committerKahrl <kahrl@gmx.net>2014-12-06 10:07:25 +0100
commit2fd14e1bd53918b3c4fcdfbc81294ff8b7afe1fa (patch)
tree7a997950d75f1d2a1c49b30128b8535d3a25af45
parent0e78aa296ee8b552273fdc9d6fabec0fb01d2e4a (diff)
downloadminetest-2fd14e1bd53918b3c4fcdfbc81294ff8b7afe1fa.tar.gz
minetest-2fd14e1bd53918b3c4fcdfbc81294ff8b7afe1fa.tar.bz2
minetest-2fd14e1bd53918b3c4fcdfbc81294ff8b7afe1fa.zip
Add Lua helper functions vector.apply(v) math.sign(x, tolerance)
-rw-r--r--builtin/common/misc_helpers.lua11
-rw-r--r--builtin/common/vector.lua8
-rw-r--r--doc/lua_api.txt4
3 files changed, 23 insertions, 0 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index 0b608126e..e1dc28965 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -213,6 +213,17 @@ function math.hypot(x, y)
end
--------------------------------------------------------------------------------
+function math.sign(x, tolerance)
+ tolerance = tolerance or 0
+ if x > tolerance then
+ return 1
+ elseif x < -tolerance then
+ return -1
+ end
+ return 0
+end
+
+--------------------------------------------------------------------------------
function get_last_folder(text,count)
local parts = text:split(DIR_DELIM)
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua
index 88ccfe6da..e9ed3aab3 100644
--- a/builtin/common/vector.lua
+++ b/builtin/common/vector.lua
@@ -39,6 +39,14 @@ function vector.round(v)
}
end
+function vector.apply(v, func)
+ return {
+ x = func(v.x),
+ y = func(v.y),
+ z = func(v.z)
+ }
+end
+
function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index e2ef3faab..6c33d92d4 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1280,6 +1280,7 @@ vector.distance(p1, p2) -> number
vector.length(v) -> number
vector.normalize(v) -> vector
vector.round(v) -> vector
+vector.apply(v, func) -> vector
vector.equals(v1, v2) -> bool
For the following functions x can be either a vector or a number.
vector.add(v, x) -> vector
@@ -1296,6 +1297,9 @@ dump(obj, dumped={})
math.hypot(x, y)
^ Get the hypotenuse of a triangle with legs x and y.
Useful for distance calculation.
+math.sign(x, tolerance)
+^ Get the sign of a number
+ Optional: Also returns 0 when the absolute value is within the tolerance (default 0)
string:split(separator)
^ e.g. string:split("a,b", ",") == {"a","b"}
string:trim()