Leges Motus Progress – Basic Particle Effects

One of the main reasons we wanted to complete the Leges Motus revamp was to improve the graphical quality and special effects capabilities of the game. One of my top-priority goals in this vein was to create a particle-effect system that would be able to add some visual flair to firing, getting hit, and so forth.

So, a couple of days ago, I got it into my head to make this happen, and I have now implemented a basic particle effects system, which, while still likely needing some expansion and optimization, will allow a wide range of cool effects to be created with relative ease. Below is a screenshot of a test application using the new system to create a fireworks-like effect.

Leges Motus particle system test.

A test of the new basic particle effects system for Leges Motus, showing some simple fireworks-like bursts.

To create this system, I took some inspiration from the following papers: “The Ocean Spray in Your Face” (Jeff Lander, 1998) and “Building an Advanced Particle System” (John Van der Burg, 2007). These are excellent resources, and I highly recommend them for their simple and understandable explanations, though I found that they left a few crucial details lacking.

First, they provide little information on rendering questions, such as adding shaders to particle effects and optimizing your drawing routine. I have not yet switched to rendering particles with GL_POINT_SPRITE, instead of quads like a normal sprite, but it will probably be important to do so for performance reasons. I have also not yet really experimented with shaders on particle effects, but I expect to look into that eventually as an addition to the system.

The basic system is as follows:

There is a simple Particle class, with important properties for each particle to keep track of separately:

Point m_pos;
Point m_prev_pos;
Vector m_vel;
uint64_t m_energy_left;
uint64_t m_initial_energy;
Color m_color;
float m_size;

ParticleEmitter is a base class, which provides the basic functionality that is required to spawn a bunch of particles. This functionality is then extended by each specific type of emitter. Currently, I have only implemented a single specific emitter type, a basic burst-like emitter that is surprisingly flexible.

class ParticleEmitter {
Image* m_image;
DrawContext::BlendMode m_blend_mode;
int m_alive_count;
Point m_center;
ParticleManager* m_manager;
std::list m_particles;
ParticleEmitter(ParticleManager* manager, Point center, Image* image, DrawContext::BlendMode mode = DrawContext::BLEND_ADD);
virtual ~ParticleEmitter();

void draw(DrawContext* context);

std::list& get_particles();

void clear();

Particle* request_particle();
void free_particle(Particle* particle, std::list::iterator it);

virtual bool update(uint64_t timediff);

Point get_center();

void set_center(Point center);
void set_center(float center_x, float center_y);

Each specific emitter type will be given its own settings, in the form of a struct. The struct for the SimpleRadialEmitter class looks like:

struct SimpleRadialEmitterSettings {
float particle_speed;
float speed_variance;
int spawn_per_second;
int spawn_variance;
uint64_t lifetime_millis;
uint64_t lifetime_variance;
float rotation_rads;
float rotation_variance;
Vector global_force;
int max_spawn;
uint64_t emitter_stop_spawning_millis;
uint64_t emitter_lifetime_millis;

Finally, all the emitters currently active are managed by the ParticleManager class, which runs their update and draw loops and deletes them when they’re done. It also contains a (potentially resizable) pool of particles, to avoid object creation/deletion too frequently during gameplay.

The ParticleManager is also a “Widget,” which means it can be placed into any other widget in the Leges Motus graphics framework. This allows the particle system to be easily overlaid on the existing game display, or anywhere else we need it.

This development is moving slowly, due to the final other programmer, Archaemic, no longer having time to work on the project. Going it alone, it’ll take me quite a while to get all the features (especially menus) done. I don’t plan to give up, though.


~ by greywhind on November 24, 2011.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: