Categories
General

Animated Plasma in Flash

OK you can tell I’m fired up right now. I should be going to sleep but instead I’m blogging about a cool plasma effect I just programmed.

Most of you won’t know that my programming career started on the Commodore Amiga. And my mentor was one of the leading demo coders, Jolyon Ralph. So, for old times’ sake, here is an actual plasma effect, programmed in Flash.

[kml_flashembed movie=”/wp-content/uploads/manual/2007/plasma.swf” width=”440″ height=”350″ FVERSION=”9″ QUALITY=”high” /]

It’s actually quite simple… we’re just calculating the colour for each pixel using the following code :

	v = Math.sin(dist(x + time, y, 128.0, 128.0) / 8.0)
		+ Math.sin(dist(x, y, 64.0, 64.0) / 8.0)
		+ Math.sin(dist(x, y  , 192.0, 64) / 7.0)
		+ Math.sin(dist(x, y+ time/ 7, 192.0, 100.0) / 8.0);
	colour = int((4 + v)) * 32;

where x and y are the positions of the pixel, t is a value for time, incrementing by 1 every frame. The sin function is the Math.sin function, and dist is a function that calculates distance between two points:

function dist(a:Number, b:Number, c:Number, d:Number)
{
	return Math.sqrt(((a - c) * (a - c) + (b - d) * (b - d)))
}

Pretty cool that Flash is finally catching up with the computing power of the Amiga! Hmmm.

Thanks to Lode Vandevenne for the refresher course…

14 replies on “Animated Plasma in Flash”

Hey Frank! That’s great work, I’ll have to take a look at the source and try to bend my head around it soon!

I’m actually surprised that AS3 can process this plasma with all the Math.sqrt calls which have traditionally been MASSIVELY slow… now there must be a plasma function that doesn’t need it…?

Seb

this needs “click to stop” button, i had to reaload the page to type a comment.

I wonder if there is a simple way to do this with perlin noise instead? I need something to calculate alpha layer for glowing effect.

Hey Seb.

you wrote:
“I’m actually surprised that AS3 can process this plasma with all the Math.sqrt calls which have traditionally”…

it´s only possible ´cause of using ByteArray and paletteMap. I´ve played a lot with these kind of plasma-creations:
//prinzipiell.com/2007/05/25/plasma-nothing-more-or-less/

or a tunnel-effect with 2 “luts”:
//prinzipiell.com/2007/08/13/a-tunnel-the-oldschool-way/

…last 2 examples would be faster too if I used ByteArray and paletteMap there.

By the way – inspiring “particle”-session on fotb. 😉

cheers
frank

Hey Seb,

Just a thought – there is a distance method built into the Point class. Would it be quicker to convert the x/y into Points and then use that?

Owen

hey owen! That’s not a bad idea… i feel a benchmark test coming on… anyone want to do the honours?

I just did a speed comparison, a load of different ways. Using the point class distance method was a lot slower (x4) than using this method. Precalculating the subtractions slowed it down a miniscule amount, whereas using 2 points rather than 4 discrete variables made it a tiny fraction faster.

Hey Owen!

That’s fascinating! I can’t believe it’s slower with Point.distance… that’s mental… can you post the code?

Of course the other way to speed up the plasma is to create look-up tables for everything that doesn’t change over time (like frank said)…

cheers!

Seb

Hey again. I’ve been doing lots of optimising code recently and built myself a tester class to save time – follow the link for a download…

I’ve also found that you can eliminate the need for using Math.sqrt by squaring the distance you are comparing as well. ie:

if ((xd*xd)+(yd*yd) < (a.radius+b.radius)*(a.radius+b.radius)) //ball collision

Runs about twice as fast!

Hey Owen! Ah the old sqrt workaround – it’s an awesome technique that I found a couple of years back. Indeed Math.sqrts are REALLY slow – avoid like the plague.

Seb

Actually, in the old days one would compute the image only once and draw it in a color indexed video mode. The animation back then was achieved by rotating the color palette. No pixels needed to be redrawn.

Comments are closed.