我发现了一个有趣的项目,该项目需要计算射击子弹的角度才能击中移动的物体/目标。此功能应提供五个参数。这是参数列表:
`xP` - last known x position of the target
`yP` - last known y position of the target
`xV` - last known x velocity of the target
`yV` - last known y velocity of the target
`bulletS` - the speed of the bullet
例如,如果我提供如下参数集:
xP yP xV yV bulletS
5 5 0 1 3
4 4 1 0 2
5 5 0 -1 3
到目前为止,我已经能够计算出距离,但是我不确定这是否正确。这是示例:
import java.lang.*;
public class Problem2 {
public static void main(String[] args) {
// Function takes arguments from the parameters table.
System.out.println(calculateShotAngle(10,10,1,0,2));
}
static double calculateShotAngle(float xP, float yP, float xV, float yV, float pSpeed) {
// Function equation to calculate distance
double distance = Math.pow(Math.sqrt(Math.pow((xV-xP),2) + Math.pow((yV-yP), 2)),2);
return distance;
}
}
一旦获得距离,我应该使用子弹的速度来确定角度。我试图了解此算法应如何工作。我猜应该先计算目标和子弹之间的距离,然后检查子弹是否到达目标。如果有人有示例或提示应如何实现,请告诉我。谢谢。
最佳答案
这个问题很复杂,但是我将尝试做出一个描述性的答案。
我们需要建立一些常数,例如引力(目前仅引力):
double gravity = 0.98;
// Add more constants if needed
建立需求常数后,我们将进行计算。
=第1部分===========
首先,您需要使用弹丸运动公式来了解目标的移动位置。
以下是目标的需求变量:
`xPT` - last known x position of the target
`yPT` - last known y position of the target
`xVT` - last known x velocity of the target
`yVT` - last known y velocity of the target
之后,计算目标在时间
t
的位置。哪里:
Vx
是沿x轴的速度(您需要进行计算)Vxo
是沿x轴的初始速度(xVT
)Vy
是沿y轴的速度(您需要进行计算)Vyo
是沿y轴的初始速度(yVT
)g
是由于重力引起的加速度t
是花费的时间只需从1开始
t
,然后递增。 (播放初始值并递增以获得所需的输出)===========第2部分===========
在时间
t
计算目标位置后,如果在时间t
可以到达目标位置,则给定位置和速度,然后计算子弹的可能发射角;如果时间可以到达目标,则计算角度答案就是答案,如果不增加t
项目符号所需的变量是:
`xPB` - last known x position of the bullet
`yPB` - last known y position of the bullet
`bulletS` - the speed of the bullet
计算角度的公式为:
哪里:
v
是初始启动速度(这是bulletS
)g
是重力常数x
是目标在时间t
处的x位置(这是xPT
)y
是目标在t
时间的y位置(这是yPT
)===========第3部分===========
使用子弹的角度,速度和初始位置,检查子弹在时间
t
能否到达目标位置公式为:
哪里:
u
是初始启动速度(这是bulletS
)g
是重力常数θ
是发射角度Ux
是子弹的初始x速度Uy
是子弹的初始y速度之后,计算时间
t
的子弹位置。哪里:
x
是项目符号在时间t
的x位置y
是子弹在时间t
处的y位置Vx
是沿x轴的速度(您需要进行计算)Vxo
是沿x轴的初始速度(Ux
)Vy
是沿y轴的速度(您需要进行计算)Vyo
是沿y轴的初始速度(Uy
)g
是由于重力引起的加速度t
是花费的时间xPB
-项目符号的最后已知x位置yPB
-项目符号的最后已知y位置===========第4部分===========
现在您有了需要变量,它们是:
`xPB` - last known x position of the bullet
`yPB` - last known y position of the bullet
`xPT` - last known x position of the target
`yPT` - last known y position of the target
比较上述变量,如果
xPB
等于xPT
并且yPB
等于yPT
,则子弹将在时间t
和发射角度θ
击中目标。如果不是,则增加时间t
并执行第1部分直到第4部分。===========摘要===========
这就是程序的流程。
static double calculateShotAngle(
double xPT, double yPT, double xVT, double yVT,
double xPB, double yPB, double bulletS) {
double gravity = 0.98;
double angle = null;
double time = 1; // change this value if needed (try smaller increments if after a single increment the bullet's position will pass the target's position)
double xPTOriginal = xPt; // Save the initial x position of target
double yPTOriginal = yPt; // Save the initial y position of target
while (true) {
// do Part 1;
// do Part 2;
// do Part 3;
// below code is Part 4
if (hasBeenHit(xPT, yPT, xPB, yPB)) {
break;
} else {
t++; // increment t
// Revert the initial position of target
xPt = xPTOriginal;
yPt = yPTOriginal;
}
}
return angle;
}
// Method used to check if bullet hits the target
// Method assumes that bullet and target only occupies a point (1 of axis x and 1 of axis y)
static boolean hasBeenHit(double xPT, double yPT, double xPB, double yPB) {
return xPT == xPB && yPT == yPB;
}
希望您能理解我的解释(我花了很多时间来做。哈哈)但是,如果您有任何疑问/澄清,请随时发表评论。
关于javascript - 如何计算射击子弹以击中移动目标的 Angular ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917375/