2D Fire Effect in ActionScript 3

I had a little bit of time today to do some me-coding (as opposed to work-coding), so I knocked up a quick 90’s era 2D fire effect. I’d actually written it C++ and OpenGL the other week and was meaning to transfer it into WebGL so it can run live rather than having a captured video, but my WebGL kung-fu is pretty weak at the moment so just to get it done I translated it to Flash.

How it works

The effect itself is incredibly simple, as the video below explains. You randomly add “hot-spots” to the bottom of the pixel array, then the new temperature value for a pixel is just the average of the pixel and the 3 pixels below it with a small amount subtracted so that the flames “cool”, which you then map to a colour gradient.

Sneaky!

Flash implementation

In the OpenGL version I’d made the colours for the flames into a 1D texture which can be easily interpolated, but I had to find some functions to do that in AS3 as there don’t appear to be 1D textures! Also, getting and converting the colours from uints and hex-codes and stuff was a pain – but I finally nailed it after googling around and pulling functions from various places (referenced in the source).

I think the most important thing I learnt from getting this working in Flash was how to and how NOT to convert colours from uints into red/green/blue components. For example, a lot of code online will use a function like the following to convert red/green/blue values into a 24-bit uint:

The above function looks like it works, and in fact in most cases does work – but if you get single digit values they aren’t zero-padded properly and craziness ensues!

The correct way to convert RGB to uint is by using the two functions below which ensure that values are capped AND zero-padded as appropriate, which means that everything works under all possible conditions:

Wrap up

Overall, it’s a nice simple effect – but it’s pretty CPU intensive. In the above example I’m only using a stage size of 250px by 120px and the framerate takes a hit even then.

I could use a single loop instead of embedded x and y loops, and I could manipulate more colours directly as uints rather than pulling out the RGB components, but it’s prolly not going to improve more than about 10-15%. As usual, there’s source code after the jump, so if you have a play with it and manage to speed it up a bunch, feel free to let me know how you did it!

Cheers!

Download Link: 2D-Fire-Effect.fla (Flash CS4 fla – but CS5 and later will convert on load).

Continue reading 2D Fire Effect in ActionScript 3

Linkage #7: Three Steps Forward, Two Steps Back

  • Paint with liquid fire! And flash! Really rather fun… Flame painter, Fluid painter, Fluid AND Fire Painter!
  • Found: 90% of the Universe – hiding in plain sight. Well, ultraviolet sight. Surely this means the search for dark matter is effectively over?
  • Coke bottles are going square… Rather neat IMHO.
  • Ten things you didn’t know about comets is a bad astronomy article, and in my case was about 80% correct :)
  • Pink Floyd’s Dark Side of the Moon – now in chiptune flavour! I downloaded this months ago and only got around to listening to it yesterday – and it was actually rather good!
  • Know what a shebang is in linux scripting? I didn’t, even though I’d been using them for years…
  • The 50 Greatest Samples in Hip-Hop History – this might take you some time to get through – but it’s some really good shizzle, my bizzle! Each of the 50 samples has the original song, along with a modern day song it was used in. PHaT!
  • Keeping on a musical bent, Captain Obvious is a music blog with loads, and loads of neat music and mp3 mixtapes – it’s good, good stuff…
  • If you’ve written some absurdly simple code, say for example: 0.1 + 0.2 and got a really unexpected result: 0.30000000000000004 – you need to read this floating point guide
  • TechRadar‘s article on Sixty Tips for XP, Vista and Windows 7 has some super handy yet little known gems to make life more pleasant in your Windows pit of hell ;)
  • NASA’s new toy – the SDO (Solar Dynamics Observatory) has opened its eyes and started staring at the sun: and it’s beautiful

Michael Franti & Spearhead – Yell Fire!

yellfire

A revolution never come with a warning
A revolution never sends you an omen
A revolution just arrive like the morning
Ring the alarm we come to wake up the snorin’

They tellin’ you to never worry ’bout the future
They tellin’ you to never worry ’bout the torture
They tellin’ you that you will never see the horror
Spend it all today and we will bill you tomorrow

Three piece suits and bank accounts in Bahamas
Wall Street crime will never send you to the slammer
Tell all the children in the arms of their Mamas
The F-15 is a homicide bomber

TV commercial for a pop-a-pill culture
Drug companies circling like a vulture
Amer-Iraqi babies with a GI Joe father
Ten years from now is anybody gonna bother?

Yell Fire! Yo, yo yo yo
(here we come, here we come)
Yell Fire! Yo, yo yo yo
(a revolution will come)

Yell Fire! Yo, yo yo yo
(put ’em up, put ’em up)
Yell Fire! Yo, yo yo yo
(throw your hands up)

A revolution never come with a warning
A revolution never sends you an omen
A revolution never come with a warning
A revolution…

Everyone addicted to the same nicotine
Everyone addicted to the same gasoline
Everyone addicted to a technicolor screen
Everybody tryin’ to get their hands on the same green

From the banks of the river to the banks of the greedy
All of the riches taken back by the needy
We come from the country and we come from the city
Play us on a record, you can play us on a CD

All the shit you givin’ us is fertilizer
The seeds that we planted you can never brutalize her
Tell the corporation you can never globalize it
Like Peter Tosh said, legalize it!

Girls and boys and the bass and the treble
Rumblin’ the speakers and it makes you wanna rebel
Throw your hands up, take it to another level
And you never ever ever make a deal with the devil

Yell Fire! Yo, yo yo yo
(here we come, here we come)
Yell Fire! Yo, yo yo yo
(a revolution will come)

Yell Fire! Yo, yo yo yo
(put ’em up, put ’em up)
Yell Fire! Yo, yo yo yo
(throw your hands up)

Throw your hands up, c’mon c’mon
Throw your hands up, put ’em up, put ’em up
Throw your hands up
Throw your hands up

Revolution never come with a warning
Revolution never send you an omen
Revolution never come with a warning

Yell Fire! Yo, yo, yo, yo
(here we come, here we come)
Yell Fire! Yo, yo, yo, yo
(revolution will come)

Yell Fire! Yo, yo yo yo
(put ’em up, put ’em up)
Yell Fire! Yo, yo yo yo
(throw your hands up)

Revolution never come with a warning
Revolution never send you an omen
Revolution just arrive like the morning
Ring the alarm we come to wake up the snorin’

Yell Fire!…
Yo, yo, yo,
Fire!

Here we come, here we come
A revolution will come
Put ’em up, put ’em up