Navigation Menu

Skip to content

Commit

Permalink
Builtin: Add vector.angle(). Returns the angle between 2 vectors (#7738)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClobberXD authored and paramat committed Mar 17, 2019
1 parent eadcbe4 commit 7f1c2b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions builtin/common/vector.lua
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -2399,6 +2399,8 @@ For the following functions, `v`, `v1`, `v2` are vectors,
* Returns a boolean, `true` if the vectors are identical.
* `vector.sort(v1, v2)`:
* Returns in order minp, maxp vectors of the cuboid defined by `v1`, `v2`.
* `vector.angle(v1, v2)`:
* Returns the angle between `v1` and `v2` in radians.

For the following functions `x` can be either a vector or a number:

Expand Down

0 comments on commit 7f1c2b8

Please sign in to comment.