More Fun with Flash 9 Particle Systems

by Josh Tynjala

Back in May, I spent some time experimenting with particle systems in Flash 9 and Actionscript 3. Thankfully, I found some time to come back to this project. I had one goal in mind: The concept of a particle system must be as flexible as possible. It could be anything from the flames and steam I created in my previous project to falling snow or even something more complex that involves artificial intelligence and flocking behaviors.

Let's take a look at a couple of the different particle systems that I've created. You can see that flames made a comeback in the example below. To mix things up, I used several active particle systems at once, and I threw in a video game sprite to add some flavor. It may be a little heavy on the CPU, but as you can see, a particle system (or six, in this case) can add a lot of atmosphere to a scene.

Preview image of flames particle effect

Next, I created a fountain of spraying water. The water fades from blue to white and an occasional drop flies high above the others. You'll see that the individual particles are a little more obvious in this example, but I think I was able to give the water some personality to make up for it.

Preview image of a fountain particle effect

Finally, I wanted to create a system that featured "skinned" particles. The snowstorm that I linked to above inspired me to create an example with falling leaves. I imported vector art from Flash to represent each particle, and as you move your mouse left and right, you control the direction of the wind. I created the illusion of depth by making smaller leaves fall slower to make them appear further away from the viewer.

Preview image of a flying leaves particle effect

Not quite as impressive, though technically interesting, I also made a swarming effect using animation along Bezier curves, and I started work on a simple bitmap particle system. I'd like to spend some more time on this project because I've got a bunch of cool ideas, but I have to move on to other things for now. Expect me to post random cool effects from time to time.

Full source code is available under an MIT license, and I've included ASDoc documentation. If you decide to play around with the code at all, I'd love to see what you create!

Update (08/06/2008): For completeness sake, I've included two classes below, ColorUtil and PointUtil, that I reference in the examples, but I omitted from the download:

ColorUtil.as

Actionscript:
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Copyright (c) 2007 Josh Tynjala
  4. //
  5. //  Permission is hereby granted, free of charge, to any person obtaining a copy
  6. //  of this software and associated documentation files (the "Software"), to
  7. //  deal in the Software without restriction, including without limitation the
  8. //  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. //  sell copies of the Software, and to permit persons to whom the Software is
  10. //  furnished to do so, subject to the following conditions:
  11. //
  12. //  The above copyright notice and this permission notice shall be included in
  13. //  all copies or substantial portions of the Software.
  14. //
  15. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. //  IN THE SOFTWARE.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////
  24.  
  25. package zeuslabs.utils
  26. {
  27.     public class ColorUtil
  28.     {
  29.         public static function blendColors(color1:Number, color2:Number, percent:Number = 0.5):Number
  30.         {
  31.             var remaining:Number = 1 - percent;
  32.            
  33.             var color1RGB:Object = ColorUtil.hexToRgb(color1);
  34.             color1 = ColorUtil.rgbToHex(color1RGB.red * percent, color1RGB.green * percent, color1RGB.blue * percent);
  35.            
  36.             var color2RGB:Object = ColorUtil.hexToRgb(color2);
  37.             color2 = ColorUtil.rgbToHex(color2RGB.red * remaining, color2RGB.green * remaining, color2RGB.blue * remaining);
  38.            
  39.             return color1 + color2;
  40.         }
  41.        
  42.         public static function hexToRgb(color:Number):Object
  43.         {
  44.             var rgbColor:Object = {};
  45.             rgbColor.red = (color & 0xff0000)>> 16;
  46.             rgbColor.green = (color & 0x00ff00)>> 8;
  47.             rgbColor.blue = color & 0x0000ff;
  48.             return rgbColor;
  49.         }
  50.        
  51.         public static function rgbToHex(red:uint, green:uint, blue:uint):Number
  52.         {
  53.             return (red <<16) + (green <<8) + blue;
  54.         }
  55.     }
  56. }

PointUtil.as

