粒子系统:粒子生成
我有一个系统,可以从源生成粒子并更新它们的位置。目前,我已经用 OpenGL 编写了一个程序,它调用我的 GenerateParticles(...)
和 UpdateParticles(...)
并显示我的输出。我希望我的系统具有的一项功能是每秒能够生成 n 个粒子。在我的 GenerateParticles(...)
和 UpdateParticles(...)
函数中,我接受 2 个重要参数:current_time
和 delta_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要小心,创建粒子的简单方法会让您创建低值 n 的分数粒子(可能不是您想要的)。相反,创建一个累加器变量,该变量与每帧的
delta_time
值相加。每个帧都会检查它,看看需要多少个粒子来创建该帧,并减去适当的数量:确保设置一些限制,这样如果时间增量很大,您就不会一次创建一百万个粒子。
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 yourdelta_time
values each frame. Each frame check it to see how many particles you need to create that frame and subtract the appropriate amounts: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.
一帧发射的粒子数量可以通过以下方式计算:
基本上,您可以发射
i
个粒子。可以为后面的帧累积小数部分
f
。当它累积大于 1 时,您可以发射整数数量的粒子:但是,以下是分数粒子数量的另一个有趣的解决方案。
通过使用伪随机数生成器(PRNG),我们可以用它来确定是否应该发射粒子:
当频率随时间变化时,这种方法非常有用。并且它消除了存储额外变量的需要。
这种方法的另一个优点是,粒子系统看起来不太均匀。例如,如果频率为 1.5,增量时间为 1,则使用第一种方法,帧将发射一系列 1, 2, 1, 2, ... 粒子。第二种方法可以打破这种模式。
此外,您可以使用
modf()
提取浮点数的整数和小数部分。The number of particles emitted at a frame can be computed by:
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: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:
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.您只需在
GenerateParticles
中创建n * delta_time
粒子即可。You just need to create
n * delta_time
particles inGenerateParticles
.