--!strict --[[ Spring — smooth, physically based motion for numbers, Vector2 and Vector3. SKLOP Studio · https://sklop.vercel.app · MIT License (free to use in any project) Uses the exact (analytic) solution of a damped spring, so it is stable at any frame rate — no jitter on low-FPS phones, no explosions on lag spikes. USAGE local Spring = require(path.to.Spring) local RunService = game:GetService("RunService") -- frequency: how fast it moves (oscillations per second) -- damping: 1 = smooth stop, < 1 = bouncy, > 1 = sluggish local scale = Spring.new(1, { frequency = 4, damping = 0.6 }) button.MouseEnter:Connect(function() scale:SetTarget(1.08) end) button.MouseLeave:Connect(function() scale:SetTarget(1) end) RunService.RenderStepped:Connect(function(dt) uiScale.Scale = scale:Update(dt) end) -- Camera sway with Vector3: local sway = Spring.new(Vector3.zero, { frequency = 2, damping = 0.4 }) sway:Impulse(Vector3.new(0, 4, 0)) ]] type Value = any -- number | Vector2 | Vector3 export type Options = { frequency: number?, damping: number?, } export type Spring = { Position: T, Velocity: T, Target: T, Frequency: number, Damping: number, SetTarget: (self: Spring, target: T) -> (), SnapTo: (self: Spring, value: T) -> (), Impulse: (self: Spring, velocity: T) -> (), Update: (self: Spring, dt: number) -> T, IsSettled: (self: Spring, epsilon: number?) -> boolean, } local function zeroLike(value: Value): Value if type(value) == "number" then return 0 end return value * 0 end local function magnitude(value: Value): number if type(value) == "number" then return math.abs(value) end return value.Magnitude end local SpringClass = {} SpringClass.__index = SpringClass local Spring = {} --- Creates a spring resting at `initial`. function Spring.new(initial: T, options: Options?): Spring local opts = options or {} local self = { Position = initial, Velocity = zeroLike(initial), Target = initial, Frequency = math.max(opts.frequency or 4, 0.01), Damping = math.max(opts.damping or 1, 0), } return (setmetatable(self, SpringClass) :: any) :: Spring end function SpringClass.SetTarget(self: any, target: Value) self.Target = target end function SpringClass.SnapTo(self: any, value: Value) self.Position = value self.Target = value self.Velocity = zeroLike(value) end function SpringClass.Impulse(self: any, velocity: Value) self.Velocity = self.Velocity + velocity end function SpringClass.Update(self: any, dt: number): Value if dt <= 0 then return self.Position end local w = 2 * math.pi * self.Frequency local z = self.Damping local x0: Value = self.Position - self.Target -- offset from target local v0: Value = self.Velocity local x: Value local v: Value if z < 1 then -- Under-damped: oscillates and settles. local wd = w * math.sqrt(1 - z * z) local e = math.exp(-z * w * dt) local c, s = math.cos(wd * dt), math.sin(wd * dt) local a = x0 local b = (v0 + x0 * (z * w)) * (1 / wd) x = (a * c + b * s) * e v = (b * (wd * c - z * w * s) - a * (wd * s + z * w * c)) * e elseif z == 1 then -- Critically damped: fastest settle without overshoot. local e = math.exp(-w * dt) local c1 = x0 local c2 = v0 + x0 * w x = (c1 + c2 * dt) * e v = (c2 - (c1 + c2 * dt) * w) * e else -- Over-damped: slow, no overshoot. local root = math.sqrt(z * z - 1) local r1 = -w * (z - root) local r2 = -w * (z + root) local c2 = (v0 - x0 * r1) * (1 / (r2 - r1)) local c1 = x0 - c2 local e1, e2 = math.exp(r1 * dt), math.exp(r2 * dt) x = c1 * e1 + c2 * e2 v = c1 * (r1 * e1) + c2 * (r2 * e2) end self.Position = self.Target + x self.Velocity = v return self.Position end function SpringClass.IsSettled(self: any, epsilon: number?): boolean local eps = epsilon or 1e-3 return magnitude(self.Position - self.Target) < eps and magnitude(self.Velocity) < eps end return Spring