Particle system
Among many things i have worked on a particle system this week. At first i was not sure how to start, i had only done this once or twice in another programming language (C#). What I am about to explain is the current state of the system, it is not what i indent to have at the end meaning that it is not finished yet.
So far I have created two classes, a ”Particle” class and a ”ParticleEmitter” class. I’ll start with the particle class.
Particle: The particle contains some variables which stores information on how the particle will behave:
class Particle
{
private:
//Stores the current speed of which the particle is moving
sf::Vector2f m_velocity;
//Acceleration will be added to the velocity every frame
sf::Vector2f m_acceleration;
//Stores the particles sprite
sf::Sprite m_sprite;
//The lifetime of the particle (How long will it be showed on the screen)
float m_secondsToLive;
//The current life of the particle, goes from 255 to 0
int m_life;
//A timer used to decrease life and change particle in other ways
float m_timer;
//A boolean if the particle is currently active or not
bool m_active;
//A boolean if the particle should scale itself or not
bool m_scaleParticle;
};
NOTE: This is only the member variables of the Particle class.
So over to the ParticleEmitter…
The manager’s job is to create X number of particles in the beginning of the game, update and draw all active particles and to remove all particles when the application exits. So the Emitter has a list of particles and it activates the particle at a certain spawn rate in its update method. This is the Emitters member variables and its update method.
void ParticleEmitter::Update(float deltaTime)
{
//Increase timer
m_timer += deltaTime;
//Check for timer tick
if (m_timer > m_spawnRate)
{
//Reset timer
m_timer = 0;
//Create a new particle
CreateParticle();
}
//Update all active particles
for (int i = 0; i < m_particles.size(); i++)
{
if (!m_particles[i]->Active())
continue;
m_particles[i]->Update(deltaTime);
}
}
private:
//Contains an area which a particle is randomly created within
sf::IntRect m_area;
//Stores all the created particles
std::vector<Particle*> m_particles;
//What velocity to activate a particle in
sf::Vector2f m_startVelocity;
//What acceleration to give an activated particle
sf::Vector2f m_acceleration;
//What texture the particle should have
sf::Texture* m_particleTexture;
//Stores the particles lifetime
float m_secondsToLive;
//A boolean if the particle should be scaled or not
bool m_scaleParticle;
//At what rate should we activate each particle (milliseconds)
float m_spawnRate;
//A timer used to activate particles
float m_timer;
I will give an example of how it works… When I create a new Emitter I decided how many particles the Emitter will create (maybe 50-100) and also set a spawn rate and a texture for the particles.
The Emitter will then create said number of particles and store them as inactive in the vector.
When I update the Emitter its timer will tick and ”Create” a new particle (Activate an inactive one). The particle will then update itself with its velocity and other variables. When the particles life goes below 0 it will deactivate itself.
By setting a high spawn rate and having the particles fade out and decrease in scale a pretty cool effect appears. This is a picture of how it looks when i apply a Emitter to a monster. I give it a water ripple/foam texture and apply a velocity opposite from the monsters movement direction.
There is also a temporary emitter on the thrown object (The freezer). This looks a lot better when it is in action. But you can see some particles flowing upwards behind the monster and some blue clouds flying downwards from the freezer.
The meaning of the particle emitter is to enhance the effect of mainly the monsters. The lava monster will probably have som flame particles on him and the ice monster will maybe have some frost clouds flying out from him.
As i said earlier, this system is not complete. I will implement more such as choosing a color for each particle and being able to create an explosion from the emitter.