Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(relativeCoords): add lib.getRelativeCoords function #685

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions imports/getRelativeCoords/shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local glm_sincos = require 'glm'.sincos
local glm_rad = require 'glm'.rad

---Get the relative coordinates of a vector3 based on a heading and offset
---@overload fun(coords: vector3, heading: number, offset: vector3): vector3
---@overload fun(coords: vector4, offset: vector3): vector4
---@param coords vector3 | vector4
---@param heading number
---@param offset vector3
---@return vector3
function lib.getRelativeCoords(coords, heading, offset)
offset = offset or heading
local x, y, z, w = coords.x, coords.y, coords.z, type(heading) == 'number' and heading or coords.w
local sin, cos = glm_sincos --[[@as fun(n: number): number, number]](glm_rad(w))
local relativeX = offset.x * cos - offset.y * sin
local relativeY = offset.x * sin + offset.y * cos

return coords.w and vec4(
x + relativeX,
y + relativeY,
z + offset.z,
w
) or vec3(
x + relativeX,
y + relativeY,
z + offset.z
)
end

return lib.getRelativeCoords