function Spring.TimeSkip(s,y)local N=tick()local j,v=s:_positionVelocity(N+y)s._position0=j s._velocity0=v s._time0=N end function Spring.__index(s,y)if Spring[y]then return Spring[y]elseif y=="\086\097\108\117\101"or y=="\080\111\115\105\116\105\111\110"or y=="\112"then local y,N=s:_positionVelocity(tick())return y elseif y=="\086\101\108\111\099\105\116\121"or y=="\118"then local y,N=s:_positionVelocity(tick())return N elseif y=="\084\097\114\103\101\116"or y=="\116"then return s._target elseif y=="\068\097\109\112\101\114"or y=="\100"then return s._damper elseif y=="\083\112\101\101\100"or y=="\115"then return s._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(y)),2)end end function Spring.__newindex(N,y,s)local j=tick()if y=="\086\097\108\117\101"or y=="\080\111\115\105\116\105\111\110"or y=="\112"then local y,v=N:_positionVelocity(j)N._position0=s N._velocity0=v elseif y=="\086\101\108\111\099\105\116\121"or y=="\118"then local y,v=N:_positionVelocity(j)N._position0=y N._velocity0=s elseif y=="\084\097\114\103\101\116"or y=="\116"then local y,v=N:_positionVelocity(j)N._position0=y N._velocity0=v N._target=s elseif y=="\068\097\109\112\101\114"or y=="\100"then local y,v=N:_positionVelocity(j)N._position0=y N._velocity0=v N._damper=math.clamp(s,0,1)elseif y=="\083\112\101\101\100"or y=="\115"then local y,v=N:_positionVelocity(j)N._position0=y N._velocity0=v N._speed=s<0 and 0 or s 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(y)),2)end N._time0=j end function Spring._positionVelocity(s,y)local N=s._position0 local j=s._velocity0 local v=s._target local E=s._damper local x=s._speed local M=x*(y-s._time0)local R=E*E local Q,g,d if R<1 then Q=math.sqrt(1-R)local y=math.exp(-E*M)/Q d,g=y*math.cos(Q*M),y*math.sin(Q*M)elseif R==1 then Q=1 local y=math.exp(-E*M)/Q d,g=y,y*M else Q=math.sqrt(R-1)local y=math.exp((-E+Q)*M)/(2*Q)local s=math.exp((-E-Q)*M)/(2*Q)d,g=y+s,y-s end local T=Q*d+E*g local U=1-(Q*d+E*g)local l=(g/x)/2 local V=-x*g local f=x*g local W=Q*d-E*g return(T*N+U*v)+l*j,(V*N+f*v)+W*j 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 / 2

	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
