[added] math.lerp_eps and matching vec2/vec3 methods; for ensuring a lerp actually reaches its destination once the delta doesn't matter

This commit is contained in:
Max Cahill 2020-04-01 20:58:18 +11:00
parent 870d291369
commit 9422152181
3 changed files with 31 additions and 0 deletions

View File

@ -55,6 +55,16 @@ function _math.lerp(a, b, t)
return a * (1.0 - t) + b * t
end
--linear interpolation with a minimum "final step" distance
--useful for making sure dynamic lerps do actually reach their final destination
function _math.lerp_eps(a, b, t, eps)
local v = math.lerp(a, b, t)
if math.abs(v - b) < eps then
v = b
end
return v
end
--classic smoothstep
--(only "safe" for 0-1 range)
function _math.smoothstep(v)

View File

@ -484,6 +484,16 @@ function vec2:lerp(other, amount)
return self:copy():lerpi(other, amount)
end
function vec2:lerp_epsi(other, amount, eps)
self.x = math.lerp_eps(self.x, other.x, amount, eps)
self.y = math.lerp_eps(self.y, other.y, amount, eps)
return self
end
function vec2:lerp_eps(other, amount, eps)
return self:copy():lerp_epsi(other, amount, eps)
end
-----------------------------------------------------------
-- vector products and projections
-----------------------------------------------------------

View File

@ -556,6 +556,17 @@ function vec3:lerp(other, amount)
return self:copy():lerpi(other, amount)
end
function vec3:lerp_epsi(other, amount, eps)
self.x = math.lerp_eps(self.x, other.x, amount, eps)
self.y = math.lerp_eps(self.y, other.y, amount, eps)
self.z = math.lerp_eps(self.z, other.z, amount, eps)
return self
end
function vec3:lerp_eps(other, amount, eps)
return self:copy():lerp_epsi(other, amount, eps)
end
-----------------------------------------------------------
-- vector products and projections
-----------------------------------------------------------