add lerp function to util and lerp method to vector3

This commit is contained in:
Lera Elvoé 2025-02-14 22:02:19 +03:00
parent a532325cd2
commit a0ba6f9da3
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 39 additions and 0 deletions

View File

@ -216,6 +216,25 @@ end
function Vector3:copy()
return Vector3(self)
end
---Returns a new vector as a linear interpolation between self and b.
---@param b vectorlike
---@param t number
---@return Vector3
function Vector3:lerp(b, t)
-- return self:copy()
if not is_weak_vector3(b) then
error("Vector3: b must be a Vector3-like table. Returning copy of self.")
return self:copy()
end
local w = Vector3(b)
return Vector3{
util.lerp(self.x, w.x, t),
util.lerp(self.y, w.y, t),
util.lerp(self.z, w.z, t),
}
end
---- CONSTANTS
Vector3.UP = Vector3(0, 1, 0)

View File

@ -97,4 +97,24 @@ function util.list_insert_once(t, value)
if util.list_find(t, value) ~= -1 then return end
table.insert(t, value)
end
---linear interpolation
---@param a number
---@param b number
---@param t number in [0, 1]
---@return number
function util.lerp(a, b, t)
return a + t * (b - a)
end
---constrains v between min and max
---@param v number
---@param min number
---@param max number
---@return number
function util.clamp(v, min, max)
return math.max(math.min(v, max), min)
end
return util