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.
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.
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.
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
//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2007 Josh Tynjala // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// package zeuslabs.utils { public class ColorUtil { public static function blendColors(color1:Number, color2:Number, percent:Number = 0.5):Number { var remaining:Number = 1 - percent; var color1RGB:Object = ColorUtil.hexToRgb(color1); color1 = ColorUtil.rgbToHex(color1RGB.red * percent, color1RGB.green * percent, color1RGB.blue * percent); var color2RGB:Object = ColorUtil.hexToRgb(color2); color2 = ColorUtil.rgbToHex(color2RGB.red * remaining, color2RGB.green * remaining, color2RGB.blue * remaining); return color1 + color2; } public static function hexToRgb(color:Number):Object { var rgbColor:Object = {}; rgbColor.red = (color & 0xff0000) >> 16; rgbColor.green = (color & 0x00ff00) >> 8; rgbColor.blue = color & 0x0000ff; return rgbColor; } public static function rgbToHex(red:uint, green:uint, blue:uint):Number { return (redPointUtil.as
//////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2007 Josh Tynjala // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. // //////////////////////////////////////////////////////////////////////////////// package zeuslabs.utils { import flash.geom.Point; public class PointUtil { public static function reverse(v:Point):Point { return new Point(-v.x, -v.y); } public static function scalarMultiply(v:Point, s:Number):Point { return new Point(v.x * s, v.y * s); } public static function scalarDivide(v:Point, s:Number):Point { return new Point(v.x / s, v.y / s); } public static function dotProduct(u:Point, v:Point):Number { return (u.x * v.x) + (u.y * v.y); } public static function rotate(u:Point, angleRadians:Number):Point { var v:Point = new Point(); v.x = u.x * Math.cos(angleRadians) + u.y * Math.sin(angleRadians); v.y = -u.x * Math.sin(angleRadians) + u.y * Math.cos(angleRadians); return v; } public static function rotateToPoint(origin:Point, target:Point):Number { var dx:Number = target.x - origin.x; var dy:Number = target.y - origin.y; return Math.atan2(dy, dx) * 180 / Math.PI; } public static function distance(p1:Point, p2:Point):Number { var dx:Number = p2.x - p1.x; var dy:Number = p2.y - p1.y; return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); } } }