welcome: please sign in
LightningToy

http://chris.ill-logic.com/images/lightning2.png

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.)

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.

LightningToy (last edited 2010-04-24 09:29:43 by localhost)