--rubb.lua --elastic bond simulator --code by Siapran --19.02.2013 --you may modify the following values to suit your needs: local n=2 --number of modules (>0) local g=1/10 --gravity local r=1/32 --elasticity local f=99/100 --friction --end of user values local line=graydraw.line local rect=graydraw.rect local wait=misc.wait local x={} --coordinates local y={} local xm={} --momentum local ym={} n=n+1 for a=1,n,1 do --initialise modules x[a]=64 y[a]=32 xm[a]=0 ym[a]=0 end graydraw.setcolor(true) --initiate grayscale mode for a=1,4,1 do --adjust contrast misc.contrast(1) end repeat --main loop clear nil --clear buffer if key(37) then x[1]=x[1]-1 end --move holding point if key(40) then x[1]=x[1]+1 end if key(38) then y[1]=y[1]-1 end if key(39) then y[1]=y[1]+1 end if x[1]<1 then x[1]=1 end --clipping if x[1]>128 then x[1]=128 end if y[1]<1 then y[1]=1 end if y[1]>64 then y[1]=64 end for a=2,n,1 do --compute elastic tention in one way xm[a]=xm[a]+(x[a-1]-x[a])*r ym[a]=ym[a]+(y[a-1]-x[a])*r end if n<2 then --compute elastic tention in the other way for a=2,n-1,1 do xm[a]=xm[a]+(x[a+1]-x[a])*r ym[a]=ym[a]+(y[a+1]-x[a])*r end end for a=2,n,1 do --apply gravity and friction, then computer changes ym[a]=ym[a]+g xm[a]=xm[a]*f ym[a]=ym[a]*f x[a]=x[a]+xm[a] y[a]=y[a]+ym[a] line(x[a-1],y[a-1],x[a],y[a],4) --draw elastic bond rect(x[a]-3,y[a]-3,x[a]+3,y[a]+3,4,0) --draw module box end line(x[1]-3,y[1],x[1]+3,y[1],4) line(x[1],y[1]-3,x[1],y[1]+3,4) refresh until key(36) --wait for key [EXIT] while key(36) do wait(1) end