function Spring.TimeSkip(c,Q)local k=tick()local s,i=c:_positionVelocity(k+Q)c._position0=s c._velocity0=i c._time0=k end function Spring.__index(c,Q)if Spring[Q]then return Spring[Q]elseif Q=="\086\097\108\117\101"or Q=="\080\111\115\105\116\105\111\110"or Q=="\112"then local Q,k=c:_positionVelocity(tick())return Q elseif Q=="\086\101\108\111\099\105\116\121"or Q=="\118"then local Q,k=c:_positionVelocity(tick())return k elseif Q=="\084\097\114\103\101\116"or Q=="\116"then return c._target elseif Q=="\068\097\109\112\101\114"or Q=="\100"then return c._damper elseif Q=="\083\112\101\101\100"or Q=="\115"then return c._speed else error(("\037\113 \105\115 \110\111\116 \097 \118\097\108\105\100 \109\101\109\098\101\114 \111\102 \083\112\114\105\110\103"):format(tostring(Q)),2)end end function Spring.__newindex(k,Q,c)local s=tick()if Q=="\086\097\108\117\101"or Q=="\080\111\115\105\116\105\111\110"or Q=="\112"then local Q,i=k:_positionVelocity(s)k._position0=c k._velocity0=i elseif Q=="\086\101\108\111\099\105\116\121"or Q=="\118"then local Q,i=k:_positionVelocity(s)k._position0=Q k._velocity0=c elseif Q=="\084\097\114\103\101\116"or Q=="\116"then local Q,i=k:_positionVelocity(s)k._position0=Q k._velocity0=i k._target=c elseif Q=="\068\097\109\112\101\114"or Q=="\100"then local Q,i=k:_positionVelocity(s)k._position0=Q k._velocity0=i k._damper=math.clamp(c,0,1)elseif Q=="\083\112\101\101\100"or Q=="\115"then local Q,i=k:_positionVelocity(s)k._position0=Q k._velocity0=i k._speed=c<0 and 0 or c else error(("\037\113 \105\115 \110\111\116 \097 \118\097\108\105\100 \109\101\109\098\101\114 \111\102 \083\112\114\105\110\103"):format(tostring(Q)),2)end k._time0=s end function Spring._positionVelocity(c,Q)local k=c._position0 local s=c._velocity0 local i=c._target local K=c._damper local r=c._speed local e=r*(Q-c._time0)local p=K*K local f,A,x if p<1 then f=math.sqrt(1-p)local Q=math.exp(-K*e)/f x,A=Q*math.cos(f*e),Q*math.sin(f*e)elseif p==1 then f=1 local Q=math.exp(-K*e)/f x,A=Q,Q*e else f=math.sqrt(p-1)local Q=math.exp((-K+f)*e)/(2*f)local c=math.exp((-K-f)*e)/(2*f)x,A=Q+c,Q-c end local D=f*x+K*A local h=1-(f*x+K*A)local M=A/r local V=-r*A local v=r*A local C=f*x-K*A return(D*k+h*i)+M*s,(V*k+v*i)+C*s end return Spring
--- Skip forwards in now
-- @param delta now to skip forwards
function Spring:TimeSkip(delta)
	local now = tick()
	local position, velocity = self:_positionVelocity(now + delta)
	self._position0 = position
	self._velocity0 = velocity
	self._time0 = now
end

function Spring:__index(index)
	if Spring[index] then
		return Spring[index]
	elseif index == "Value" or index == "Position" or index == "p" then
		local position, _ = self:_positionVelocity(tick())
		return position
	elseif index == "Velocity" or index == "v" then
		local _, velocity = self:_positionVelocity(tick())
		return velocity
	elseif index == "Target" or index == "t" then
		return self._target
	elseif index == "Damper" or index == "d" then
		return self._damper
	elseif index == "Speed" or index == "s" then
		return self._speed
	else
		error(("%q is not a valid member of Spring"):format(tostring(index)), 2)
	end
end

function Spring:__newindex(index, value)
	local now = tick()

	if index == "Value" or index == "Position" or index == "p" then
		local _, velocity = self:_positionVelocity(now)
		self._position0 = value
		self._velocity0 = velocity
	elseif index == "Velocity" or index == "v" then
		local position, _ = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = value
	elseif index == "Target" or index == "t" then
		local position, velocity = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = velocity
		self._target = value
	elseif index == "Damper" or index == "d" then
		local position, velocity = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = velocity
		self._damper = math.clamp(value, 0, 1)
	elseif index == "Speed" or index == "s" then
		local position, velocity = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = velocity
		self._speed = value < 0 and 0 or value
	else
		error(("%q is not a valid member of Spring"):format(tostring(index)), 2)
	end

	self._time0 = now
end

function Spring:_positionVelocity(now)
	local p0 = self._position0
	local v0 = self._velocity0
	local p1 = self._target
	local d = self._damper
	local s = self._speed

	local t = s * (now - self._time0)
	local d2 = d * d

	local h, si, co
	if d2 < 1 then
		h = math.sqrt(1 - d2)
		local ep = math.exp(-d * t) / h
		co, si = ep * math.cos(h * t), ep * math.sin(h * t)
	elseif d2 == 1 then
		h = 1
		local ep = math.exp(-d * t) / h
		co, si = ep, ep * t
	else
		h = math.sqrt(d2 - 1)
		local u = math.exp((-d + h) * t) / (2 * h)
		local v = math.exp((-d - h) * t) / (2 * h)
		co, si = u + v, u - v
	end

	local a0 = h * co + d * si
	local a1 = 1 - (h * co + d * si)
	local a2 = si / s

	local b0 = -s * si
	local b1 = s * si
	local b2 = h * co - d * si

	return a0 * p0 + a1 * p1 + a2 * v0, b0 * p0 + b1 * p1 + b2 * v0
end

return Spring
