抛射距离/时间
我正在制作一个游戏,需要计算出一个物体需要多长时间才能落到某个高度。
该物体具有初始 y 值 (y)、初始垂直速度 (vy)、重力常数 (gravity) 以及预期下降的垂直目标距离(目的地)。
我可以使用循环来解决这个问题:
int i = 0;
while(y < destination) {
y += vy;
vy += gravity;
i++;
}
return i;
唯一的问题是我需要对数百个对象执行此操作,并且必须每帧都执行此操作。
有什么办法可以使用某种公式来解决这个问题吗?这样我就可以加快游戏速度,同时仍然解决这个问题。
谢谢
I'm making a game and need to figure out how long it will take an object to fall to a certain height.
The object has an initial y value (y), an initial vertical velocity (vy), a gravity constant (gravity), and the vertical target distance it is supposed to drop (destination).
I can figure this out using a loop:
int i = 0;
while(y < destination) {
y += vy;
vy += gravity;
i++;
}
return i;
The only problem with this is that I need to do this for several hundred objects and I have to do it every frame.
Is there any way to figure this out using some kind of formula? That way I can speed up my game while still figuring out this problem.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用基本物理(运动学)明确地解决这个问题。
给定初速度 v、恒定加速度 a 和固定距离 x,时间为:
(1/2)at^2 + vt - x = 0
或
at^2 + 2vt - 2x = 0
求解该二次公式并取积极的时间。
You can solve this explicitly using elementary physics (kinematics).
Given an initial velocity v, constant acceleration a and a fixed distance x, the time is:
(1/2)at^2 + vt - x = 0
or
at^2 + 2vt - 2x = 0
Solve that quadratic formula and take the positive time.
查看:
http://en.wikipedia.org/wiki/Equations_for_a_falling_body
Check out:
http://en.wikipedia.org/wiki/Equations_for_a_falling_body
唯一的问题是我需要对数百个对象执行此操作,并且必须每一帧都执行此操作。
我认为如果性能对您来说至关重要,那么可以使用包含大量
的公式*
和^
可能不适合您的情况。我不知道你的目的是什么,是否需要模拟准确,但我认为你可以查看 粒子系统。尽管这只是一种通用方法,并且不会直接加速您的程序,但该领域正在进行的许多研究可能会有用。另外,您可以阅读此以了解更多信息。
另外,由于您的方法仅使用
+
,我相信它非常有效。除非你的对象真的很难渲染,否则几百个不会有问题。大多数公式可能会让您的程序看起来更好,但并不能更好地运行。仅供参考,我曾经在一台相当旧的机器上渲染了近 10000 个粒子,效果非常好。The only problem with this is that I need to do this for several hundred objects and I have to do it every frame.
I think if performance is critical for you, then a formula with lots of
*
s and^
s probably won't suit your situation.I don't know what your purpose is, whether you need the simulation to be accurate or not, but I think you could check out Particle System. Although it's just a general method and it won't speed up your program directly, there are many researches going on in this field that might be useful. Also, you could read this to know more about this.
Also, since your method only use
+
, I believe it is quite efficient. Unless your objects are really hard to render, a few hundred won't be a problem. Most of the formulas will probably make your program looks better but does not work better. FYI, I once rendered almost 10000 particles on a pretty old machine and it worked great.您想知道在给定初始(垂直)速度 v 和加速度(由于重力)ad 需要多少帧嗯>。
n 帧后行驶的距离为
因此设 d = vn + ½an (n−1) 并求解n:
然后使用二次公式得到n:
其他一些答案已向您推荐了牛顿运动方程的常用解,但是这些仅适用于连续方程。这里有一个离散模拟,因此,如果您想要准确而不是近似的答案,那么您需要求和而不是积分,如上所述。
我第一次编写这种预测代码是在一个坦克互相发射炮弹的游戏中。为了帮助瞄准,游戏在地面上预测炮弹落地的位置画了一个目标标线。在我的第一次尝试中,我使用了连续运动方程的正规解,但结果却相差甚远。模拟的离散性对结果产生显着差异,对于某些应用(例如绘制目标标线),预测和模拟之间的一致性至关重要。
You want to know how many frames it will take to travel a distance d, given initial (vertical) velocity v and acceleration (due to gravity) a.
After n frames the distance travelled is
So set d = vn + ½an(n−1) and solve for n:
And then use the quadratic formula to get n:
Some of the other answers have referred you to the usual solutions to Newton's equations of motion, but these only work for the continuous equations. You have a discrete simulation here, so if you want accurate rather than approximate answers then you need sums rather than integrals, as described above.
The first time I had to write this kind of prediction code was in a game involving tanks firing artillery shells at each other. To assist with aiming, the game drew a target reticule on the ground at the predicted position that the shell would land. For my first attempt I used the normal solutions to the continuous equations of motion, and the result was quite a way off. The discreteness of the simulation makes a noticeable difference to the result, and for some applications (like drawing a target reticule) agreement between the prediction and the simulation is vital.