javascript - 如何计算发射子弹以击中移动目标的角度?
问题描述
我发现了一个有趣的项目,它需要计算发射子弹以击中移动物体/目标的角度。此函数中应提供五个参数。以下是参数列表:
`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 部分 ===========
首先,您需要使用 Projectile Motion Formula 了解目标正在移动的位置。
以下是目标的需求变量:
`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
是所用时间
只需从t
1 开始,然后递增。(使用初始值和增量来获得所需的输出)
=========== 第 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
是目标在时间的 x 位置t
(这是xPT
)
y
是目标在时间的 y 位置t
(这是yPT
)
=========== PART 3 ============
使用子弹的角度、速度、初始位置检查子弹是否能及时到达目标位置t
公式是:
其中:是初始发射速度(这是)是重力常数是发射角度是子弹的初始 x 速度是子弹的初始 y 速度
u
bulletS
g
θ
Ux
Uy
之后计算子弹在时间的位置t
。
其中:
x
是子弹在时间的 x 位置是子弹在时间t
y
的 y 位置t
Vx
是沿 x 轴的速度(您需要计算这个)
Vxo
是沿 x 轴的初始速度(the Ux
)
Vy
是沿 y 的速度-axis(你需要计算这个)
Vyo
是沿 y 轴的初始速度(the 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;
}
我希望你能理解我的解释(我花了很多时间做它。哈哈)但是如果你有任何问题/澄清,请随时发表评论。
推荐阅读
- spring - Spring Rest Controller 继承
- android - 如何使用 Retrofit 删除 JSON 请求中的“nameValuePairs”?
- ag-grid - 是否有高级 Ag-Grid 事件来监听列状态的任何变化?
- node.js - systemd 服务处于非活动状态(已死)
- apache - htaccess 中预渲染的替代程序(不使用 prerender.io)。推荐完整的自定义代码
- arrays - 从数组中导出特定输出
- python - 导入Pycharm时找不到Project Interpreter中的模块
- raspberry-pi - 如何将 HC-05 连接到 MSP430
- java - 在 EditText 中停止自动滚动
- javascript - 如何使用 React.js 创建过滤器下拉列表?