diff options
author | Paramat <paramat@users.noreply.github.com> | 2017-12-21 19:57:42 +0000 |
---|---|---|
committer | SmallJoker <SmallJoker@users.noreply.github.com> | 2017-12-21 20:57:42 +0100 |
commit | d04c41ad80650822be0ff3d18f253a0dbd570116 (patch) | |
tree | 3a668a7b84754f37beb376b843f48e660adaff5f /builtin/common | |
parent | 18b921015a33c6e597f480bccc7e2974af33c4ea (diff) | |
download | minetest-d04c41ad80650822be0ff3d18f253a0dbd570116.tar.gz minetest-d04c41ad80650822be0ff3d18f253a0dbd570116.tar.bz2 minetest-d04c41ad80650822be0ff3d18f253a0dbd570116.zip |
Vector functions: Fix vector.direction() function, improve documentation (#6801)
vector.direction() now returns a normalised vector with direction p1 to p2.
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/vector.lua | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua index 0549f9a56..c3d380ed3 100644 --- a/builtin/common/vector.lua +++ b/builtin/common/vector.lua @@ -63,34 +63,13 @@ function vector.distance(a, b) end function vector.direction(pos1, pos2) - local x_raw = pos2.x - pos1.x - local y_raw = pos2.y - pos1.y - local z_raw = pos2.z - pos1.z - local x_abs = math.abs(x_raw) - local y_abs = math.abs(y_raw) - local z_abs = math.abs(z_raw) - if x_abs >= y_abs and - x_abs >= z_abs then - y_raw = y_raw * (1 / x_abs) - z_raw = z_raw * (1 / x_abs) - x_raw = x_raw / x_abs - end - if y_abs >= x_abs and - y_abs >= z_abs then - x_raw = x_raw * (1 / y_abs) - z_raw = z_raw * (1 / y_abs) - y_raw = y_raw / y_abs - end - if z_abs >= y_abs and - z_abs >= x_abs then - x_raw = x_raw * (1 / z_abs) - y_raw = y_raw * (1 / z_abs) - z_raw = z_raw / z_abs - end - return {x=x_raw, y=y_raw, z=z_raw} + return vector.normalize({ + x = pos2.x - pos1.x, + y = pos2.y - pos1.y, + z = pos2.z - pos1.z + }) end - function vector.add(a, b) if type(b) == "table" then return {x = a.x + b.x, |