[英]How to calculate the angle to shoot a bullet in order to hit a moving target?
我發現了一個有趣的項目就是需要計算射擊子彈的角度以擊中移動的物體/目標。 此功能應提供五個參數。 以下是參數列表:
`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
是時間
剛開始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
是目標在時間t
的x位置(這是xPT
)
y
是目標在時間t
的y位置(這是yPT
)
===========第3部分===========
使用子彈的角度,速度,初始位置檢查子彈是否能夠在時間t
到達目標位置
公式是:
地點:
u
是初始發射速度(這是bulletS
)
g
是重力常數
θ
是發射角度
Ux
是子彈的初始x速度
Uy
是子彈的最初速度
之后計算子彈在時間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;
}
我希望你理解我的解釋(我花了很多時間來做它。哈哈)但如果你有任何問題/澄清,請隨意評論。
假設子彈將從原點(0,0)發射。
如果子彈在時間t
之后達到目標,那么等式將是:
(xP + xV * t, yP + yV * t) = ((bullets * t) * cos(angle), (bullets * t) * sin(angle))
現在,如果你解決它,你就會得到
xP = (bullets * cos(angle) - xV) * t /* equation 1 */
yP = (bullets * sin(angle) - yV) * t /* equation 2 */
將等式1除以等式2得到:
xP * sin(angle) - yP * sin(angle) = (xP * yV - xV * yP) / bullets
現在,如果假設m = sin(angle)
,則cos(angle) = sqrt(1 - m * m)
所以現在,你必須解決這個等式:
xP * m - yP * sqrt(1 - m * m) = (xP * yV - xV * yP) / bullets
在一側移動平方根,而在另一側移動其余部分,以便在平方后獲得二次方程式,並且可以求解該方程式以m = sin(angle)
得到2值
所以,最終角度是angle = arcsin(m)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.