# How do I implement "prewarming" in particle engines?

How is prewarming in particle engines made?

I have a particle engine and need to support this, which is a feature I have already seen in Unity.

Is it done at compilation, like informations stored somewhere? or pre-processed at launch? or is there a way to simulate a particle system when it is created?

Considering it is time based, I don't see how I could for example simulate 15 seconds of life for a particle system without actually using 15 seconds

• You wouldn't have to use 15 seconds because you could quickly iterate through the simulation, stepping it at intervals that approximate real timesteps, without actually needing to wait for those timesteps. Jun 27, 2014 at 18:38
• Yes I just found a way to do it that works, I'll update my post soon with the solution used. Thanks! Jun 27, 2014 at 18:43
• My bad, that's what I meant actually Jun 28, 2014 at 8:27

I ended up with a simple idea that worked surprisingly good, so here it is:

In my particle engine, I generate n particles per gameloop tick, and running at 60 FPS we have, for an elapsed time t in seconds, n * 60 * t particles generated.

This could vary if your game isn't fluid and frames drop, but we assume it runs correctly.

Now what happens is that the prewarn function is launched once the particle emitter starts, and emits this number of particles. Then we have to simulate where they are, because they should have been all generated at different dates, when in truth they haven't.

All of this is done with a simple for loop (example in JavaScript):

particles.prewarm = function () {
// FPS = 60, this.rate = number of particles per gameloop tick, this.life = life of a particle in seconds
var particlesCount = (FPS * this.rate) * this.life; // Gets the number of particles to emit
var timeUnit = this.life / particlesCount; // This is the amount of time between two particles for the duration of our simulation

for (var i = 0; i < particlesCount; i++) {
var p = this.emit(); // Emit a random particle

// Here, we set the "birth" date of each particle at their simulated point in time
p.birth = time.time - this.life + timeUnit * i;
p.update (time.time - p.birth); // Here I call my particle's update with a special deltaTime which corresponds to the duration of the simulation for this particle
}
}

• BTW, just as a simplification, timeUnit is equal to 1.0 / (FPS * this.rate), since the factors of this.life cancel out. Jun 28, 2014 at 8:45
• True, there's lot of room for optimization in this code anyway Jun 28, 2014 at 8:48