# Is there any algorithm for creating 3D lightning?

Is there any algorithm for generating lightning?
Lets say, you input the starting and ending coordinates (x,y,z) and out comes an array of coordinates to show where the lightning goes.
It doesn't have to be scientifically accurate but as long it looks random and unpredictable

EDIT: I found this, but can't figure out how to put this in to C/++ code. There is a 2D example provided but I want 3D :O

EDIT2: The output might be something like this:

struct coord3 { int x, y, z; };

struct line
{
unsigned char lumo;
coord3 start;
coord3 end;
};

line* lightning = new line[n]; // each element of the array would be a line that
// needs to be drawn for the lightning so the
// lightning can "fork" and can have different
// brightnesses

• The sample you found will bleed you dry in 3d. It actually tries to solve the electrical equations. – drxzcl Aug 22 '10 at 15:38
• Eww. But I still want to use it :O – 小太郎 Aug 23 '10 at 3:08

If you need a set begin and endpoint then you'll have to generate the points inbetween. Start by randomly cutting the line (begin - end) in a number of segments. For each point generate a random amplitude within a given range and scale it (or scale your random range) so the points closer to the beginning and end can't displace as much. Next for each segment endpoint do a random to see if a fork should be created there, give it a random number of segments and repeat the same process for the fork. Forks can have a fixed endpoint as well if you want. Nodes should have a random value to decide how long they should be kept the same. This way you'll get lightning going from a to b with some points staying in place a few frames longer, some a few frames shorter and some random forking.

• Could you link to an example of this in action? – 小太郎 Aug 23 '10 at 1:09
• youtube.com/watch?v=-fiZtLkOIQ4&feature=related – Kaj Aug 23 '10 at 2:02
• Hmm. Looks okay... but it doesn't really look "lightningty" – 小太郎 Aug 23 '10 at 6:38
• I guess you'd have to add more (and thinner) forks, more random segment lengths, play with the delay, select your (forked) endpoints wisely. Sorry, it was the closest I could think of, and it was a couple of years ago - I bet it could be improved, but at least it's a starting point? – Kaj Aug 23 '10 at 7:07
• youtube.com/watch?v=lAPTrlTq0q4&feature=related around the 8 second mark...looks lightningey to me? Alternatively, there are some vids on the net of highly slomo lightning, analyze and try to break it down. – Kaj Aug 23 '10 at 7:44

Why not try a random walk? You could also have a small random chance of branching at each step.

Ah, you wanted ending coordinates too. Then just weight your directional probability based on how many steps you are from the ending point. The fewer steps remaining, the more chance of going toward the desired endpoint.

• Too random. I don't think it would look as much like lightning. I could try it though – 小太郎 Aug 22 '10 at 12:54
• This is basically what I've done before in 2D; it worked out fairly well. The trick is to severely limit the deflection angle at each 'step' - lightning never really goes "sideways" so you're looking for something like 5 to 10 degrees off the current direction. I also forked out some very small branches of the lightning occasionally but that got kind of expensive. After about halfway you can start bringing it back around; it actually doesn't look bad if you just traverse the angles in reverse order so you end up with a symmetrical lightning fork. – ravuya Aug 22 '10 at 15:35

I'd personally try with L-System. I wouldn't be surprised if there are examples you could find with google.