Particle Systems

Introduction

Hi, I feel I should start this tutorial with a short introduction to who I am. My name is Chris Gilbert and I have been programming for around the past 5 years. My ventures into programming started in the days of DOS with nothing more complex than batch file writing and dabbling in QBasic. Then I got a copy of Visual Basic and stuck to VB for a few years when about 2 years ago I came across C++ for the first time. I now develop almost entirely in C++.

So what is a particle system? A particle system is a concept, and it can be anything you want it to be. I mean that! A particle system can be pretty much anything you want, however as a general rule, a particle system is a collection of any number of entities, either related or unrelated, that behave according to a set of logical rules. So a particle system has two parts to it which we can identify, the entities or particles, and its logic set. A good example of a particle system would be a rain cloud. The drops of rain are the entities, and they behave according to the rule of gravity - IE yous is goin down boi. When they hit the ground, they effectively die. Most of the time, entities in a particle system have a very finite lifespan, and when they die they are re-initialised as a brand new particle.

"We don't try to model the outcome of the system, we try to model the system and see its outcome" - Andre LaMothe

Let's explore the concept of particle systems a little further. I think that one of the reasons why programmers have problems understanding particle systems and their scope is because particle systems have no fixed rules and almost unlimited scope. Think about it, think about the rule that I made up about particle systems, any number of entities that just behave according to a set of rules. This could be anything from cars in a road system (Grand Theft Auto?), to stars in the sky (M\$ Windows screensaver ;)), to anything!

Particle systems could even go down to the atomic level with billions of particles representing an object, and that object behaving just like it would in real life! I have included that quote by Andre LaMothe because it's very true. When writing a particle system there are no fixed rules, there are no definites, you just have to tweak everything until the final effect looks good!

Particles are the answer to everything, and yet nothing. Their limitation comes in the form of one thing only, how fast the CPU is. Computers just aren't fast enough to perfectly simulate objects on the atomic level in a realtime environment, and it's perfectly possible that the car system in a game such as GTA was done using something other than a particle system, but hopefully this gives you an idea of the scope of these extremely powerful objects. Just take note that it is up to you as the programmer to strike some kind of balance between how many particles there are in any given particle system, and how much CPU time is taken up handling them all.

When would you use a particle system? Whenever you want to create some eye candy? ;) I play games these days and some of them impress me with their particle systems, some of them don't, but pretty much all of these games have got them! Explosions, sparks, underwater bubbles, other special fx, it's all there! I feel this is a tribute to particle systems's flexibility because their scope for modelling is amazing. Particle systems have been used back as far as I remember (Not that long ago, but still in the days of 2D DOS games!). A particle doesn't need 2 dimensions, or 3 - in fact should you so decide it a particle could have a million dimensions, but this brings me to another point: Your particles have to be represented as something. This is the -visual- aspect of a particle system. A particle system could happily waste away CPU time juggling 1000 particles modelling a rather convincing explosion, but unless the particles are actually drawn on the screen as something, it all amounts to nothing. A particle can be represented by anything. A 3d model, a single vertex, a textured quad, anything! It is up to you as the programmer to decide what should represent an entity in your particle system.

Why use particle systems? Because they can take a lot of work off your shoulders. As you are programming you should be looking at every opportunity to put everything into one massive particle system! Well, realistically speaking that's not very practical. To be honest it is up to you when and why to use a particle system as the programmer.

So now you know more about particle systems, let's have a short example of how to approach writing a particle system which models a rain cloud.

We want the particles to:

1) Initialise in a random place in the 3d world
2) Move towards the ground according to gravity
3) Die when they hit the ground, and immediately come back to life as a new particle

Particle systems in a game will often look similar to the following:

```#define MAX_PARTICLES = 1000;

typedef struct tParticle1
{
... Properties of the particle
}

tParticle1 Particles1[MAX_PARTICLES];
```

Ok so why do we have a constant defining the maximum number of particles? In the kind of particle system I'm thinking of, it's because the entities are kept in a single 1 dimensional array and every entitity has the same attributes. In most particle systems, all the entities will have the same properties because it's (much) faster to keep several particle systems representing groups of related entities rather than one big particle system representing a group of un-related entities (By that I mean either the particles are all an instance of a single structure, or in the case that they are 'un-related' that all the particles are instances of different structures). In fact, what is often the case is that the only property un-related entities definitely share is some way of representing a given position in 2d or 3d space, which seriously limits what the rules of the particle system can use to manipulate the particle.

Particle systems often (If not always) have several functions related to them, initialisation, update and rendering. Sometimes particle systems even have 2 initialisation routines associated with them, one of the routines being a one-off initialisation routine (Which when the system is created will initialise all of the particles), the other one being a general purpose initialisation function (Which works on individual particles) for when the particle has died and is being restarted. These functions are typically associated with a specific particle system because the particles in that particle system may have completely different properties and may behave in a completely different way to another particle system. The update function is where the rule set of the given particle system comes in to place. The update function might simply loop through each of the particles in the system and decrease the y coordinate of the given particle according to gravity (As is the case with our rain cloud *g*)

In the case of a rain cloud (Or even, a rainy sky - for the sake of simplicity), the associated functions of the particle system might look something like:

```initall()
{
for(int i = 0; i <= MAX_PARTICLES; i++)
{
Particles[i].x = rand() % WORLD_WIDTH;
Particles[i].y = rand() % WORLD_HEIGHT;
Particles[i].z = rand() % WORLD_DEPTH;
}
}
initentity(int index)
{
Particles[index].x = rand() % WORLD_WIDTH;
Particles[index].y = rand() % WORLD_HEIGHT;
Particles[index].z = rand() % WORLD_DEPTH;
}

render()
{
for(int i = 0; i <= MAX_PARTICLES; i++)
{
draw_rain_texture(Particles[i].x, Particles[i].y, Particles[i].z);
}
}

update()
{
for(int i = 0; i <= MAX_PARTICLES; i++)
{
Particles[i].y =- (rand() % 2) - 2.5;
if(collisiondetect(Particles[i]))
{
initentity(i);
{
}
}
}
}
```

And that would in theory render a rain cloud. Well, I mean this is a highly simplified and very crude particle system. I mean, the particle system doesn't even take into account wind or anything!

A particle system is a simple object that merely manipulates a group of entities according to a set of rules. They can be as big or small as you like, as simple or complex as you like. They are used in games mainly to perform special effects but are by no means limited to simple special effects.

So where to go from here? Hopefully by now you have a pretty good idea of what a particle system is, and you'll probably want to simulate fireworks in the night sky ;) Just a suggestion! But it is quite simple to do, and you will be presented with a problem which will require several particle systems to solve, or one very flexible particle system, or one very large very complex particle system ;) If I was going to give you some advice as to where to start approaching the problem, I would recommend you start at the beginning. Get a video and watch fireworks going off, see how closely you can emulate the effect :)

Hope this helps!

Chris Gilbert (The Gilb)