function Spring.TimeSkip(h,K)local R=tick()local V,m=h:_positionVelocity(R+K)h._position0=V h._velocity0=m h._time0=R end function Spring.__index(h,K)if Spring[K]then return Spring[K]elseif K=="\086\097\108\117\101"or K=="\080\111\115\105\116\105\111\110"or K=="\112"then local K,R=h:_positionVelocity(tick())return K elseif K=="\086\101\108\111\099\105\116\121"or K=="\118"then local K,R=h:_positionVelocity(tick())return R elseif K=="\084\097\114\103\101\116"or K=="\116"then return h._target elseif K=="\068\097\109\112\101\114"or K=="\100"then return h._damper elseif K=="\083\112\101\101\100"or K=="\115"then return h._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(K)),2)end end function Spring.__newindex(R,K,h)local V=tick()if K=="\086\097\108\117\101"or K=="\080\111\115\105\116\105\111\110"or K=="\112"then local K,m=R:_positionVelocity(V)R._position0=h R._velocity0=m elseif K=="\086\101\108\111\099\105\116\121"or K=="\118"then local K,m=R:_positionVelocity(V)R._position0=K R._velocity0=h elseif K=="\084\097\114\103\101\116"or K=="\116"then local K,m=R:_positionVelocity(V)R._position0=K R._velocity0=m R._target=h elseif K=="\068\097\109\112\101\114"or K=="\100"then local K,m=R:_positionVelocity(V)R._position0=K R._velocity0=m R._damper=math.clamp(h,0,1)elseif K=="\083\112\101\101\100"or K=="\115"then local K,m=R:_positionVelocity(V)R._position0=K R._velocity0=m R._speed=h<0 and 0 or h 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(K)),2)end R._time0=V end function Spring._positionVelocity(h,K)local R=h._position0 local V=h._velocity0 local m=h._target local B=h._damper local d=h._speed local p=d*(K-h._time0)local Q=B*B local e,Z,r if Q<1 then e=math.sqrt(1-Q)local K=math.exp(-B*p)/e r,Z=K*math.cos(e*p),K*math.sin(e*p)elseif Q==1 then e=1 local K=math.exp(-B*p)/e r,Z=K,K*p else e=math.sqrt(Q-1)local K=math.exp((-B+e)*p)/(2*e)local h=math.exp((-B-e)*p)/(2*e)r,Z=K+h,K-h end local F=e*r+B*Z local M=1-(e*r+B*Z)local L=Z/d local n=-d*Z local H=d*Z local c=e*r-B*Z return(F*R+M*m)+L*V,(n*R+H*m)+c*V end return Spring

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
