粒子系统:粒子生成

发布于 2024-08-29 07:16:56 字数 470 浏览 4 评论 0原文

我有一个系统,可以从源生成粒子并更新它们的位置。目前,我已经用 OpenGL 编写了一个程序,它调用我的 GenerateParticles(...)UpdateParticles(...) 并显示我的输出。我希望我的系统具有的一项功能是每秒能够生成 n 个粒子。在我的 GenerateParticles(...)UpdateParticles(...) 函数中,我接受 2 个重要参数:current_timedelta_time。在 UpdateParticles(...) 中,我根据以下公式更新粒子的位置:new_pos = curr_pos + delta_time*article_vector。如何使用这些参数和全局变量(或其他机制)每秒产生 n 个粒子?

I have a system that generates particles from sources and updates their positions. Currently, I have written a program in OpenGL which calls my GenerateParticles(...) and UpdateParticles(...) and displays my output. One functionality that I would like my system to have is being 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 according to the following formula: new_pos = curr_pos + delta_time*particle_vector. How can I use these parameters and global variables (or other mechanisms) to produce n particles per second?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-09-05 07:16:56

您需要小心,创建粒子的简单方法会让您创建低值 n 的分数粒子(可能不是您想要的)。相反,创建一个累加器变量,该变量与每帧的 delta_time 值相加。每个帧都会检查它,看看需要多少个粒子来创建该帧,并减去适当的数量:

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

确保设置一些限制,这样如果时间增量很大,您就不会一次创建一百万个粒子。

You need to be careful, the naive way of creating particles will have you creating fractional particles for low values of n (probably not what you want). Instead create an accumulator variable that gets summed with your delta_time values each frame. Each frame check it to see how many particles you need to create that frame and subtract the appropriate 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 put some limiting in so that if the time delta is large you don't create a million particles all at once.

苯莒 2024-09-05 07:16:56

一帧发射的粒子数量可以通过以下方式计算:

double n = delta_time * frequency
int i = (int)n;
f = n - i;

基本上,您可以发射 i 个粒子。

可以为后面的帧累积小数部分f。当它累积大于 1 时,您可以发射整数数量的粒子:

f_sum += f;
if (f_sum > 1.0) {
    int j = (int)f_sum;
    f_sum -= j;
    i += j;
}

但是,以下是分数粒子数量的另一个有趣的解决方案。

通过使用伪随机数生成器(PRNG),我们可以用它来确定是否应该发射粒子:

if (f >= r()) // Assumes r() is a PRNG generating a random value in [0, 1) 
    i++;

当频率随时间变化时,这种方法非常有用。并且它消除了存储额外变量的需要。

这种方法的另一个优点是,粒子系统看起来不太均匀。例如,如果频率为 1.5,增量时间为 1,则使用第一种方法,帧将发射一系列 1, 2, 1, 2, ... 粒子。第二种方法可以打破这种模式。

此外,您可以使用 modf() 提取浮点数的整数和小数部分。

The number of particles emitted at a frame can be computed by:

double n = delta_time * frequency
int i = (int)n;
f = n - i;

Basically you can emit i particles.

The fractional part f can be accumulated for the later frames. When it accumulates greater than one you can emit the integral number of particles:

f_sum += f;
if (f_sum > 1.0) {
    int j = (int)f_sum;
    f_sum -= j;
    i += j;
}

However, the following is another interesting solution for fractional number of particle.

By using a pseudo random number generator (PRNG), we can use it to determine whether 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 respect to time. And it eliminates the need of storing 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 delta time is 1, using the first approach, the frames will emit a sequence of 1, 2, 1, 2, ... particles. The second approach can break this pattern.

Besides, you may use of modf() to extract the integral and fractional part of a floating point number.

执手闯天涯 2024-09-05 07:16:56

您只需在 GenerateParticles 中创建 n * delta_time 粒子即可。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文