Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screen = {}
- screen.width = 600
- screen.height = 300
- nodeList = {}
- nodeRadius = 2
- numNodes = 50
- dt=0.03
- for i=1, numNodes do
- local node = {}
- node.x = math.random(screen.width)
- node.y = math.random(screen.height)
- local v = math.random(200)
- local th = math.random(2*math.pi)
- node.vx = v*math.cos(th)
- node.vy = v*math.sin(th)
- nodeList[i] = node
- end
- phyClock = {}
- phyClock.curTime = 0
- springConstant = 75
- maxDist = 120
- curTime = 0
- function newprintpixel(x,y,r,g,b)
- if x>0 and x0 and y< 184 then
- tpt.drawpixel( x,y)
- end
- end
- function update()
- curTime = curTime + dt
- while phyClock.curTime < dt do
- phyClock.curTime = phyClock.curTime + 0.01
- for i=1, numNodes do
- local A = nodeList[i]
- for j=i+1, numNodes do
- local B = nodeList[j]
- local dx = (A.x-B.x)
- local dy = (A.y-B.y)
- local dist = dx^2 + dy^2
- if dist < maxDist*maxDist then
- local ang = math.atan2(dy, dx)
- local falloff = 1-dist/(maxDist*maxDist)
- local F = springConstant*falloff
- A.vx = A.vx - F*math.cos(ang)*0.01
- A.vy = A.vy - F*math.sin(ang)*0.01
- B.vx = B.vx + F*math.cos(ang)*0.01
- B.vy = B.vy + F*math.sin(ang)*0.01
- end
- end
- end
- for i=1, numNodes do
- local node = nodeList[i]
- node.x = node.x + node.vx*0.01
- node.y = node.y + node.vy*0.01
- if node.x+nodeRadius < 0 then
- node.x = screen.width + nodeRadius
- elseif node.x-nodeRadius > screen.width then
- node.x = -nodeRadius
- end
- if node.y+nodeRadius < 0 then
- node.y = screen.height + nodeRadius
- elseif node.y-nodeRadius > screen.height then
- node.y = -nodeRadius
- end
- end
- end
- phyClock.curTime = phyClock.curTime - dt
- end
- function HSL(h, s, l, a)
- if s<=0 then
- return l,l,l,a
- end
- h, s, l = h/256*6, s/255, l/255
- local c = (1-math.abs(2*l-1))*s
- local x = (1-math.abs(h%2-1))*c
- local m,r,g,b = (l-.5*c), 0,0,0
- if h < 1 then
- r,g,b = c,x,0
- elseif h < 2 then
- r,g,b = x,c,0
- elseif h < 3 then
- r,g,b = 0,c,x
- elseif h < 4 then
- r,g,b = 0,x,c
- elseif h < 5 then
- r,g,b = x,0,c
- else
- r,g,b = c,0,x
- end
- return (r+m)*255,(g+m)*255,(b+m)*255,a
- end
- function draw()
- for i=1, numNodes do
- local node = nodeList[i]
- newprintpixel( node.x, node.y,0, 255, 255)
- for j=i+1, numNodes do
- local b = nodeList[j]
- local dist = (node.x-b.x)^2 + (node.y-b.y)^2
- local falloff = math.max((1-dist/(maxDist*maxDist)), 0)
- if falloff > 0 then
- tpt.drawline(node.x, node.y, b.x, b.y,HSL((falloff*255+curTime*32)%255, 255, 128, falloff*255))
- end
- end
- end
- end
- tpt.register_step(update)
- tpt.register_step(draw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement