local V={}do V.__index=V function V.new(L,B)local U=setmetatable({},V)U.f=L U.p=B U.v=B*0 return U end function V.Update(B,V,L)local U=(B.f*2)*pi local A=B.p local m=B.v local X=L-A local d=exp(-U*V)local i=L+(m*V-X*(U*V+1))*d local f=((U*V)*(X*U-m)+m)*d B.p=i B.v=f return i end function V.Reset(L,V)L.p=V L.v=V*0 end end
------------------------------------------------------------------------

local Spring = {}
do
	Spring.__index = Spring

	function Spring.new(freq, pos)
		local self = setmetatable({}, Spring)
		self.f = freq
		self.p = pos
		self.v = pos * 0
		return self
	end

	function Spring:Update(dt, goal)
		local f = self.f * 2 * pi
		local p0 = self.p
		local v0 = self.v

		local offset = goal - p0
		local decay = exp(-f * dt)

		local p1 = goal + (v0 * dt - offset * (f * dt + 1)) * decay
		local v1 = (f * dt * (offset * f - v0) + v0) * decay

		self.p = p1
		self.v = v1

		return p1
	end

	function Spring:Reset(pos)
		self.p = pos
		self.v = pos * 0
	end
end
