Advertisement
Siapran

3Dcl.lua

Jun 24th, 2013
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. --3Dcl.lua
  2. --cloth falls on a round table
  3. --code by Siapran
  4. --09.04.13
  5.  
  6. --USER VALUES
  7. --you may modify these to fit your needs
  8. local g=1/32 --gravity
  9. local r=49/100 --elasticity (MUST BE UNDER 1/2)
  10. local f=49/50 --friction
  11. local t=5 --table radius
  12. --end of user values
  13.  
  14. for a=1,4,1 do
  15.  misc.contrast(-1)
  16. end
  17.  
  18. local print=nbdraw.print
  19. local cursor=nbdraw.cursor
  20. local line=nbdraw.line
  21. local pixel=nbdraw.pixel
  22. local page=nbdraw.copypage
  23. local wait=misc.wait
  24.  
  25. --REQUIRES module math for trigonometric functions.
  26. --Module math defines cos(), sin(), pi, pisurdeux, pisurquatre.
  27. --you may replace the following code with your own
  28. --cos() and sin() functions, or a module that defines these functions.
  29. clear nil
  30. cursor(1,1)
  31. print "loading module"
  32. func,err=misc.modload("math")
  33. if func==nil then
  34.  clear nil
  35.  print(err)
  36.  repeat until key(36)
  37. end
  38. func()
  39. --end of module loading section
  40.  
  41. clear nil
  42. cursor(1,1)
  43. print "preparing engine"
  44.  
  45. local coord={}
  46. local clothx={}
  47. local clothy={}
  48. local clothz={}
  49. local clothxm={}
  50. local clothym={}
  51. local clothzm={}
  52.  
  53. local mode=0
  54. local az=pi-pisurquatre --replace with value if needed (corresponds to pi*3/4)
  55. local ay=pisurquatre --replace with value if needed (corresponds to pi/8)
  56. local p=0
  57. local x=0
  58. local xb=0
  59. local xc=0
  60. local y=0
  61. local yb=0
  62. local z=0
  63. local zb=0
  64. local crz=0
  65. local srz=0
  66. local cry=0
  67. local sry=0
  68. local count=0
  69. local lm
  70. local vtwo=sqrt(2)
  71.  
  72. p=0
  73. for i=-7,7,1 do
  74.  for j=-7,7,1 do
  75.   p=p+1
  76.   clothx[p]=i
  77.   clothy[p]=j
  78.   clothz[p]=3
  79.   clothxm[p]=0
  80.   clothym[p]=0
  81.   clothzm[p]=0
  82.  end
  83. end
  84.  
  85. local function traction(ia,ja,ib,jb,l) --calculates the traction exerced by a plot on another relative to its relaxed length
  86.  lm=nil --length measured
  87.  coord[1]=clothx[ib+15*(jb-1)]-clothx[ia+15*(ja-1)] --get 3D vector AB
  88.  coord[2]=clothy[ib+15*(jb-1)]-clothy[ia+15*(ja-1)]
  89.  coord[3]=clothz[ib+15*(jb-1)]-clothz[ia+15*(ja-1)]
  90.  lm=sqrt(coord[1]^2+coord[2]^2+coord[3]^2) --measure AB
  91.  coord[1]=coord[1]*(lm-l)*r/lm --apply elasticity
  92.  coord[2]=coord[2]*(lm-l)*r/lm
  93.  coord[3]=coord[3]*(lm-l)*r/lm
  94.  clothxm[ia+15*(ja-1)]=clothxm[ia+15*(ja-1)]+coord[1] --write out
  95.  clothym[ia+15*(ja-1)]=clothym[ia+15*(ja-1)]+coord[2]
  96.  clothzm[ia+15*(ja-1)]=clothzm[ia+15*(ja-1)]+coord[3]
  97. end
  98.  
  99. count=0
  100. clear nil
  101.  
  102. repeat
  103.  if key(3)then az=az-(pi/16)end --replace with value if needed
  104.  if key(1)then az=az+(pi/16)end
  105.  if key(7)and ay>-pisurdeux then ay=ay-(pi/16)end --replace with value if needed (corresponds to pi/2)
  106.  if key(2)and ay<pisurdeux then ay=ay+(pi/16)end
  107.  if key(5) then
  108.   if mode==0 then
  109.    mode=1
  110.   else
  111.    mode=0
  112.   end
  113.   while key(5) do
  114.    wait(1)
  115.   end
  116.  end
  117.  crz=cos(az) --compute rotation matrix
  118.  srz=sin(az)
  119.  cry=cos(ay)
  120.  sry=sin(ay)
  121.  if mode==0 then --if mode==compute mode
  122.   cursor(1,1)
  123.   print(count) --show number of frames computed
  124.   for a=1,15,1 do --compute new momentums
  125.    for b=1,15,1 do
  126.     if sqrt((a-8)^2+(b-8)^2)>t then --if the plot is not on the table
  127.      for c=-1,1,1 do --explore adjacent plots
  128.       for d=-1,1,1 do
  129.        if c+a>0 and c+a<16 and d+b>0 and d+b<16 then --clipping
  130.         if c%2~=d%2 then
  131.          traction(a,b,a+c,b+d,1) --sides
  132.         else
  133.          if c~=0 and d~=0 then
  134.           traction(a,b,a+c,b+d,vtwo) --diagonals
  135.          end
  136.         end
  137.        end
  138.       end
  139.      end
  140.     end
  141.    end
  142.   end
  143.   for a=1,15,1 do --apply new momentums
  144.    for b=1,15,1 do
  145.     p=a+15*(b-1)
  146.     if sqrt((a-8)^2+(b-8)^2)>t then --if plot is not on the table
  147.      clothzm[p]=clothzm[p]-g --apply gravity
  148.      clothxm[p]=clothxm[p]*f --apply friction
  149.      clothym[p]=clothym[p]*f
  150.      clothzm[p]=clothzm[p]*f
  151.      clothx[p]=clothx[p]+clothxm[p] --update coordinates
  152.      clothy[p]=clothy[p]+clothym[p]
  153.      clothz[p]=clothz[p]+clothzm[p]
  154.     end
  155.     x=clothx[p]*crz-clothy[p]*srz --rotate plot
  156.     y=clothx[p]*srz+clothy[p]*crz
  157.     xc=x*cry-clothz[p]*sry
  158.     z=x*sry+clothz[p]*cry
  159.     x=xc
  160.     pixel(y/(x+50)*250+64,64-(z/(x+50)*250+32),1,1) --draw plot
  161.    end
  162.   end
  163.   count=count+1
  164.   page(1,0)
  165.   clear(1)
  166.  else --if mode==wireframe mode
  167.   clear(1)
  168.   for a=1,15,1 do
  169.    p=a
  170.    xb=clothx[p]*crz-clothy[p]*srz
  171.    yb=clothx[p]*srz+clothy[p]*crz
  172.    xc=xb*cry-clothz[p]*sry
  173.    zb=xb*sry+clothz[p]*cry
  174.    xb=xc
  175.    for b=2,15,1 do
  176.     p=a+15*(b-1)
  177.     x=clothx[p]*crz-clothy[p]*srz
  178.     y=clothx[p]*srz+clothy[p]*crz
  179.     xc=x*cry-clothz[p]*sry
  180.     z=x*sry+clothz[p]*cry
  181.     x=xc
  182.     line(yb/(xb+50)*250+64,64-(zb/(xb+50)*250+32),y/(x+50)*250+64,64-(z/(x+50)*250+32),1)
  183.     xb=x
  184.     yb=y
  185.     zb=z
  186.    end
  187.   end
  188.   for b=1,15,1 do
  189.    p=(b-1)*15+1
  190.    xb=clothx[p]*crz-clothy[p]*srz
  191.    yb=clothx[p]*srz+clothy[p]*crz
  192.    xc=xb*cry-clothz[p]*sry
  193.    zb=xb*sry+clothz[p]*cry
  194.    xb=xc
  195.    for a=2,15,1 do
  196.     p=a+15*(b-1)
  197.     x=clothx[p]*crz-clothy[p]*srz
  198.     y=clothx[p]*srz+clothy[p]*crz
  199.     xc=x*cry-clothz[p]*sry
  200.     z=x*sry+clothz[p]*cry
  201.     x=xc
  202.     line(yb/(xb+50)*250+64,64-(zb/(xb+50)*250+32),y/(x+50)*250+64,64-(z/(x+50)*250+32),1)
  203.     xb=x
  204.     yb=y
  205.     zb=z
  206.    end
  207.   end
  208.   page(1,0)
  209.  end
  210. until key(36)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement