Particle System

Particle System

Particle System Framework

In this post I will cover in detail the framework of the Particle System for a Game being created by my group.

I will only be covering my contributions in this blog but it may show separate areas while demonstrating my own work.

First have a look through my Class Diagram.

Particle System Class Diagram

While it may look confusing, in reality it is very simple. While it may look cluttered and have lots of variables, many of them are just set and forget, these set and forget variables are created when the user instantiates an emitter or particle System allowing for a wide range of variation even with very few classes.

The idea behind the framework is modularization and customisation. I accomplish this by creating a few different types of emitters (Glow, Trail & Circle) these all have distinct behaviour and can themselves be customised greatly.

I only have 3 types of emitters at the moment but I will be adding more, for example a Beam and Explosion Emitter. Once I had created these emitters I now had to create the class which handled these, as a single emitter doesn’t give a very good effect. I solved this by creating a Particle system class, this manages the emitters and works as an easy interface for the black boxing part of the project. The Particle Systems are created with a specific task in mind. I currently have two as shown in the Class Diagram.

Particle System Classes

Abstract Base

I decided to use inheritance for my Particle Systems this allowed me to keep all of the child systems stored easily and allowed me to keep the code simple from a black box standpoint (from the outside they are all the same). The Parent holds a few universal Fields and Functions as demonstrated in the Class Diagram, holding the Position and world Rotation of the system so that all children must use it. The update function in the base handles only removing all the dead Emitters from the list meaning it should always be called in child classes update overrides (by using base.Update()).

PS Missile

The Missile system so far consists of only two different emitters a Trail and a Glow, I will be adding a third later today once written which will be an Explosion Emitter to add the final touches to the effect at the moment they just disappear when the trail has finished. The Class contains a large range of variable that the user can change for the two Emitters to get different effects, at the moment these are within the class but they will be moved to the constructor soon to add even more customization to the class.

Particle System Particle System

PS Laser

The Laser system so far contains three Emitters, all of which are circles.

Two of the emitters are placed in the centre facing out on mirrored angles and only a 90° spawn angle, meaning the particles spawn anywhere within this cone giving the effect of gathering energy from the area around the centre.

The third Emitter is a full circle sat at the origin of the particle system, this is just a very small circle which spawns a lot of particle in quick succession to give the effect energy is being compressed into a small point. Like the PS_Missile class this has a wire range of different variables that can all be adjusted by the user and may be moved into the constructor in the future.

(Just had the idea of creating a simple input struct for each of the emitters, that way I only need to pass this into the class instead of the long list of variables.)

Emitter Classes

Abstract Base

For the Emitter I also decided to go with inheritance as this allowed to the easiest expansion of the range of Emitters. The base class holds only variables that every Emitter must use and functions that the child must either implement or override, the variables it hold are position, rotation, colour and scale.

The base class also creates some functions which are universal like the Update function, its only purpose is to remove dead particles and return false when there are no more particles in the list so that it knows to remove itself, this must be called using base.update() in child updates.

The function implements a simple AddParticle function() but many of the emitters require different fields so this is virtual and child classes allowed to implement their own. The emitter also offers a range of functions for colour variation, these work by taking in a base colour and then altering it by a random amount within a given range.

Emitter Trail

The Emitter Trail works by taking in a new position every update and then filling in the gaps between the previous and current position at a given interval with particles, this interval can be customised to add different effects, it must be at least 1 or will cause problems so I have coded in a check that makes sure it is at least 1, these dropped particles can be given a direction to travel if you wish or they can just remain stationary.

Particle System

Emitter Circle

The Emitter Circle works in a very simple manner, instead of spawning at the origin of the emitter the particles spawn location is worked out through a combination of angles and a radius, then directs them back towards the origin of the emitter. The spawn angle can be affected by 2 things, the angle at which I want them to spawn within (between 0 – 360°) and the rotation of the Emitter (which is derived from the parent particle system).

Emitter Glow

The Emitter Glow is very similar to the Emitter Trail class and I may merge the two on my next re-factoring pass, the major difference from the trail is the glow cannot be given a direction to travel, the rest of the differences are just variables such as Scale and Life.

Particle Classes

At the moment I only have a single Particle Class but once I get further into the framework I will want to have animated particles, at which point I will convert to inheritance like the PS and Emitters.

Particle

Particle System

The Particle class contains all the relevant information to a single particle and handles its own update and draw. The update function simply lowers the life of the particle by Delta Time and then move the particle in the desired direction. The Draw function handles the drawing of the particle, it calculates the scale and colour by lerping in between a start and end value for both with its LifeWeight as the value to lerp by. (LifeWeight = LifeLeft / StartingLife).


Video Demonstration

This is a basic demonstration of the progression my particle effects have followed from the very start to where I am now. I still have lots to do and will continue to implement features as they are needed, thanks to the way I have designed the system, it will be very easy to expand.