function Spring.TimeSkip(U,Y)local C=tick()local L,p=U:_positionVelocity(C+Y)U._position0=L U._velocity0=p U._time0=C end function Spring.__index(U,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,C=U:_positionVelocity(tick())return Y elseif Y=="\086\101\108\111\099\105\116\121"or Y=="\118"then local Y,C=U:_positionVelocity(tick())return C elseif Y=="\084\097\114\103\101\116"or Y=="\116"then return U._target elseif Y=="\068\097\109\112\101\114"or Y=="\100"then return U._damper elseif Y=="\083\112\101\101\100"or Y=="\115"then return U._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(C,Y,U)local L=tick()if Y=="\086\097\108\117\101"or Y=="\080\111\115\105\116\105\111\110"or Y=="\112"then local Y,p=C:_positionVelocity(L)C._position0=U C._velocity0=p elseif Y=="\086\101\108\111\099\105\116\121"or Y=="\118"then local Y,p=C:_positionVelocity(L)C._position0=Y C._velocity0=U elseif Y=="\084\097\114\103\101\116"or Y=="\116"then local Y,p=C:_positionVelocity(L)C._position0=Y C._velocity0=p C._target=U elseif Y=="\068\097\109\112\101\114"or Y=="\100"then local Y,p=C:_positionVelocity(L)C._position0=Y C._velocity0=p C._damper=math.clamp(U,0,1)elseif Y=="\083\112\101\101\100"or Y=="\115"then local Y,p=C:_positionVelocity(L)C._position0=Y C._velocity0=p C._speed=U<0 and 0 or U 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 C._time0=L end function Spring._positionVelocity(U,Y)local C=U._position0 local L=U._velocity0 local p=U._target local i=U._damper local g=U._speed local e=g*(Y-U._time0)local E=i*i local a,X,w if E<1 then a=math.sqrt(1-E)local Y=math.exp(-i*e)/a w,X=Y*math.cos(a*e),Y*math.sin(a*e)elseif E==1 then a=1 local Y=math.exp(-i*e)/a w,X=Y,Y*e else a=math.sqrt(E-1)local Y=math.exp((-i+a)*e)/(2*a)local U=math.exp((-i-a)*e)/(2*a)w,X=Y+U,Y-U end local Z=a*w+i*X local o=1-(a*w+i*X)local c=(X/g)/2 local r=-g*X local b=g*X local y=a*w-i*X return(Z*C+o*p)+c*L,(r*C+b*p)+y*L 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/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
