Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --3Dcl.lua
- --cloth falls on a round table
- --code by Siapran
- --09.04.13
- --USER VALUES
- --you may modify these to fit your needs
- local g=1/32 --gravity
- local r=49/100 --elasticity (MUST BE UNDER 1/2)
- local f=49/50 --friction
- local t=5 --table radius
- --end of user values
- for a=1,4,1 do
- misc.contrast(-1)
- end
- local print=nbdraw.print
- local cursor=nbdraw.cursor
- local line=nbdraw.line
- local pixel=nbdraw.pixel
- local page=nbdraw.copypage
- local wait=misc.wait
- --REQUIRES module math for trigonometric functions.
- --Module math defines cos(), sin(), pi, pisurdeux, pisurquatre.
- --you may replace the following code with your own
- --cos() and sin() functions, or a module that defines these functions.
- clear nil
- cursor(1,1)
- print "loading module"
- func,err=misc.modload("math")
- if func==nil then
- clear nil
- print(err)
- repeat until key(36)
- end
- func()
- --end of module loading section
- clear nil
- cursor(1,1)
- print "preparing engine"
- local coord={}
- local clothx={}
- local clothy={}
- local clothz={}
- local clothxm={}
- local clothym={}
- local clothzm={}
- local mode=0
- local az=pi-pisurquatre --replace with value if needed (corresponds to pi*3/4)
- local ay=pisurquatre --replace with value if needed (corresponds to pi/8)
- local p=0
- local x=0
- local xb=0
- local xc=0
- local y=0
- local yb=0
- local z=0
- local zb=0
- local crz=0
- local srz=0
- local cry=0
- local sry=0
- local count=0
- local lm
- local vtwo=sqrt(2)
- p=0
- for i=-7,7,1 do
- for j=-7,7,1 do
- p=p+1
- clothx[p]=i
- clothy[p]=j
- clothz[p]=3
- clothxm[p]=0
- clothym[p]=0
- clothzm[p]=0
- end
- end
- local function traction(ia,ja,ib,jb,l) --calculates the traction exerced by a plot on another relative to its relaxed length
- lm=nil --length measured
- coord[1]=clothx[ib+15*(jb-1)]-clothx[ia+15*(ja-1)] --get 3D vector AB
- coord[2]=clothy[ib+15*(jb-1)]-clothy[ia+15*(ja-1)]
- coord[3]=clothz[ib+15*(jb-1)]-clothz[ia+15*(ja-1)]
- lm=sqrt(coord[1]^2+coord[2]^2+coord[3]^2) --measure AB
- coord[1]=coord[1]*(lm-l)*r/lm --apply elasticity
- coord[2]=coord[2]*(lm-l)*r/lm
- coord[3]=coord[3]*(lm-l)*r/lm
- clothxm[ia+15*(ja-1)]=clothxm[ia+15*(ja-1)]+coord[1] --write out
- clothym[ia+15*(ja-1)]=clothym[ia+15*(ja-1)]+coord[2]
- clothzm[ia+15*(ja-1)]=clothzm[ia+15*(ja-1)]+coord[3]
- end
- count=0
- clear nil
- repeat
- if key(3)then az=az-(pi/16)end --replace with value if needed
- if key(1)then az=az+(pi/16)end
- if key(7)and ay>-pisurdeux then ay=ay-(pi/16)end --replace with value if needed (corresponds to pi/2)
- if key(2)and ay<pisurdeux then ay=ay+(pi/16)end
- if key(5) then
- if mode==0 then
- mode=1
- else
- mode=0
- end
- while key(5) do
- wait(1)
- end
- end
- crz=cos(az) --compute rotation matrix
- srz=sin(az)
- cry=cos(ay)
- sry=sin(ay)
- if mode==0 then --if mode==compute mode
- cursor(1,1)
- print(count) --show number of frames computed
- for a=1,15,1 do --compute new momentums
- for b=1,15,1 do
- if sqrt((a-8)^2+(b-8)^2)>t then --if the plot is not on the table
- for c=-1,1,1 do --explore adjacent plots
- for d=-1,1,1 do
- if c+a>0 and c+a<16 and d+b>0 and d+b<16 then --clipping
- if c%2~=d%2 then
- traction(a,b,a+c,b+d,1) --sides
- else
- if c~=0 and d~=0 then
- traction(a,b,a+c,b+d,vtwo) --diagonals
- end
- end
- end
- end
- end
- end
- end
- end
- for a=1,15,1 do --apply new momentums
- for b=1,15,1 do
- p=a+15*(b-1)
- if sqrt((a-8)^2+(b-8)^2)>t then --if plot is not on the table
- clothzm[p]=clothzm[p]-g --apply gravity
- clothxm[p]=clothxm[p]*f --apply friction
- clothym[p]=clothym[p]*f
- clothzm[p]=clothzm[p]*f
- clothx[p]=clothx[p]+clothxm[p] --update coordinates
- clothy[p]=clothy[p]+clothym[p]
- clothz[p]=clothz[p]+clothzm[p]
- end
- x=clothx[p]*crz-clothy[p]*srz --rotate plot
- y=clothx[p]*srz+clothy[p]*crz
- xc=x*cry-clothz[p]*sry
- z=x*sry+clothz[p]*cry
- x=xc
- pixel(y/(x+50)*250+64,64-(z/(x+50)*250+32),1,1) --draw plot
- end
- end
- count=count+1
- page(1,0)
- clear(1)
- else --if mode==wireframe mode
- clear(1)
- for a=1,15,1 do
- p=a
- xb=clothx[p]*crz-clothy[p]*srz
- yb=clothx[p]*srz+clothy[p]*crz
- xc=xb*cry-clothz[p]*sry
- zb=xb*sry+clothz[p]*cry
- xb=xc
- for b=2,15,1 do
- p=a+15*(b-1)
- x=clothx[p]*crz-clothy[p]*srz
- y=clothx[p]*srz+clothy[p]*crz
- xc=x*cry-clothz[p]*sry
- z=x*sry+clothz[p]*cry
- x=xc
- line(yb/(xb+50)*250+64,64-(zb/(xb+50)*250+32),y/(x+50)*250+64,64-(z/(x+50)*250+32),1)
- xb=x
- yb=y
- zb=z
- end
- end
- for b=1,15,1 do
- p=(b-1)*15+1
- xb=clothx[p]*crz-clothy[p]*srz
- yb=clothx[p]*srz+clothy[p]*crz
- xc=xb*cry-clothz[p]*sry
- zb=xb*sry+clothz[p]*cry
- xb=xc
- for a=2,15,1 do
- p=a+15*(b-1)
- x=clothx[p]*crz-clothy[p]*srz
- y=clothx[p]*srz+clothy[p]*crz
- xc=x*cry-clothz[p]*sry
- z=x*sry+clothz[p]*cry
- x=xc
- line(yb/(xb+50)*250+64,64-(zb/(xb+50)*250+32),y/(x+50)*250+64,64-(z/(x+50)*250+32),1)
- xb=x
- yb=y
- zb=z
- end
- end
- page(1,0)
- end
- until key(36)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement