Particle systems – Part 1

particles

Particle systems are one of the techniques that fascinates me the most of the ones that are used in video game development. I think it is because it has a certain “natural” feeling, specially if combined with a good physics engine. In short if you don’t know what a particle system or what is it for think about the following examples: smoke, fire, rain, sparks, hair, lightning… Many of these effects would be difficult to generate in a game without a particle system. The most used game engines even have their own particle system, for example UDK uses Cascade and Unity uses Shuriken. I will publish a series of articles on how to engineer (this is about engineering games after all) particle systems, I will start with the basic concept and move on step by step until the use of a full fledge engine particle system (Unity’s Shuriken).
In this first article, I will explain the basic idea. I’ll be mixing different programming languages and frameworks as I don’t want to restrict these articles to a single technology but to the particle system concept itself. And now on to the explanation itself.

Particle system

A particle system is a technique used in computer graphics to simulate complex systems that would be difficult to represent otherwise. The main idea behind it is that in order to mimic the randomness of these systems instead of trying to represent the whole effect itself (fire, sparks, water…) a collection of particles with different properties is used. A particle system usually is made of: a pool of particles (of course), particle characteristics/behavior and a emitter/s. Mind you there are other ways of generating a particle system (for example not using a emitter but drawing the particles directly all over your screen) but this is the most common and can be easily expanded.

Particles

Particles are one of the components that form the particle system. In object oriented programming you can think of these as objects instantiated from a single class (you can later use inheritance and polymorphism to have different types of particles). In its most basic form a particle class contains the position and lifetime (this is a very important parameter and will be explained later on) of the particle and methods to update and draw it. For example a basic implementation using the XNA framework could be the following:

public class Particle
{
	// The position of the particle
	public Vector2 position { get; set; }
	//Where to move the particle
	public Vector2 direction {get; set; }
	// The lifetime of the particle
	public int lifeTime { get; set; }
	// The texture that will be drawn to represent the particle
	public Texture2D texture { get; set; }

	public Particle(Texture2D texture, Vector2 position, Vector2 direction, int lifeTime)
	{
		this.texture = texture;
		this.position = position;
		this.direction = direction;
		this.lifeTime = lifeTime;
	}

	public void Update()
	{
		//We reduce the lifeTime of the particle
		lifeTime--;
		// Updating the position in the desired direction
		position += direction;
	}

	public void Draw(SpriteBatch spriteBatch)
	{
		//Basic drawing
		spriteBatch.Draw(texture, position, Color.White);
	}
}

Emitter

The emitter is simply an object (in 2D/3D space depending if you are working in 2D or 3D) from where all the particles will emanate. This can be a simple point (a single position), or a 2D/3D object. For example we can have a circle and particles being generated from all the perimeter of it. If for example you want to generate water jumping off a waterfall the emitter will need to be the whole edge of the the waterfall. In the previous example, the emitter is a single point and is declared as a Vector2 within the particle system class.

Behavior

This is where things get interesting. The part that makes the particle system shine is the behavior of its particles. As you have seen above with our very basic particle class we have defined already some of it. The particles will have a direction and a life time. The life time is very important parameter and controls the time that a specific particle will exist. Once the life time is reached the particle will be destroyed. This can mean literally destroying the object instance or just mark it as dead. To obtain a performance boost I recommend the later, we could have a pool of pre-instantiated particle objects that we reuse marking them as alive and dead. Only when alive they’ll be updated and drawn. The other approach is create new instances for every new particle that needs to be created and destroy the objects.

Besides the life time, many other properties can be defined for the particles. The final effect that our particle system will create will depend on the properties of our particles and how we update them. For example, we could change the color of our particles like this:

public class Particle
{
	// The position of the particle
	public Vector2 position { get; set; }
	//Where to move the particle
	public Vector2 direction {get; set; }
	// The lifetime of the particle
	public int lifeTime { get; set; }
	// The texture that will be drawn to represent the particle
	public Texture2D texture { get; set; }

	//The color of the particle
	public Color color { get; set; }
	//Use to create random numbers
	private Random random;

	public Particle(Texture2D texture, Vector2 position, Vector2 direction, int lifeTime, Color color)
	{
		this.texture = texture;
		this.position = position;
		this.direction = direction;
		this.lifeTime = lifeTime;
		this.color = color;
		random = new Random();
	}

	public void Update()
	{
		//We reduce the lifeTime of the particle
		lifeTime--;
		// Updating the position in the desired direction
		position += direction;
		//Change the color of the particle through different shades of red/yellow
		color = new Color(1f , (float)random.NextDouble(), 0f);
	}

	public void Draw(SpriteBatch spriteBatch)
	{
		//Basic drawing
		spriteBatch.Draw(texture, position, Color.White);
	}
}

In the previous example we have created a new property for the particle (color) and using a random number generator we are updating it to a different random color. This is one important aspect of particle system, randomness. In many of the properties assigned to the particles we are going to want to apply some constrained randomness to have a more “natural” effect. This will also make that our effects will be different every time. Imagine for example two torches in a scene and both of them have fire generated using the same particle system. Due to the randomness introduced into it, they will be similar (fire effect) but not exactly the same. Constraining the randomness is also necessary as you don’t want a fully random effect. In the example above we are applying the randomness to the green component of the RGB color of the particle leaving the red and blue component to 1 and 0 respectively. This will create particles that will have a random color but constrained to be always a combination of red, a random shade of green, and no blue. If you think of it, this will generate, particles whose colors will vary among the reds and yellows. If we want to make fire, we are already in the right direction.

This is the end of the first article on particle systems, in the next one I will explain how the particle system handles the particles and further particle behavior customization. The examples here have been done in C# and XNA but can be easily ported to other languages and frameworks following the same idea.

Advertisements

One response to “Particle systems – Part 1

  1. Pingback: Particle Systems – Part 3 (Unity’s Shuriken) | Stilghar·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s