一些物理洞察力
1 ) 对于目标是“点对象”
所以你必须解决向量方程
位置bullet [ time=t1 > t0 ] == Positiontarget [ time=t1 > t0 ] -- (Eq 1)
位置由运动(也是矢量)方程给出
位置object [ t ] = 位置object [ t0 ] + 速度object * ( t - t0 )
现在,子弹能够到达目标的条件是方程 1 有 x 和 y 的解。让我们写下 x 的方程:
Xbullet [ t0 ] + SpeedXbullet * ( t - t0 ) = Xtarget [ t0 ] + SpeedXtarget * ( t - t0 )
所以对于碰撞时间我们有
( t碰撞 - t0 ) = (xtarget [ t 0 ] - xbullet [ t0 ] ) / (SpeedXbullet - SpeedXtarget) -- (Eq 2)
由于我们需要 t > t0 的解决方案,这意味着有一个截距就足够了>
符号 ( xtarget[ t0 ] - xbullet[ t0 ] ) = 符号 ( SpeedXbullet - SpeedXtarget ) -- (Eq 3)
Which tells us the evident fact that if an object is moving faster than the other, and in the same direction, they will eventually collide.
从公式 2 中,您可以看到对于给定的 SpeedXtarget存在无限解(正如其他答案中已经指出的那样)对于 t 和 SpeedX子弹,所以我认为你的规格不完整。
I guess (as stated in a commentary I made in another answer) thinking in a "tower defense" kind of game, that your bullets have a limited range.
所以你还需要另一个约束:
距离 [ 位置目标 [ t碰撞 - t0 ] - 位置子弹 [ t 0 ] ]
Which still permits infinite solutions, but bounded by an upper value for the Collision time, given by the fact that the target may abandon the range.
此外,距离由下式给出
距离[v,u]= +Sqrt[ (Vx-Ux)^2 + (Vx-Vy)^2 ]
所以,等式 4 变为,
(Xtarget[t碰撞 - t0] - Xbullet[t0])2 + (Ytarget[t碰撞 - t0] - Ybullet[t0])22 -- (Eq 5)
注意 { Xbullet[t0] , Ybullet[t0} 是塔位置。
现在,在公式 5 中替换目标位置的值:
(Xtarget[t0] + SpeedXtarget * (tt0) - Xbullet[t0])2 + (Y目标[t0] + SpeedY目标 * (tt0) - Ybullet[t0])22 -- (方程 6)
调用初始距离:
Dxt0 = Xtarget[t0] - Xbullet[t0]
和
Dyt0 = Ytarget[t0] - Ybullet[t0]
等式 6 变为
(Dtx0 + SpeedXtarget * (tt0) )2 + (Dty0 + SpeedYtarget * (tt0))22 -- (Eq 7)
这是要在t-t0求解的二次方程。正解将为我们提供最大的碰撞时间。之后目标将超出范围。
正在调用
速度目标2 = SpeedX目标2 + SpeedY目标2
和
H = Dtx0 * SpeedX目标 + Dty0 * SpeedY目标
T碰撞最大值 = t0 - ( H
+/- Sqrt (BulletRange2 * Speedtarget2 - H2) ) / Speedtarget 2
So you need to produce the collision BEFORE this time. The sign of the
square root should be taken such as the time is greater than t0
After you select an appropriate flying time for your bullet from the visual
effects point of view, you can calculate the SpeedX and SpeedY for the bullet
from
SpeedXbullet = ( Xtarget [ t0 ] - Xbullet [ t0 ] ) / ( t碰撞 - t0 ) + SpeedX目标
和
SpeedYbullet = ( Ytarget [ t0 ] - Ybullet [ t0 ] ) / ( t碰撞 - t0 ) + SpeedY目标
2 ) 对于目标和塔是“广泛的对象”
现在,对于目标是半径为 R 的圆的情况进行概括是微不足道的。你得到的,相当于子弹的“扩展范围”。该扩展名只是 R。
因此,将 BulletRange 替换为 (BulletRange + R),您将获得最大允许碰撞时间的新方程。
如果您还想考虑大炮的半径,同样的考虑也适用,给出“双扩展范围
NewBulletRange = BulletRange + R目标 + R塔
无限范围子弹
如果您决定某些特殊项目符号不应具有范围(和检测)限制,则仍然存在屏幕边框限制。但处理起来有点困难。如果您需要这种弹丸,请发表评论,我会尝试做一些数学运算。