2d 游戏:通过预测弹丸和单位的交点向移动目标开火 [英] 2d game : fire at a moving target by predicting intersection of projectile and unit
问题描述
好的,这一切都发生在一个美好而简单的 2D 世界中... :)
Okay, this all takes place in a nice and simple 2D world... :)
假设我在 Apos 位置有一个静态物体 A,在 Bpos 有一个 bVelocity 的线性移动物体 B,以及一个速度为 Avelocity 的弹药...
Suppose I have a static object A at position Apos, and a linearly moving object B at Bpos with bVelocity, and an ammo round with velocity Avelocity...
考虑到 B 的线速度和 A 的弹药速度,我如何确定 A 必须射击、击中 B 的角度?
How would I find out the angle that A has to shoot, to hit B, taking into account B's linear velocity and the speed of A's ammo ?
现在瞄准的是物体的当前位置,这意味着当我的射弹到达那里时,单位已经移动到更安全的位置:)
Right now the aim's at the current position of the object, which means that by the time my projectile gets there the unit has moved on to safer positions :)
推荐答案
首先旋转轴使 AB 垂直(通过旋转)
First rotate the axes so that AB is vertical (by doing a rotation)
现在,将 B 的速度向量分成 x 和 y 分量(比如 Bx 和 By).您可以使用它来计算您需要射击的向量的 x 和 y 分量.
Now, split the velocity vector of B into the x and y components (say Bx and By). You can use this to calculate the x and y components of the vector you need to shoot at.
B --> Bx
|
|
V
By
Vy
^
|
|
A ---> Vx
你需要 Vx = Bx
和 Sqrt(Vx*Vx + Vy*Vy) = 弹药速度
.
这应该会为您提供新系统中所需的向量.转换回旧系统,您就完成了(通过向另一个方向旋转).
This should give you the vector you need in the new system. Transform back to old system and you are done (by doing a rotation in the other direction).
这篇关于2d 游戏:通过预测弹丸和单位的交点向移动目标开火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!