Physics Based Flames

Looks cool, right?  I’ve always been in love with the idea of trying to put real physics into games- and flames was my first target.  It turned out to be a lot of work (had to write my own fast fourier transform!) but being foolish and optimistic, I decided to give it a shot.

Modern GPU’s are astonishingly powerful – even low end ones.  Processing clock speeds ground to a halt around 2005, but GPU’s don’t have to run fast- they run in parallel.  If you can take your algorithm and represent it as a sequence of texture transformations, you can squeeze a lot of beauty out of those little processing units.

And I’ve tried to do so.  The flames engine represents the space around a burning object as a field of little cells that can hold varying amounts of fuel, air, and heat at any given point.  And there is a differential equation defined in which if there is enough of each element at a point in space, a reaction occurs in which fuel is combusted with air and heat is produced.  The field itself behaves like a viscous fluid, and the forces within the field depend on things like heat, buoyancy and pressure gradient, and also the overall motion of the object itself.  There are all kinds of ways to customize this- you can vary the fuel rate, cooling rate, light spectrum as a function of heat, and so on:

(The method by which the space behaves as a realtime fluid is based on books and research by Jos Stam of the University of Toronto.  It’s a wonderfully elegant algorithm, and extendable in all sorts of ways.)

Now here’s where I’m going to let you in on a secret:  the flames algorithm is not *quite* 3d.  It’s actually 2.5D – two dimensions plus a depth coordinate.  It’s one thing to ask a GPU to simulate a realtime fluid of 1024×1024 pixels, but it’s another one altogether to ask for 1024x1024x1024 pixels – a thousandfold increase!  But, the z coordinate trick works really well and it reads onscreen as 3D quite nicely.  Take a look:

This little snippet of a burning fireball represents more time and energy than I’d like to admit.  I haven’t yet done a full usage tutorial on the flames effect, but expect to see one in the next few days.  Enjoy!

-The Falling Frog


Physics Based Lightning

A couple years back, a friend of mine and myself, with a couple of other people, talked for a while about creating an RPG game using the Unity engine.  Like many such projects, it never really got off the ground – but I decided to take a couple of the effects I designed and put them up on the Unity Asset Store, so that others might benefit from the work.  Here’s the first one in a series I’m calling the Physics Based effects series- Physics Based Lightning.

Why Lightning?

I did look around the asset store before I started to see if there was already a nice lightning effect I could use for our game – but all the ones I saw fell short in some way.  Here’s what I was looking for in a nice effect:

-Should have controllable forkedness

-Should look volumetric, not flat, without any visible knobs or artifacts where the segments are joined together.

-Should be fractal both in the path and the fork tree.

-The bolt should animate smoothly, smoothly tracking targets and flicking around- the bolt should not appear as a sequence of unrelated lightning bolts but a continuously animating one.

And this is the effect I came up with.  I actually ended up using the equation for the electric field of a line charge to draw the lightning segments, which gives you a couple nice things.  First, it looks right.  Second, since electric fields add together linearly, two line charges will join together perfectly and look exactly the same as a single, longer line charge, so you don’t get artifacts.

As far as the forking goes, the physically correct way to do it would have been to create a field in three dimensions defining the exact electric potential at each point based on the shape of the source and target fields.  That might be possible on some future hardware set, but not today, so I approximated the field as being two point charges, one at the source and one at the target, and I added some fractal directional noise to the position at the source and target points.

The result- well, I think I can say it’s the best lightning I’ve ever seen, even in a AAA game!  But you can judge for yourself.

I also made a nice tutorial video going over how to use the effect.  Enjoy!

-The Falling Frog

Blog at

Up ↑