Share Your Code

Implementing Lightning Effects in Corona

Posted by sithu1986, Posted on August 26, 2011

GitHub URL: 
https://github.com/astk/Corona-lua-lightning

I've implemented the lightning effect in Corona SDK using Lua and the algorithm as described in the following article.
http://drilian.com/2009/02/25/lightning-bolts/

The result can be seen here:
http://www.youtube.com/watch?v=JnRxfs87ppk

The vector class I used in this file was extended from the following gist:
https://gist.github.com/1006414

Write something like this to create a new lightning. You need to pass the starting and target positions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local util = require("util")
 
_W = display.contentWidth
_H = display.contentHeight
 
gameStates = { menu = 0, pause = 1, gameOver = 2, levelSelection = 3, lightning = 4, inGame = 5};
currentState = gameStates["menu"];
 
function onTouch(event)
        if (event.phase == "began") then
                print("creating lightning...")
                util.newLightning(_W/2,_H/2,event.x,event.y)
                _G.currentState = gameStates["lightning"]
        end
end
 
Runtime:addEventListener("touch", onTouch)

Thanks,
Aung Sithu
http://rivaledge.sg


Replies

open768
User offline. Last seen 42 weeks 5 days ago. Offline
Joined: 6 Jul 2011

nice :) would be good to have a bitmap library in corona to be able to do plasma effects.

my build on your ontouch :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function onTouch(event)
        local iDegree, iloop, iStrikes, ix,iy
        
        if (event.phase == "began") then
                print("creating lightning...")
                
                iStrikes = math.random(2,10) -- max ten lightning strikes
                for iLoop = 1,iStrikes do
                        iDegree = math.random(1,360)    -- angle of lightning
                        ix = math.sin(math.rad(iDegree) ) * _W/2
                        iy = math.cos(math.rad(iDegree) ) * _H/2
                        util.newLightning(event.x,event.y, event.x+ix, event.y + iy)
                end
                
                _G.currentState = gameStates["lightning"]
        end
end

sithu1986
User offline. Last seen 1 year 13 weeks ago. Offline
Joined: 3 Aug 2010

Cool. :D

Cheers!

open768
User offline. Last seen 42 weeks 5 days ago. Offline
Joined: 6 Jul 2011

I'm confused how the lightningupdate event knows what startPointX,startPointY, targetX, targetY are as these are not stored.

and each invocation of newLightning will have different values - so it works, but should it?

:-?

a.lepel
User offline. Last seen 10 hours 6 min ago. Offline
Joined: 28 Nov 2011

I really like! Thanks!