Particle Generation and Management Study

Over the past week, I have had opportunity to play with some ideas for particle generation that have been floating around in my mind for a while. Why particles? Well, while single object animations are great, there is a certain level of realism to movement in everything from explosions and fireworks to dust and debris when your environment can act upon the individual particles that make up your animation. Enter the particle generator. Instead of simply embedding an explosion movie into the appropriate scene, we can instead render a particle generator. These generators will then produce the debris for our effect, and each element (or particle) of that debris will be able to interact with its environment – from gravity to boundaries. By calculating each particle to have a mass of its own, the effects can be quite pleasing.

For my study, I decided that I wanted to explore the management side of particles. For instance, what lifespan should they have? How should they respond to gravity? What elasticity should they portray when they hit the ground? These were all questions that I wanted to play with until I figured out something that, at least to me, looks somewhat acceptable. One additional thing that I wanted to be sure and address was how to do garbage collection of particles that were no longer in use. As particles die, they need to be nullified in order for Flash’s System.gc() to clean them up. With a high volume particle generator, it’s possible for several hundred particles to be created every second. Combine this with a lifetime of several seconds, and you can very quickly build up thousands of residual particles in memory. These need to be cleaned up as soon as they die.

So, taking all these things into account, let’s glance at my final product:

By hovering over the color boxes, you can change the color of the generated particles, and by selecting one or more of the options, you can change the environment and behavior of the particles as well.

I’m very interested to get input and/or suggestions on improving the feel of this study. I didn’t put a whole lot of additional effort into making the gravitational algorithms account for mass, so there is no terminal velocity or other additional realism attributes present. My focus with this is definitely more on the look and feel of the particles themselves and how they behave.

One problem I did notice is that, when you leave the SWF open in a window for several minutes — or if you turn on high volume and click to boost generation for a while — it will begin to hesitate and finally freeze altogether while the mouse is moving across it. What’s odd is that the only MouseEvent.MOUSE_MOVE listener in the whole thing is for the “Track Mouse” option, and it is removed as soon as the option is turned off again, so I don’t know what would be causing the memory issue. If anyone has any ideas or suggestions after reviewing the code, please let me know!

As always, I want to share as much of my learning process as possible with anyone reading, so I’m making the full source code for this study available.

View Full Source

4 thoughts on “Particle Generation and Management Study

  1. I didn’t have the time to look at the source yet, but I keep getting lots of:

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at ParticleManager/cleanupParticles()

    Looks like you aren’t removingChildren() from the particles DisplayObjectContainer.

    By the way, you could use object pools instead of relying on System.gc(). Although you are making sure it’s called once in a while, it’ll always have to work to clean up your instances, and you’ll always have to work creating new ones. A particle pool could be more efficient.

  2. Ugh, you know what? You’re right. I had originally attached the particles as children of the manager object, but I moved it to the parent when I wanted to start moving the origin of the manager around. I never did change the cleanup to remove from the parent.

    I’ll have to check into the object pools. I was not very happy with having to rely on the System.gc() to automatically clean up my particles, so I’ll have to read up on speed and efficiency for this.

    Thanks for the tips!

  3. Pingback: Particle Generation and Management Study | Dev Art Of War

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>