Skip to content

Commit 5c0239b

Browse files
authored
feat(relativeCoords): add lib.getRelativeCoords function (#685)
1 parent c1ccd76 commit 5c0239b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

imports/getRelativeCoords/shared.lua

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local glm_sincos = require 'glm'.sincos
2+
local glm_rad = require 'glm'.rad
3+
4+
---Get the relative coordinates of a vector3 based on a heading and offset
5+
---@overload fun(coords: vector3, heading: number, offset: vector3): vector3
6+
---@overload fun(coords: vector4, offset: vector3): vector4
7+
---@param coords vector3 | vector4
8+
---@param heading number
9+
---@param offset vector3
10+
---@return vector3
11+
function lib.getRelativeCoords(coords, heading, offset)
12+
offset = offset or heading
13+
local x, y, z, w = coords.x, coords.y, coords.z, type(heading) == 'number' and heading or coords.w
14+
local sin, cos = glm_sincos --[[@as fun(n: number): number, number]](glm_rad(w))
15+
local relativeX = offset.x * cos - offset.y * sin
16+
local relativeY = offset.x * sin + offset.y * cos
17+
18+
return coords.w and vec4(
19+
x + relativeX,
20+
y + relativeY,
21+
z + offset.z,
22+
w
23+
) or vec3(
24+
x + relativeX,
25+
y + relativeY,
26+
z + offset.z
27+
)
28+
end
29+
30+
return lib.getRelativeCoords

0 commit comments

Comments
 (0)