Actionscript:
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Copyright (c) 2007 Josh Tynjala
  4. //
  5. //  Permission is hereby granted, free of charge, to any person obtaining a copy
  6. //  of this software and associated documentation files (the "Software"), to
  7. //  deal in the Software without restriction, including without limitation the
  8. //  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. //  sell copies of the Software, and to permit persons to whom the Software is
  10. //  furnished to do so, subject to the following conditions:
  11. //
  12. //  The above copyright notice and this permission notice shall be included in
  13. //  all copies or substantial portions of the Software.
  14. //
  15. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. //  IN THE SOFTWARE.
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////
  24.  
  25. package zeuslabs.utils
  26. {
  27.     import flash.geom.Point;
  28.  
  29.     public class PointUtil
  30.     {   
  31.         public static function reverse(v:Point):Point
  32.         {
  33.             return new Point(-v.x, -v.y);
  34.         }
  35.        
  36.         public static function scalarMultiply(v:Point, s:Number):Point
  37.         {
  38.             return new Point(v.x * s, v.y * s)
  39.         }
  40.        
  41.         public static function scalarDivide(v:Point, s:Number):Point
  42.         {
  43.             return new Point(v.x / s, v.y / s);
  44.         }
  45.        
  46.         public static function dotProduct(u:Point, v:Point):Number
  47.         {
  48.             return (u.x * v.x) + (u.y * v.y);
  49.         }
  50.        
  51.         public static function rotate(u:Point, angleRadians:Number):Point
  52.         {
  53.             var v:Point = new Point();
  54.             v.x = u.x * Math.cos(angleRadians) + u.y * Math.sin(angleRadians);
  55.             v.y = -u.x * Math.sin(angleRadians) + u.y * Math.cos(angleRadians);
  56.             return v;
  57.         }
  58.        
  59.         public static function rotateToPoint(origin:Point, target:Point):Number
  60.         {
  61.             var dx:Number = target.x - origin.x;
  62.             var dy:Number = target.y - origin.y;
  63.             return Math.atan2(dy, dx) * 180 / Math.PI;
  64.         }
  65.        
  66.         public static function distance(p1:Point, p2:Point):Number
  67.         {
  68.             var dx:Number = p2.x - p1.x;
  69.             var dy:Number = p2.y - p1.y;
  70.             return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  71.         }
  72.     }
  73. }

9 Comments

JesterXL

W00t, Magus!

Campbell

Ohhhhh pretty! Another Quality release Josh…..with good frame rates.

Props bro!

Josh Tynjala

Thanks Campbell. The frame rates can be set higher, actually. I did a couple of tests at 60fps, and they still performed pretty well.

RaXaR

Great Work on the particle system. I have recently found these websites that could prove to be useful: http://blog.andre-michelle.com/2005/as3-bitmapcloud-3d/ and also http://www.cove.org/ape/. Keep up the great work.

memory

well done,just i want to research particle i found you blog.

oyun siteleri

t thikk that
Great Work on the particle system. I have recently found these websites that could prove to be useful: http://blog.andre-michelle.com/2005/as3-bitmapcloud-3d/ and also http://www.cove.org/ape/. Keep up the great work.

thisis very good idea..great

Sean

Great work josh! I recently downloaded your source files for the Particle experiments you’ve done and when attempting to utilize your classes for my own experimentation, I noticed in your BitmapParticles.as class you are importing two classes that are not included in your zip file; ColorUtil and PointUtil. I am curious to know the purpose of these two classes and if they are required for the Particle classes to function properly?

Thanks again!

Josh Tynjala

Sean, these classes aren’t required for the core particle system classes. However, I seem to use PointUtil in one of the examples. I didn’t see any references to ColorUtil, other than the import statement, so it was probably left over from something I was experimenting with. I’ve included both classes in the post above in case anyone else wants to see them.

Leave a Comment

Note: New comments may need to be approved before they appear.

Some HTML allowed in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To display code in comments: <pre>Code here. May be multiline. Format XML with &gt; and &lt; entities.</pre>