The number of particles emitted in the frame can be calculated:
double n = delta_time * frequency int i = (int)n; f = n - i;
In principle, you can emit particles i .
The fractional part f may accumulate for subsequent frames. When it accumulates more than one, you can emit an integer number of particles:
f_sum += f; if (f_sum > 1.0) { int j = (int)f_sum; f_sum -= j; i += j; }
However, the following interesting solution for a fractional number of particles.
Using a pseudo random number generator (PRNG), we can use it to determine if a particle should be emitted:
if (f >= r())
This approach is useful when the frequency changes relative to time. And this eliminates the need to store an additional variable.
Another beauty of this approach is that the particle system will look less uniform. For example, if the frequency is 1.5, and the delta time is 1, using the first approach, the frames will emit a sequence of 1, 2, 1, 2, ... particles. The second approach may violate this pattern.
Alternatively, you can use modf() to extract the integral and fractional parts of the floating point number.
source share