A poor man’s particle system

For a recent Flash job, I was required to add a particle system on top of an application already very CPU heavy. The project involved a large stage size, AR marker detection from a webcam feed, and a 3D-orientated plane displaying a running video, attached to the marker. This video plane then had to spew out various flavours of 3D particle at appropriate moments in the FLV.

The idea of plugging in a sexy particle engine like Flint, when the SWF was struggling even to maintain its target framerate of 25fps, made me uncomfortable. Bespoke seemed the only way to go, and hey! it worked out. Here’s what one of the particle types ended up resembling:

(It’s worth mentioning that fewer particles than can be made in the above demo were needed for the project itself. Maxing out the version here gets my CPU usage up to around 40%, which would not have left enough room for the marker tracking and FLV playback.)

To briefly cover the tricks used here, I faked 3D z-positioning by scaling clips as a function of their bespoke ‘zPosition’ value. The formula: focalLength / (focalLength + zPosition) gives the scale factor used to set the scaleX and scaleY properties of each MovieClip. The scaleX parameter was also adjusted to spoof motion blur, by overriding the x position property, and repeatedly comparing it to its value at the last update. The greater the change, the larger the scaleX multiplier, and the more stretched/blurred the particle would appear.

Rotation of the whole particle field was done by avoiding setting the x or z properties directly, depending instead on ‘radius’ and ‘rotational offset’ values. All the particles reside inside an imagined cylinder (the particle field), with its central spindle aligned with the y-axis. Each particle has its x and z location calculated on the basis of this rotation and distance from the central axis, as they move in their orbits. Channelling Mr McCallum, my secondary school maths teacher, the formulae to do this kind of positioning are, for the x axis: cos(angle) * radius; and for the z axis: sin(angle) * radius. (These can be used instead with the x and y properties to show rotation around a flat circle as opposed to the cylinder.)

In addition to rotary motion, the particles were required to ‘wander’ of their own accord. To achieve this, a large bitmap of Perlin noise is generated at the beginning of the simulation, with a reference to it passed to each particle instance. Going from pixel to pixel each frame, the RGB value is sampled and used to increment the y-velocity and radius-velocity of each particle. Over time, the Perlin noise is traversed completely, resulting in a wide range of even motion. Each particle begins sampling from a randomised row/column too, so that the movement is staggered.

Thanks Ken Perlin!
Setting the Perlin noise generation to tile allows the bitmap to be traversed from side to side and top to bottom without any sudden changes in value.

With all said and done, there may indeed have been a package out there that was equally lightweight and flexible enough for the nuances of the project. But, hard deadlines being as they are, it sometimes pays just to go from the ground up, omitting any chaff on the way – rather than hunt for elusive wheat through sprawling, multifaceted libraries.

If the source code happens to be of use to anyone I’d be very happy to clean it up and post it here. Until then… indolence wins out. Sorry!

Related posts

  1. 04threedee

    2011.04.16

    Dinoglyphs
  2. p0809_sm

    2014.09.15

    Insanity Press
  3. inspiralmonkeysSM

    2010.06.27

    New Things
  4. syn0p5
  5. extractor
  6. orange
  1. adam 2012.03.15 7:39pm

    hi mate, how do you get an output to use from the akt?
    http://akt.akineticblog.com/
    many thanks
    adam

  2. Bay 2012.06.19 8:28pm

    I would also love to utilize your generator…I work at a church and would love to be able to implement flashier text.

    http://akt.akineticblog.com/

    is there an output method so I can save the file to disk?

  3. Adam 2012.07.08 2:04pm

    Thanks for the interest guys, and sorry that I wasn’t aware of your comments sooner (time to turn email notifications on).

    In case it’s still useful, my recommendation for saving the animations would be either to pick up some screen recording software, or save the output using a second computer and a capture card. (With work commitments I’m unable to put any time into creating an offline version unfortunately.)

    For the former option, there’s a lot of software out there, and I’ve used Camtasia (http://www.techsmith.com/camtasia.html) before, but like all software solutions it can suffer from a slow framerate due to the video encoding overhead. This is alleviated by a faster computer so it may well be fine for you.

    Using a capture card offers more dependable performance but it’s considerably more costly; worthwhile probably only if you already have access to one.

    I’m glad the tool’s of some use – good luck with your recordings!

HTML tag cannot be used in this comment.

Hello

I'm Adam Vernon: front-end developer, free-time photographer, small-hours musician and general-purpose humanoid.