# A Basic Particle System

The clever people at CERN are smashing particles together in the hope that Doctor Who will turn up and tell them to stop.

– Ben Aaronovitch, Moon Over Soho

Particle Systems are physics models that model small particles. Long ago, those systems have been introduced to game engines and are the foundation of creating a realistic looking environment. They are great to, for example, model explosions, vapour trails and general lights in a game.

## A Single Particle

Basically speaking, a particle system is a group of particles that all act and behave the same way — think of rain drops or parts of a car after a crash. All those particles have a few things in common, namely a position, a velocity vector, a colour, and an age. The age is used to delete particles after a certain specified time:

Updating the particles is once again done using our semi-implicit symplectic Euler integrator:

### Environment

To define the environment in which the particles live, a singleton is used. All external forces acting on the particles can be defined here.

## Abstract Particle System

To enjoy the marvellous beauty of hundreds of particles moving together in perfect harmony, an abstract class to handle all their needs is implemented:

### maxParticles and maxLifeSpan

The maxParticles and maxLifeSpan variables define how many particles are allowed to be alive at the same time and for how long a particle may live.

### dxApp, gc, nt

Those variables simply store references to the main DirectX App, to the graphics component of the DirectX class and to an object of the number theory class. The number theory class is used to generate a few random numbers, to make the movement of the particles appear more natural.

### std::vector particles

This is the array where all the actual particles of the system are kept in as long as they are live.

### bool regenerate = false

This boolean, initially set to false, defines the behaviour of particles once they reach the end of their life cycle. If set to false, they simply die, but if set to true, they regenerate and come back to life — this might be useful to simulate water fountains, for example, or other closed systems.

### mathematics::Vector2F position

This vector defines the centre of the particle system, i.e. the location where all particles of the system spawn.

### virtual void GenerateParticle(…) = 0;

This purely abstract function must be overridden by the actual particle system classes. It defines how the particles for a specific system are generated.

### Setters

The two setters are self-explanatory.

### ParticleSystem(…)

The constructors simply initializes the DirectX components.

### virtual bool update(double deltaTime) = 0;

This purely abstract function must also be overridden by the actual particle system to define how the particles are updated each frame.

### virtual void draw(double farSeer) const

Drawing the particles is easy, it works as it has always worked:

#### Getters

The getters are self-explanatory.

## An Example: Explosions

As an example, we will add a little explosion to the Kicker-demo each time the ball hits one of the edges of the table. Have a look at the header file:

### Construction

The entire explosion, i.e. all the particles, are created during construction, as follows:

The position of the particle system is set to the position of the ball, plus/minus a random offset, and the initial velocity of the particles is based on the velocity of the ball at the moment it hits the wall. The age of each particle is also randomized, to make sure they don’t all disappear at once.

The particles are then set to have three different colours: A third of the particles are each black, red, and golden. Hello Germany!

### virtual void GenerateParticle(…)

This function describes how the particles are generated. In this case, the particles are simply added to the array:

### virtual bool update(double deltaTime) override;

Last but not least, we have the update function. Here each particle is updated using the semi-implicit symplectic Euler-integration technique of the previous tutorials. In addition, the age and intensity of the particles are updated. If a particle reaches the end of its lifecycle, it is deleted from the array. The deletion simulated the erase-remove idiom:

And that’s it already — we now have our own particle system!

To use the particle system in the Kicker-demo, an array with particle systems of the type explosion is added to the playstate header:

Now during the game update, if the ball hits one of the edges of the table, a particle system is created and added to the array:

Perhaps it would be a nice idea to also use the event system to queue up particle systems, but for now, this will do.

Drawing the particles is a walk in the park:

Enjoy the explosions!

This should be enough information for you to be able to create and add your own particle systems within minutes!