diff options
author | ANAND <ClobberXD@gmail.com> | 2019-03-17 07:35:03 +0530 |
---|---|---|
committer | Paramat <paramat@users.noreply.github.com> | 2019-03-17 02:05:03 +0000 |
commit | 7f1c2b8a00ede0659746102762aeb1da487eb6f8 (patch) | |
tree | 0dd9b48cd77da498bc14ee6f48a05d7e862df4c4 /builtin/common | |
parent | eadcbe474a04174b78d35ab802e974bc2bf3fac6 (diff) | |
download | minetest-7f1c2b8a00ede0659746102762aeb1da487eb6f8.tar.gz minetest-7f1c2b8a00ede0659746102762aeb1da487eb6f8.tar.bz2 minetest-7f1c2b8a00ede0659746102762aeb1da487eb6f8.zip |
Builtin: Add vector.angle(). Returns the angle between 2 vectors (#7738)
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/vector.lua | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua index c3d380ed3..9a53409fa 100644 --- a/builtin/common/vector.lua +++ b/builtin/common/vector.lua @@ -70,6 +70,15 @@ function vector.direction(pos1, pos2) }) end +function vector.angle(a, b) + local dotp = a.x * b.x + a.y * b.y + a.z * b.z + local cpx = a.y * b.z - a.z * b.y + local cpy = a.z * b.x - a.x * b.z + local cpz = a.x * b.y - a.y * b.x + local crossplen = math.sqrt(cpx ^ 2 + cpy ^ 2 + cpz ^ 2) + return math.atan2(crossplen, dotp) +end + function vector.add(a, b) if type(b) == "table" then return {x = a.x + b.x, |