diff --git a/math.lua b/math.lua index 7c9def4..31713ce 100644 --- a/math.lua +++ b/math.lua @@ -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) diff --git a/vec2.lua b/vec2.lua index 02efe4e..4a92369 100644 --- a/vec2.lua +++ b/vec2.lua @@ -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 ----------------------------------------------------------- diff --git a/vec3.lua b/vec3.lua index 38e5cdf..754998a 100644 --- a/vec3.lua +++ b/vec3.lua @@ -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 -----------------------------------------------------------