Skip to content

Commit

Permalink
Add vector.dot and vector.cross
Browse files Browse the repository at this point in the history
Mostly copied from MarkuBu's code
  • Loading branch information
HybridDog authored and sfan5 committed Jul 16, 2019
1 parent 458f617 commit 71db715
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions builtin/common/vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ function vector.angle(a, b)
return math.atan2(crossplen, dotp)
end

function vector.dot(a, b)
return a.x * b.x + a.y * b.y + a.z * b.z
end

function vector.cross(a, b)
return {
x = a.y * b.z - a.z * b.y,
y = a.z * b.x - a.x * b.z,
z = a.x * b.y - a.y * b.x
}
end

function vector.add(a, b)
if type(b) == "table" then
return {x = a.x + b.x,
Expand Down
4 changes: 4 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,10 @@ For the following functions, `v`, `v1`, `v2` are vectors,
* 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.
* `vector.dot(v1, v2)`
* Returns the dot product of `v1` and `v2`
* `vector.cross(v1, v2)`
* Returns the cross product of `v1` and `v2`

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

Expand Down

0 comments on commit 71db715

Please sign in to comment.