Jake Gundersen

Posted by fattjake8, Posted on October 29, 2011

0 votes
GitHub URL: 
https://github.com/fattjake/CoronaLightning

An implementation of the recursive algorithm found on krazydad.com to create lightning/electricity bolts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local curDetail = 1
 
lines = display.newGroup()
 
function drawLightning(x1, y1, x2, y2, displace, r, g, b)
        if displace < curDetail then
 
                --glow around lightning
                for i = 1, 4 do
                        line = display.newLine(x1, y1, x2, y2)
                        line:setColor(r, g, b)
                        line.width = 20 / i
                        line.alpha = 0.05 * i
                        lines:insert(line)
                end
 
                --bolt itself
                line = display.newLine(x1, y1, x2, y2)
                line:setColor(r, g, b)
                line.width = 2
                lines:insert(line)
 
        else
                local midx = (x2+x1)/2
                local midy = (y2+y1)/2
                midx = midx + (math.random(0, 1) - 0.5)*displace
                midy = midy + (math.random(0, 1) - 0.5)*displace
                drawLightning(x1, y1, midx, midy, displace/2, r, g, b)
                drawLightning(x2, y2, midx, midy, displace/2, r, g, b)
        end
end
 
local function ontouch(e)
        if e.phase == 'began' then
                drawLightning(0, 0, e.x, e.y, 100, 255, 255, 255)
                timer.performWithDelay(500, function() display.getCurrentStage():remove(1); lines = display.newGroup() end)
        end
end
 
Runtime:addEventListener('touch', ontouch)


Replies

canupa.com's picture
canupa.com
User offline. Last seen 21 hours 2 min ago. Offline
Joined: 20 Jun 2011

jake, that's awesome!
krazydad FTW!

iNSERT.CODE's picture
iNSERT.CODE
User offline. Last seen 1 day 22 hours ago. Offline
Joined: 7 Jul 2011

Excellent stuff. Could really be useful thanks.