问题描述
在我的2D游戏,我想创建一个智能机器人谁可以拍的球员。假设我可以通过我的机器人:
in my 2d game I would like to create an intelligent bot who can "shoot" to the player.Suppose I can pass to my bot:
actual xEnemy, yEnemy
also enemy speed and angle direction
我如何计算在哪里拍摄考虑到机器人必须旋转的枪往好的方向发展?
How can I calculate "where to shoot" considering that Bot must rotate its gun to the right direction?
这对我来说是一个非常大的问题,因为......我绝对不是在数学好!预先感谢您的precious帮助!
This is a really big problem for me, because.. I'm absolutely not good in math!Thanks in advance for your precious help!
推荐答案
记号:我写在大写字母,小写的标量,并∠V矢量,该矢量V使与X轴的角度。 (你可以在许多语言功能 ATAN2
计算。)
Notation: I write vectors in capital letters, scalars in lower case, and ∠V for the angle that the vector V makes with the x-axis. (Which you can compute with the function atan2
in many languages.)
最简单的例子是一个固定的射手可以立即转动。
The simplest case is a stationary shooter which can rotate instantly.
让目标是在位置A,并以速度VA移动和射手是固定在位置B和可以发射子弹速度的取值的。让射手火在时间0子弹击中在时间的 T 的使得| A - B +的 T 的VA | = T小号的。这是一个简单的二次方程中的 T 的,你应该能够轻松解决(或确定,有没有解决方案)。在确定的 T 的,你现在就可以计算出发射角度,这仅仅是∠(A - B +的 T 的VA)。
Let the target be at the position A and moving with velocity VA, and the shooter be stationary at the position B and can fire bullets with speed s. Let the shooter fire at time 0. The bullet hits at time t such that |A − B + t VA| = t s. This is a straightforward quadratic equation in t, which you should be easily able to solve (or determine that there is no solution). Having determined t, you can now work out the firing angle, which is just ∠(A − B + t VA).
现在假设射手不是静止的,但有恒定的速度VB。 (我假定牛顿相对论这里,即子弹速度被添加到射手的速度。)
Now suppose that the shooter is not stationary but has constant velocity VB. (I'm supposing Newtonian relativity here, i.e. the bullet velocity is added to the shooter's velocity.)
它仍然是一个简单的二次方程制定出时间来打:| A - B +的 T 的(VA - VB)| = T小号的。在这种情况下,触发角∠(A - B +的 T 的(VA - VB))。
It's still a straightforward quadratic equation to work out the time to hit: |A − B + t(VA − VB)| = t s. In this case the firing angle is ∠(A − B + t (VA − VB)).
如果射手等待,直到时间的 U 的发射前?然后,子弹击中目标时| A - B +的 T 的(VA - VB)| =( T 的 - 的 U 的)的取值的。触发角仍然是∠(A - B +的 T 的(VA - VB))。
What if the shooter waits until time u before firing? Then the bullet hits the target when |A − B + t(VA − VB)| = (t − u) s. The firing angle is still ∠(A − B + t(VA − VB)).
现在您的问题。假设射手能够及时完成半圈的研究的。然后,它一定能火在时间的研究的。 (基本上是:制定出必要的射击角度,如果有的话,一个镜头在时间的研究的,如上所述,旋转到这个角度,停止,等待时间的研究的,随后起火。)
Now for your problem. Suppose that the shooter can complete a half rotation in time r. Then it can certainly fire at time r. (Basically: work out the necessary firing angle, if any, for a shot at time r, as described above, rotate to that angle, stop, wait until time r, then fire.)
但你可能想知道的最早的的时间,让射手可以射击。在这里你可能想使用逐次逼近找到它。 (算法的素描:???你可以火在时间0号你能开火时的研究的是你能不能开火时半的研究的编号等)
But you probably want to know the earliest time at which the shooter can fire. Here's where you probably want to use successive approximation to find it. (Sketch of algorithm: Can you fire at time 0? No. Can you fire at time r? Yes. Can you fire at time ½ r? No. etc.)
这篇关于人工智能算法"拍摄"在2D游戏的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!