Particles - Team-Innis/UtH-Engine GitHub Wiki
UtH-Engine has a simple-to-use, yet very customizable particle system. It offers a nice framework for defining and emitting particles where you can choose exactly how your particles behave. ParticleSystem
derives from GameObject
, so you can update and draw it as such and you can also add it onto a layer.
Initialization
The simplified usage pattern for the particle system goes as follows:
- Instantiate a
ParticleSystem
. In the constructor you'll need to supply the maximum amount of particles that can be active at a time. This must be done for performance reasons. - Create a
ParticleTemplate
, modify it to your liking and pass it into yourParticleSystem
by usingSetTemplate
. - Create one or more
Affectors
, set up their functions and pass them into yourParticleSystem
. - Take care of updating, and drawing your
ParticleSystem
. You'll also need to take care of emittance (see the next section).
Particle System
ParticleSystem
is the class in the center of it all. It takes care if initializing, updating and emitting the particles.
To have a functional particle system, you'll need to supply it with a template and one or more affectors. Then you also need to take care to call Update
and Draw
in their appropriate places. With SetEmitProperties
you can choose to let the particle system emit automatically, but otherwise you'll need to take care of emitting the particles yourself by calling the Emit
function.
Particle Template
A class that contains some common particle attributes. Any particles a ParticleSystem
creates, will be created according to how a template is defined.
ParticleTemplate
has the following attributes:
- Texture - A texture to be used. You can also optionally define custom texture coordinates.
- Lifetime - Defines how long the particle will be active before it's deleted.
- Speed - The initial pixels/second value. This can be a random value inside a specified range.
- Color - The initial color of the particle.
Particle Affector
Affectors are used to define how particles behave during and after creation. You have two ways you can use an affector:
- Derive a custom class from
Affector
and override the functions - Supply the affector with the approppriate function pointers or lambdas by using the
Set*Func
functions.
Should you choose to use the second option, the supplied functions have to have the following following signatures:
Init function: void (uth::Particle&, const uth::ParticleTemplate&)
Update function: void (float)
Particle update function: void (uth::Particle&, const uth::ParticleTemplate&, float)
You can have any number of affectors inside a ParticleSystem
as you like, each with their own behavior. You should prefer fewer affectors, however, since the amount of affectors is directly proportional to the amount of work it takes to update each particle.
Notes
- The particle system only supports a single texture. Atlases might be supported in the future.
- If the maximum amount of active particles is reached at any time, any attempts to call
Emit
will be ignored. ParticleSystem
takes a copy ofParticleTemplate
, so you won't have to keep it in memory after passing.- Any
Affectors
need to be allocated withnew
and must remain in memory as long as any particle system using them is active. You should not delete them afterwards, as the particle system takes care if that automatically