Particle System: Particle Generation

I have a system that generates particles from sources and updates their positions. Currently, I have written a program in OpenGL that calls my GenerateParticles(...) and UpdateParticles(...) and displays my output. One functionality that I would like my system to be able to generate n particles per second. In my GenerateParticles(...) and UpdateParticles(...) functions, I accept 2 important parameters: current_time and delta_time . In UpdateParticles(...) I update the position of my particle using the following formula: new_pos = curr_pos + delta_time*particle_vector . How can I use these parameters and global variables (or other mechanisms) to get n particles per second?

+4
source share
3 answers

You need to be careful, the naive way to create particles will allow you to create fractional particles for low values ​​of n (probably not what you want). Instead, create a battery variable that sums with your delta_time values ​​for each frame. Each frame checks it to see how many particles you need to create for this frame and subtract the corresponding amounts:

 void GenerateParticles(double delta_time) { accumulator += delta_time; while (accumulator > 1.0 / particles_per_second) { CreateParticle(...); accumulator -= 1.0 / particles_per_second; } } 

Make sure you add a limit so that if the delta is long, you are not creating a million particles at the same time.

+2
source

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()) // Assumes r() is a PRNG generating a random value in [0, 1) i++; 

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.

+1
source

You just need to create n * delta_time particles in GenerateParticles .

0
source

Source: https://habr.com/ru/post/1307390/


All Articles