Builtin: Add vector.angle(). Returns the angle between 2 vectors (#7738)

This commit is contained in:
ANAND 2019-03-17 07:35:03 +05:30 committed by Nils Dagsson Moskopp
parent e639e7a37b
commit 6cf1909254
Signed by: erle
GPG Key ID: A3BC671C35191080
1 changed files with 9 additions and 0 deletions

View File

@ -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,