INTRODUCTION
As promised here is a tutorial on how to build a Particle Engine, I will describe it in general terms so that anyone will be free to implement it in his preferred language, then we will implement it for Hollywood-MAL using the SDL plugin to speedup objects drawing and achieve high frame rates.
I’ve already implemented it in the upcoming game CryptoMages.
Source code will be available for all Patrons in a Patrons only post.
WHAT IS A PARTICLE ENGINE
A Particle Engine is an object that is used handle a so-called Particle System which is able to draw hundreds or thousands of small graphical objects (particles) concurrently to create various graphical effects like fire, explosions, water, smoke, bullets, sparks and so on.
Each particle has its very own life with some semi-random properties to make it different from all the other particles. Every particle has a death time and when a particle die a new one will be generated making the effect continuos.
A Particle Engine is the code that handle all the stuff related to one (or more) Particle System which is an object instance that is generated by the Particle Engine.
A Particle is the small graphical objects generated by the Particle System that, with the proper settings and along with all the other particles, shows a visual effect.
PARTICLE SYSTEM
As just said a Particle System is an object instance generated by the Particle Engine, in the Particle System we defines how it have to behave, how many particles it has to generate, at which speed, which textures it have to use, the particles speed, direction and so on.
We are going to analyze all the parameteres that defines a Particle System, at least in my own vision and my own implementation 🙂
THE EMITTER
Every Particle System has an Emitter which is the point from where all particles are generated. Since the Emitter is basically a point it has the x and y coordinates, but it also has the following properties :
- Delay
Since the Particle System is updated every rendering cycle, the Delay property specify how many cycles we have to skip to generate one or more particles.
Set it to 0 to generate particles every rendering cycle, set it to 4 to generate particles every 4 cycles. If your screen refresh rate is 50 FPS every rendering cycle should be long 20 milliseconds, it means that every 20*4 milliseconds (80ms) a new particle(s) will be generated.
The particle generation is called emission. - Quantity
This property specify how many particles we have to create at each generation loop. Think about the previous property, where we have set Delay to 4: if we set Quantity to 5 means that every 4 cycles the Particle System will generate 5 particles in one go. - Count
This variable is used to determine how many emissions we want to achieve, if we set this property to -1 means infinite emissions, if for example we set it to 10 the Particle System will be stopped once 10 emissions will been completed. - SpreadX
This value determines the horizontal spread expressed in pixels. This can be very useful for effects like snow, rain, and so on…. To make it clear how it works here is an example :
– Set the x coordinate to 100, this is emitter’s horizontal position
– Set the SpreadX property to 10
With the above settings, every time a particle is generated the particle’s horizontal coordinate will be between x-spreadX and x+spreadX => (100-10) and (100+10) => between 90 and 110. - SpreadY
This is the same as SpreadX but applied to the vertical coordinate.
That’s all for now, next time we will see how we can add a basic gravity simulation for our particles and the particle’s properties that defines their behaviour.
Happy Coding!