粒子瞄准算法
我正在构建一个粒子系统,我想添加的功能之一是“目标”功能。我想要做的是为每个粒子设置一个 X,Y 目标并使其到达那里,但不是沿着一条直线(废话),而是考虑到应用于粒子的所有其他运动效果。
我的粒子具有的相关参数:
- posx, posy :具有任意值的 inits。在每个刻度上,speedx 和 speedy 分别添加到 posx 和 posy
- speedx, speedy :具有任意值的 inits。在每个刻度上,accelx 和 accely 分别添加到 speedx speedy(如果有)
- accelx、accely:具有任意值的 inits。目前的实现在粒子的整个生命周期中保持不变。
- life :从任意值开始,系统每次tick减1。
我想要实现的是粒子在其最后一个生命周期到达目标 X,Y,同时从其原始值(速度和加速度)开始,因此朝向目标的运动将看起来“平滑”。我正在考虑将其朝目标方向加速,同时重新计算每次滴答所需的加速力。但感觉不太对劲,很想听听一些建议。
I'm building a particles systems, one of the features I'd like to add is a "target" feature. What I want to be able to do is set an X,Y target for each particle and make it go there, not in a straight line though (duh), but considering all other motion effects being applied on the particle.
The relevant parameters my particles have:
- posx, posy : inits with arbitrary values. On each tick speedx and speedy are added to posx and posy respectively
- speedx, speedy : inits with arbitrary values. On each tick accelx and accely are added to speedx speedy respectively if any)
- accelx, accely : inits with arbitrary values. With current implementation stays constant through the lifespan of the particle.
- life : starts with an arbitrary value, and 1 is reduced with each tick of the system.
What I want to achieve is the particle reaching the target X,Y on it's last life tick, while starting with it's original values (speeds and accelerations) so the motion towards the target will look "smooth". I was thinking of accelerating it in the direction of the target, while recalculating the needed acceleration force on each tick. That doesn't feel right though, would love to hear some suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于“平滑”运动,您可以保持速度恒定、加速度恒定或加加速度恒定。这取决于你所说的“顺利”和你所说的“无聊”。让我们保持加速度恒定。
从物理学的角度来看,你有这个约束
,因为行进的距离是
v*t+1/2at^2
。求解未知加速度给出(为此,速度必须与时间采用相同的单位,例如“每刻度像素”,而生命是“刻度数”。)
因为您使用 欧拉积分,这不会使粒子精确地落在目标上。但我怀疑这会是一个真正的问题。
效果就像一个魅力:
另一张图片,这次有持续的急动
看起来曲线中还有另一个弯曲......
Java 代码
For a "smooth" motion, you either keep the speed constant, or the acceleration constant, or the jerk constant. That depends on what you call "smooth" and what you call "boring". Let's keep the acceleration constant.
From a physics point of view, you have this constraint
Because distance traveled is
v*t+1/2at^2
. Solving for the unknown acceleration gives(For this to work speedy must be in the same unit as time, for example "pixels per tick" and life is a number of "ticks". )
Since you use euler integration, this will not bring the particle exactly on the target. But I doubt it'll be a real issue.
Works like a charm:
Another picture, this time with constant jerk
Looks like there is another bend in the curve...
Java code
您可以认为您的颗粒最初“施加”了一个力 (Fv),该力对应于其初始速度所具有的惯性。然后应用与目标距离成正比的吸引力 (Fa)。然后,您可以将这些力相加,并给定粒子权重,您可以推导出在时间 t 时考虑的加速度。
然后你可以像往常一样从 v(t-1) 和 a(t) 计算 v(t)
编辑:我忘记了粒子的生命可以直接从到目标的距离计算(例如:生命 = 距离 / 初始距离将从 1 开始,接近 0 接近目标)
编辑:您可以将其视为一种磁铁。请参阅维基百科了解力公式。
You could consider that your particule is initially "applied" a force (Fv) which corresponds to the inertia it has from its initial velocity. Then you apply an attraction force (Fa) that is proportionnal to the distance to the target. You can then sum those forces, and given a particle weight, you can deduce acceleration to consider at time t.
Then you can compute v(t) from v(t-1) and a(t) as usual
Edit: I forgot the life of the particle can directly be computed from the distance to the target (for instance: life = distance / initialDistance will go from 1 at start and approch 0 near the target)
Edit: You could think of this as a kind of magnet. See wikipedia for the force formula.
您可以使用的一种运动是匀加速 http://en.wikipedia.org/wiki/ Acceleration#Uniform_acceleration
你的粒子将平滑地移向目标并以相当高的速度撞击它。
为了满足你规定的标准,请执行以下操作:
计算粒子与目标的距离,粒子将在它的生命周期结束了,假设速度从现在开始不会改变。
这个距离放入这个等式中:http://upload.wikimedia .org/math/6/2/9/6295e1819e6bfe1101506caa4b4ec706.png 并求解
使用它作为您的加速度
分别为 x 和 y 执行此操作
one kind of movement you can use is the uniform acceleration http://en.wikipedia.org/wiki/Acceleration#Uniform_acceleration
Your particles will make a smoth move towards the target and hit it with rather high velocity
For meeting your stated criteria, do the following:
calculate the distance from the target, the particle will have at the end of it's life time, assuming the speed doesn't change from now on.
this distance put in this equation: http://upload.wikimedia.org/math/6/2/9/6295e1819e6bfe1101506caa4b4ec706.png and solve it for a
use this as your acceleration
Do this seperately for x and y