More Fun with Flash 9 Particle Systems

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

////////////////////////////////////////////////////////////////////////////////
//
//  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 (red 

PointUtil.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));
		}
	}
}

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. Pingback: plasticthinking » Blog Archive » fisix engine å’Œ APE(Actionscript Physics Engine)

  2. 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!

  3. 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.

  4. Pingback: Flash Game Frameworks, Classes and Libraries | der hess