首页 > 解决方案 > 如何计算发射子弹以击中移动目标的角度?

问题描述

我发现了一个有趣的项目,它需要计算发射子弹以击中移动物体/目标的角度。此函数中应提供五个参数。以下是参数列表:

`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;
    }

}

一旦我得到距离,我应该使用子弹的速度来获得一个角度。我试图了解这个算法应该如何工作。我想应该先计算目标和子弹之间的距离,然后检查子弹是否到达目标。如果有人有示例或提示如何实现这一点,请告诉我。谢谢你。

标签: javascriptjava

解决方案


这个问题很复杂,但我会尝试做一个描述性的答案。

我们需要建立一些常数,比如重力(现在只有重力):

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是所用时间

只需从t1 开始,然后递增。(使用初始值和增量来获得所需的输出)

=========== 第 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 速度
在此处输入图像描述

ubulletS
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等于xPTyPB等于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;
}

我希望你能理解我的解释(我花了很多时间做它。哈哈)但是如果你有任何问题/澄清,请随时发表评论。


推荐阅读