function Spring.TimeSkip(J,M)local C=J._clock()local G,W=J:_positionVelocity(C+M)J._position0=G J._velocity0=W J._time0=C end function Spring.__index(J,M)if Spring[M]then return Spring[M]elseif M=="\086\097\108\117\101"or M=="\080\111\115\105\116\105\111\110"or M=="\112"then local M,C=J:_positionVelocity(J._clock())return M elseif M=="\086\101\108\111\099\105\116\121"or M=="\118"then local M,C=J:_positionVelocity(J._clock())return C elseif M=="\084\097\114\103\101\116"or M=="\116"then return J._target elseif M=="\068\097\109\112\101\114"or M=="\100"then return J._damper elseif M=="\083\112\101\101\100"or M=="\115"then return J._speed elseif M=="\067\108\111\099\107"then return J._clock 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(M)),2)end end function Spring.__newindex(C,M,J)local G=C._clock()if M=="\086\097\108\117\101"or M=="\080\111\115\105\116\105\111\110"or M=="\112"then local M,W=C:_positionVelocity(G)C._position0=J C._velocity0=W C._time0=G elseif M=="\086\101\108\111\099\105\116\121"or M=="\118"then local M,W=C:_positionVelocity(G)C._position0=M C._velocity0=J C._time0=G elseif M=="\084\097\114\103\101\116"or M=="\116"then local M,W=C:_positionVelocity(G)C._position0=M C._velocity0=W C._target=J C._time0=G elseif M=="\068\097\109\112\101\114"or M=="\100"then local M,W=C:_positionVelocity(G)C._position0=M C._velocity0=W C._damper=math.clamp(J,0,1)C._time0=G elseif M=="\083\112\101\101\100"or M=="\115"then local M,W=C:_positionVelocity(G)C._position0=M C._velocity0=W C._speed=J<0 and 0 or J C._time0=G elseif M=="\067\108\111\099\107"then local M,W=C:_positionVelocity(G)C._position0=M C._velocity0=W C._clock=J C._time0=J()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(M)),2)end end function Spring._positionVelocity(J,M)local C=J._position0 local G=J._velocity0 local W=J._target local V=J._damper local j=J._speed local N=j*(M-J._time0)local t=V*V local v,Z,L if t<1 then v=math.sqrt(1-t)local M=math.exp(-V*N)/v L,Z=M*math.cos(v*N),M*math.sin(v*N)elseif t==1 then v=1 local M=math.exp(-V*N)/v L,Z=M,M*N else v=math.sqrt(t-1)local M=math.exp((-V+v)*N)/(2*v)local J=math.exp((-V-v)*N)/(2*v)L,Z=M+J,M-J end local h=v*L+V*Z local y=1-(v*L+V*Z)local b=Z/j local x=-j*Z local R=j*Z local u=v*L-V*Z return(h*C+y*W)+b*G,(x*C+R*W)+u*G end return Spring

function Spring:TimeSkip(delta)
	local now = self._clock()
	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(self._clock())
		return position
	elseif index == "Velocity" or index == "v" then
		local _, velocity = self:_positionVelocity(self._clock())
		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
	elseif index == "Clock" then
		return self._clock
	else
		error(("%q is not a valid member of Spring"):format(tostring(index)), 2)
	end
end

function Spring:__newindex(index, value)
	local now = self._clock()

	if index == "Value" or index == "Position" or index == "p" then
		local _, velocity = self:_positionVelocity(now)
		self._position0 = value
		self._velocity0 = velocity
		self._time0 = now
	elseif index == "Velocity" or index == "v" then
		local position, _ = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = value
		self._time0 = now
	elseif index == "Target" or index == "t" then
		local position, velocity = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = velocity
		self._target = value
		self._time0 = now
	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)
		self._time0 = now
	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
		self._time0 = now
	elseif index == "Clock" then
		local position, velocity = self:_positionVelocity(now)
		self._position0 = position
		self._velocity0 = velocity
		self._clock = value
		self._time0 = value()
	else
		error(("%q is not a valid member of Spring"):format(tostring(index)), 2)
	end
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
