Advertisement
jacksonmj

electronic_steve's script from http://tpt.io/.272754

Aug 5th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. local screen = {}
  2. screen.width = 600
  3. screen.height = 300
  4. nodeList = {}
  5. nodeRadius = 2
  6. numNodes = 50
  7. dt=0.03
  8. for i=1, numNodes do
  9.     local node = {}
  10.     node.x = math.random(screen.width)
  11.     node.y = math.random(screen.height)
  12.     local v = math.random(200)
  13.     local th = math.random(2*math.pi)
  14.     node.vx = v*math.cos(th)
  15.     node.vy = v*math.sin(th)
  16.     nodeList[i] = node
  17. end
  18. phyClock = {}
  19. phyClock.curTime = 0
  20. springConstant = 75
  21. maxDist = 120
  22. curTime = 0
  23.  
  24. function newprintpixel(x,y,r,g,b)
  25.     if x>0 and x0 and y< 184 then
  26.         tpt.drawpixel( x,y)
  27.     end
  28. end
  29.  
  30. function update()
  31.     curTime = curTime + dt
  32.     while phyClock.curTime < dt do
  33.         phyClock.curTime = phyClock.curTime + 0.01
  34.         for i=1, numNodes do
  35.             local A = nodeList[i]
  36.             for j=i+1, numNodes do
  37.                 local B = nodeList[j]
  38.                 local dx = (A.x-B.x)
  39.                 local dy = (A.y-B.y)
  40.                 local dist = dx^2 + dy^2
  41.                 if dist < maxDist*maxDist then
  42.                     local ang = math.atan2(dy, dx)
  43.                     local falloff = 1-dist/(maxDist*maxDist)
  44.                     local F = springConstant*falloff
  45.                     A.vx = A.vx - F*math.cos(ang)*0.01
  46.                     A.vy = A.vy - F*math.sin(ang)*0.01
  47.                     B.vx = B.vx + F*math.cos(ang)*0.01
  48.                     B.vy = B.vy + F*math.sin(ang)*0.01
  49.                 end
  50.             end
  51.         end
  52.         for i=1, numNodes do
  53.             local node = nodeList[i]
  54.             node.x = node.x + node.vx*0.01
  55.             node.y = node.y + node.vy*0.01
  56.             if node.x+nodeRadius < 0 then
  57.                 node.x = screen.width + nodeRadius
  58.             elseif node.x-nodeRadius > screen.width then
  59.                 node.x = -nodeRadius
  60.             end
  61.             if node.y+nodeRadius < 0 then
  62.                 node.y = screen.height + nodeRadius
  63.             elseif node.y-nodeRadius > screen.height then
  64.                 node.y = -nodeRadius
  65.             end
  66.         end
  67.     end
  68.     phyClock.curTime = phyClock.curTime - dt
  69. end
  70.  
  71. function HSL(h, s, l, a)
  72.     if s<=0 then
  73.         return l,l,l,a
  74.     end
  75.     h, s, l = h/256*6, s/255, l/255
  76.     local c = (1-math.abs(2*l-1))*s
  77.     local x = (1-math.abs(h%2-1))*c
  78.     local m,r,g,b = (l-.5*c), 0,0,0
  79.     if h < 1 then
  80.         r,g,b = c,x,0
  81.     elseif h < 2 then
  82.         r,g,b = x,c,0
  83.     elseif h < 3 then
  84.         r,g,b = 0,c,x
  85.     elseif h < 4 then
  86.         r,g,b = 0,x,c
  87.     elseif h < 5 then
  88.         r,g,b = x,0,c
  89.     else
  90.         r,g,b = c,0,x
  91.     end
  92.     return (r+m)*255,(g+m)*255,(b+m)*255,a
  93. end
  94.  
  95. function draw()
  96.     for i=1, numNodes do
  97.         local node = nodeList[i]
  98.         newprintpixel( node.x, node.y,0, 255, 255)
  99.         for j=i+1, numNodes do
  100.             local b = nodeList[j]
  101.             local dist = (node.x-b.x)^2 + (node.y-b.y)^2
  102.             local falloff = math.max((1-dist/(maxDist*maxDist)), 0)
  103.             if falloff > 0 then
  104.                 tpt.drawline(node.x, node.y, b.x, b.y,HSL((falloff*255+curTime*32)%255, 255, 128, falloff*255))
  105.             end
  106.         end
  107.     end
  108. end
  109.  
  110. tpt.register_step(update)
  111. tpt.register_step(draw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement