简体   繁体   English

如何计算射击子弹以击中移动目标的角度?

[英]How to calculate the angle to shoot a bullet in order to hit a moving target?

I found one interesting project on that is requiring to calculate the angle to shoot a bullet in order to hit a moving object/target. 我发现了一个有趣的项目就是需要计算射击子弹的角度以击中移动的物体/目标。 There are five parameters that should be provided in this function. 此功能应提供五个参数。 Here is the list of the arguments: 以下是参数列表:

`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

For example if I provide set of parameters like this: 例如,如果我提供这样的参数集:

xP yP xV yV bulletS
5  5  0  1    3
4  4  1  0    2
5  5  0  -1   3

So far I was able to calculate the distance but I'm not sure if that is correct. 到目前为止,我能够计算距离,但我不确定这是否正确。 Here is example: 这是一个例子:

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

}

Once I get the distance I should use speed of the bullet to get an angle. 一旦我到达距离,我应该使用子弹的速度来获得一个角度。 I'm trying to understand how this algorithm should work. 我试图了解这个算法应该如何工作。 I guess the distance between the target and the bullet should be calculated first then check if bullet reached the target. 我想应首先计算目标与子弹之间的距离,然后检查子弹是否到达目标。 If anyone have an example or tips how this should be achieved please let me know. 如果有人有一个例子或提示如何实现这一点,请告诉我。 Thank you. 谢谢。

This problem is complicated but I will try to make a descriptive answer. 这个问题很复杂,但我会尝试做一个描述性的答案。

We need to establish some constants, like gravity (for now only gravity): 我们需要建立一些常数,比如引力(现在只有引力):

double gravity = 0.98;
// Add more constants if needed

After establishing the need constants we will do the calculations. 在确定需求常数后,我们将进行计算。

=========== PART 1 =========== ===========第1部分===========

First you need to know where your target is moving by using the Projectile Motion Formula. 首先,您需要使用投射运动公式知道目标的移动位置。

Here are the need variables for the target: 以下是目标的需求变量:

`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

After that calculate the position of the target at time t . 之后计算目标在时间t的位置。

在此输入图像描述
在此输入图像描述

Where: 哪里:
Vx is the velocity along x-axis (You need to calculate this) Vx是沿x轴的速度(你需要计算这个)
Vxo is the initial velocity along x-axis (the xVT ) Vxo是沿x轴的初始速度( xVT
Vy is the velocity along y-axis (You need to calculate this) Vy是沿y轴的速度(你需要计算这个)
Vyo is the initial velocity along y-axis (the yVT ) Vyo是沿y轴的初始速度( yVT
g is the acceleration due to gravity g是重力加速度
t is the time taken t是时间

Just start t at 1 then increment it. 刚开始t 1然后增加它。 (Play with the initial value and increment to get the desired output) (使用初始值和增量来播放以获得所需的输出)

=========== PART 2 =========== ===========第2部分===========

After calculating the location of the target at time t , you then calculate the possible launch angle of the bullet given the position and speed if it can reach the position of the target at time t , if it can reach then the angle will be the answer, if not increment t 在时间t计算目标的位置后,如果能够在时间t到达目标的位置,则计算给定位置和速度的子弹的可能发射角度,如果它可以达到那么角度将是答案,如果不增加t

The needed variables for the bullet are: 子弹所需的变量是:

`xPB` - last known x position of the bullet
`yPB` - last known y position of the bullet
`bulletS` - the speed of the bullet

The formula to calculate the angle is: 计算角度的公式是:
在此输入图像描述

Where : 地点:
v is initial launch speed (this is bulletS ) v是初始启动速度(这是bulletS
g is the gravity constant g是重力常数
x is the x position of the target at time t (this is xPT ) x是目标在时间t的x位置(这是xPT
y is the y position of the target at time t (this is yPT ) y是目标在时间t的y位置(这是yPT

=========== PART 3 =========== ===========第3部分===========
Using the angle, speed, initial position of the bullet check if the bullet can reach the target's position at time t 使用子弹的角度,速度,初始位置检查子弹是否能够在时间t到达目标位置

The formula is: 公式是:
在此输入图像描述
Where : 地点:
u is initial launch speed (this is bulletS ) u是初始发射速度(这是bulletS
g is the gravity constant g是重力常数
θ is the launch angle θ是发射角度
Ux is initial x velocity of the bullet Ux是子弹的初始x速度
Uy is initial y velocity of the bullet Uy是子弹的最初速度

After that calculate the position of the bullet at time t . 之后计算子弹在时间t的位置。

在此输入图像描述
在此输入图像描述
Where: 哪里:
x is the x position of the bullet at time t x是子弹在时间t的x位置
y is the y position of the bullet at time t y是子弹在时间t的y位置
Vx is the velocity along x-axis (You need to calculate this) Vx是沿x轴的速度(你需要计算这个)
Vxo is the initial velocity along x-axis (the Ux ) Vxo是沿x轴的初始速度( Ux
Vy is the velocity along y-axis (You need to calculate this) Vy是沿y轴的速度(你需要计算这个)
Vyo is the initial velocity along y-axis (the Uy ) Vyo是沿y轴的初始速度( Uy
g is the acceleration due to gravity g是重力加速度
t is the time taken t是时间
xPB - last known x position of the bullet xPB - 子弹的最后已知x位置
yPB - last known y position of the bullet yPB - 子弹的最后已知y位置

=========== PART 4 =========== ===========第4部分===========
Now you have the need variables, which are: 现在你有了需要的变量,它们是:

`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

Compare the above variables, if xPB is equal to xPT and yPB is equal to yPT then the bullet will hit the target at time t and at launch angle θ . 比较上述变量,如果xPB等于xPTyPB等于yPT那么子弹将在时间t和发射角θ处击中目标。 If not then increment time t and do Part 1 until Part 4 . 如果没有那么增加时间t并执行第1 部分直到第4部分

=========== SUMMARY =========== ===========摘要===========
This will be the flow of your program. 这将是您的计划的流程。

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

I hope you understand my explanation (I've spent a lot of time making it. Haha) But if you have any questions/clarifications, feel free to comment it. 我希望你理解我的解释(我花了很多时间来做它。哈哈)但如果你有任何问题/澄清,请随意评论。

Assuming the bullet will be fired from origin (0,0). 假设子弹将从原点(0,0)发射。

If the bullet meets the target after time t , then equation would be: 如果子弹在时间t之后达到目标,那么等式将是:

(xP + xV * t, yP + yV * t) = ((bullets * t) * cos(angle), (bullets * t) * sin(angle))

Now, If you solve it, you will get 现在,如果你解决它,你就会得到

xP = (bullets * cos(angle) - xV) * t /* equation 1 */
yP = (bullets * sin(angle) - yV) * t /* equation 2 */

Dividing equation 1 with equation 2 you get: 将等式1除以等式2得到:

xP * sin(angle) - yP * sin(angle) = (xP * yV - xV * yP) / bullets

Now, if you assume m = sin(angle) , then cos(angle) = sqrt(1 - m * m) 现在,如果假设m = sin(angle) ,则cos(angle) = sqrt(1 - m * m)

So now, you have to solve the equation: 所以现在,你必须解决这个等式:

xP * m - yP * sqrt(1 - m * m) = (xP * yV - xV * yP) / bullets

Move the term with square root on one side and the rest on other, so that you get a quadratic equation after squaring and you can solve that equation to get 2 valuee in terms of m = sin(angle) 在一侧移动平方根,而在另一侧移动其余部分,以便在平方后获得二次方程式,并且可以求解该方程式以m = sin(angle)得到2值

So, the final angle is angle = arcsin(m) 所以,最终角度是angle = arcsin(m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM