Overview
A little demo of a lightning effect that I was experimenting with for a game. (The screenshot doesn't do it justice -- imagine that lightning bolt in 3D, rotating around.)
Download Lightning v0.2 for Windows (click to shoot bolts!)
View the source code (you can run this on Linux or MacOSX if you have Python with PyOpenGL installed)
Algorithm Description
The lightning bolt algorithm is very simple. It basically has a starting energy, and every time the bolt zigs or zags, it loses a little bit of energy. Also, when the bolt forks into two bolts, each bolt only gets part of the energy of the parent bolt.
Here's how it works in pseudo-code:
function recursive_bolt(position, energy):
newposition = position + [random vector, mostly downwards]
color = [interpolate between white and blue based on energy]
draw_line(position, newposition, color)
fork = True if rand() > 0.8 # fork 20% of the time
if fork:
recursive_bolt(newposition, energy*0.3)
recursive_bolt(newposition, energy*0.6)
else:
recursive_bolt(newposition, energy*0.9)The actual implementation uses a class called Bolt instead of a function, but it's the same idea.
