function Spring.new(U,f,j)local M=f or 1 local n=j or 1 local o=U or 0 local H=0*o local y=o local V=H local u=tick()local W={}local l={}function W.getpv()return posvel(M,n,o,H,y,V,tick()-u)end function W.setpv(U,f)local j=tick()local M,n=targposvel(y,V,j-u)o,H=U,f y,V=M,n u=j end function W.settargetpv(U,f)local j=tick()local W,l=posvel(M,n,o,H,y,V,j-u)o,H=W,l y,V=U,f u=j end function W.accelerate(f,U)local j=tick()local W,l=posvel(M,n,o,H,y,V,j-u)local i,b=targposvel(y,V,j-u)o,H=W,l+U y,V=i,b u=j end function l.__index(U,f)local j=tick()if f=="\112"or f=="\112\111\115\105\116\105\111\110"then local U,f=posvel(M,n,o,H,y,V,j-u)return U elseif f=="\118"or f=="\118\101\108\111\099\105\116\121"then local U,f=posvel(M,n,o,H,y,V,j-u)return f elseif f=="\116\112"or f=="\116"or f=="\116\097\114\103\101\116\112\111\115\105\116\105\111\110"then local U,f=targposvel(y,V,j-u)return U elseif f=="\116\118"or f=="\116\097\114\103\101\116\118\101\108\111\099\105\116\121"then local U,f=targposvel(y,V,j-u)return f elseif f=="\100"or f=="\100\097\109\112\101\114"then return M elseif f=="\115"or f=="\115\112\101\101\100"then return n else error("\110\111 \118\097\108\117\101 "..(tostring(f).." \101\120\105\115\116\115"))end end function l.__newindex(U,f,j)local W=tick()if f=="\112"or f=="\112\111\115\105\116\105\111\110"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=j,f y,V=l,i elseif f=="\118"or f=="\118\101\108\111\099\105\116\121"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,j y,V=l,i elseif f=="\116\112"or f=="\116"or f=="\116\097\114\103\101\116\112\111\115\105\116\105\111\110"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,f y,V=j,i elseif f=="\116\118"or f=="\116\097\114\103\101\116\118\101\108\111\099\105\116\121"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,f y,V=l,j elseif f=="\100"or f=="\100\097\109\112\101\114"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,f y,V=l,i M=j elseif f=="\115"or f=="\115\112\101\101\100"then local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,f y,V=l,i n=j elseif f=="\097"or f=="\097\099\099\101\108\101\114\097\116\105\111\110"then u=tick()local U,f=posvel(M,n,o,H,y,V,W-u)local l,i=targposvel(y,V,W-u)o,H=U,f+j y,V=l,i else error("\110\111 \118\097\108\117\101 "..(tostring(f).." \101\120\105\115\116\115"))end u=W end return setmetatable(W,l)end return Spring

function Spring.new(initial, td, ts)
	local d=td or 1
	local s=ts or 1
	local p0=initial or 0
	local v0=0*p0
	local p1=p0
	local v1=v0
	local t0=tick()

	local selfmeta={}
	local meta={}

	function selfmeta.getpv()
		return posvel(d,s,p0,v0,p1,v1,tick()-t0)
	end

	function selfmeta.setpv(p,v)
		local time=tick()
		local tp,tv=targposvel(p1,v1,time-t0)
		p0,v0=p,v
		p1,v1=tp,tv
		t0=time
	end

	function selfmeta.settargetpv(tp,tv)
		local time=tick()
		local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
		p0,v0=p,v
		p1,v1=tp,tv
		t0=time
	end

	function selfmeta:accelerate(a)
		local time=tick()
		local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
		local tp,tv=targposvel(p1,v1,time-t0)
		p0,v0=p,v+a
		p1,v1=tp,tv
		t0=time
	end

	function meta.__index(_self,index)
		local time=tick()
		if index=="p" or index=="position" then
			local p,_v=posvel(d,s,p0,v0,p1,v1,time-t0)
			return p
		elseif index=="v" or index=="velocity" then
			local _p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			return v
		elseif index=="tp" or index=="t" or index=="targetposition" then
			local tp,_tv=targposvel(p1,v1,time-t0)
			return tp
		elseif index=="tv" or index=="targetvelocity" then
			local _tp,tv=targposvel(p1,v1,time-t0)
			return tv
		elseif index=="d" or index=="damper" then
			return d
		elseif index=="s" or index=="speed" then
			return s
		else
			error("no value "..tostring(index).." exists")
		end
	end

	function meta.__newindex(_self,index,value)
		local time=tick()
		if index=="p" or index=="position" then
			local _p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=value,v
			p1,v1=tp,tv
		elseif index=="v" or index=="velocity" then
			local p,_v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=p,value
			p1,v1=tp,tv
		elseif index=="tp" or index=="t" or index=="targetposition" then
			local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local _tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=p,v
			p1,v1=value,tv
		elseif index=="tv" or index=="targetvelocity" then
			local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,_tv=targposvel(p1,v1,time-t0)
			p0,v0=p,v
			p1,v1=tp,value
		elseif index=="d" or index=="damper" then
			local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=p,v
			p1,v1=tp,tv
			d=value
		elseif index=="s" or index=="speed" then
			local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=p,v
			p1,v1=tp,tv
			s=value
		elseif index=="a" or index=="acceleration" then
			t0=tick()
			local p,v=posvel(d,s,p0,v0,p1,v1,time-t0)
			local tp,tv=targposvel(p1,v1,time-t0)
			p0,v0=p,v+value
			p1,v1=tp,tv
		else
			error("no value "..tostring(index).." exists")
		end
		t0=time
	end

	return setmetatable(selfmeta,meta)
end


return Spring
