local z=2.718281828459 local function p(p,R,W,s,k,m,V)if R==0 then return W elseif p<.99999999 then local Q=(1-p*p)^.5 local t=(W-k)+((2*p)/R)*m local L=((p/Q)*(W-k)+s/(Q*R))+(((2*p)*p-1)/(Q*R))*m local Z=math.cos((Q*R)*V)local e=math.sin((Q*R)*V)local x=z^((p*R)*V)return(((Z/x)*t+(e/x)*L)+k)+(V-(2*p)/R)*m,(((R*(Z*Q-p*e))/x)*L-((R*(Z*p+Q*e))/x)*t)+m elseif p<1.00000001 then local p=(W-k)+(2/R)*m local Q=(W-k)+(s+m)/R local t=z^(R*V)return((p+(Q*R)*V)/t+k)+(V-2/R)*m,m-(R/t)*(p+(R*V-1)*Q)else local Q=(p*p-1)^.5 local t=(m-s)/((2*R)*Q)local L=(p/R)*m-(k-W)/2 local Z=(1-p/Q)*L+t local e=(1+p/Q)*L-t local x=z^((-(Q+p)*R)*V)local F=z^(((Q-p)*R)*V)return((Z*x+e*F)+k)+(V-(2*p)/R)*m,(((F*(Q-p))*R)*e-((x*(p+Q))*R)*Z)+m end end local function R(z,p,R)return z+R*p,p end local W={}W.__index=W
--[[
	A simplistic spring implementation.
]]

local e = 2.718281828459045

local function posvel(d, s, p0, v0, p1, v1, x)
	if s == 0 then
		return p0
	elseif d < 1 - 1e-8 then
		local h = (1 - d * d) ^ 0.5
		local c1 = (p0 - p1 + 2 * d / s * v1)
		local c2 = d / h * (p0 - p1) + v0 / (h * s) + (2 * d * d - 1) / (h * s) * v1
		local co = math.cos(h * s * x)
		local si = math.sin(h * s * x)
		local ex = e ^ (d * s * x)
		return co / ex * c1 + si / ex * c2 + p1 + (x - 2 * d / s) * v1,
			s * (co * h - d * si) / ex * c2 - s * (co * d + h * si) / ex * c1 + v1
	elseif d < 1 + 1e-8 then
		local c1 = p0 - p1 + 2 / s * v1
		local c2 = p0 - p1 + (v0 + v1) / s
		local ex = e ^ (s * x)
		return (c1 + c2 * s * x) / ex + p1 + (x - 2 / s) * v1, v1 - s / ex * (c1 + (s * x - 1) * c2)
	else
		local h = (d * d - 1) ^ 0.5
		local a = (v1 - v0) / (2 * s * h)
		local b = d / s * v1 - (p1 - p0) / 2
		local c1 = (1 - d / h) * b + a
		local c2 = (1 + d / h) * b - a
		local co = e ^ (-(h + d) * s * x)
		local si = e ^ ((h - d) * s * x)
		return c1 * co + c2 * si + p1 + (x - 2 * d / s) * v1, si * (h - d) * s * c2 - co * (d + h) * s * c1 + v1
	end
end

local function targposvel(p1, v1, x)
	return p1 + x * v1, v1
end

local Spring = {}

Spring.__index = Spring
