How to emit a single particle using SFML's Thor Particle System


Usecase: shells dropping in sync with firing, fake bullets, etc

You must use a particle emitter to create particles, however this doesn’t mean it’s impossible to create single particles on command. You can create a particle emitter which simply adds particles from a queue to the system

#pragma once
#include <SFML/Graphics.hpp>
#include <Thor/Math.hpp>
#include <Thor/Graphics.hpp>
#include <Thor/Particles.hpp>

namespace sfext {

class SingleParticleEmitter
	std::vector<thor::Particle> *particles;
	SingleParticleEmitter(const SingleParticleEmitter& that);
	void operator() (thor::EmissionInterface& system, sf::Time dt);

	void push(thor::Particle particle);
	void push(sf::Vector2f position, sf::Vector2f velocity, float lifetime, float rot=0, float rotsp=0);

}; // end namesapce sfext

The above allows you to use the push functions to emit particles. Here is an example of it in use:

SingleParticleEmitter single_emitter;
thor::ParticleSystem particle_system;

// When you need to emit a shell:

single_emitter.push(origin, velocity, 1.0f);
// lifetime of the particle in seconds --^

Here is the cpp file:

#include "singleparticleemitter.hpp"

using namespace sfext;

	particles = new std::vector<thor::Particle>();

SingleParticleEmitter::SingleParticleEmitter(const SingleParticleEmitter& that)
	particles = that.particles;

void SingleParticleEmitter::operator() (thor::EmissionInterface& system, sf::Time dt)
	for (auto particle : *particles) {

void SingleParticleEmitter::push(thor::Particle particle)

void SingleParticleEmitter::push(sf::Vector2f position, sf::Vector2f velocity, float lifetime, float rot, float rotsp)
	thor::Particle particle(sf::seconds(lifetime));
	particle.position = position;
	particle.velocity = velocity;
	particle.rotation = rot;
	particle.rotationSpeed = rotsp;
	particle.scale = sf::Vector2f(1.f, 1.f);
	particle.color = sf::Color::White;
	particle.textureIndex = 0u;