我需要實現一點AI來弄清楚如何用拋射物運動擊中目標。需要幫助破譯拋射物運動的公式
我在維基百科發現這種過度:
Angle required to hit coordinate
它看起來像只是我需要的東西,尤其是因爲我有額外的問題從上面零高度發射炮彈。然而,我的數學技能並不好,所以對我來說這一切看起來都是完全廢話,我不知道如何將它翻譯成代碼。
如果任何人都可以把它分解成我能理解的基本操作符(+ - *%)和函數(sin,cos,sqrt等),我真的很感激它。
我需要實現一點AI來弄清楚如何用拋射物運動擊中目標。需要幫助破譯拋射物運動的公式
我在維基百科發現這種過度:
Angle required to hit coordinate
它看起來像只是我需要的東西,尤其是因爲我有額外的問題從上面零高度發射炮彈。然而,我的數學技能並不好,所以對我來說這一切看起來都是完全廢話,我不知道如何將它翻譯成代碼。
如果任何人都可以把它分解成我能理解的基本操作符(+ - *%)和函數(sin,cos,sqrt等),我真的很感激它。
如果xTarget/yTarget
角度爲目標的位置,xProj/yProj
的射彈和v
彈丸的初始速度(每秒米)的初始位置,則該公式將轉化爲以下僞代碼:
x = xTarget - xProj;
y = yTarget - yProj;
g = 9.8;
tmp = pow(v, 4) - g * (g * pow(x, 2) + 2 * y * pow(v, 2));
if tmp < 0
// no solution
else if x == 0
angle1 = pi/2;
if y < 0
angle2 = -pi/2;
else
angle2 = pi/2;
end
else
angle1 = atan((pow(v, 2) + sqrt(tmp))/(g * x));
angle2 = atan((pow(v, 2) - sqrt(tmp))/(g * x));
end
g
是放大常數(〜9.8 m/s^2),atan
是弧線切線功能和pow
是冪函數。 if語句是必要的,因爲公式可能沒有解決方案(如果目標不能以初始速度到達),一個解決方案(然後angle1 == angle2
)或兩個解決方案(可以在this動畫中看到;這也是爲什麼您在公式中有+/-符號)。
在大多數編程語言中,你還會發現atan2
,在這種情況下,你應該能夠
if tmp < 0
// no solution
else
angle1 = atan2(pow(v, 2) + sqrt(tmp), g * x);
angle2 = atan2(pow(v, 2) - sqrt(tmp), g * x);
end
公式很簡單,不用擔心推導。
x is the horizontal distance away of the target you're trying to hit
y is the vertical distance away of the target you're trying to hit
v is the initial velocity of the launch
g is the acceleration due to gravity (9.81 m/s on earth)
和formula on that link會給你你需要啓動拋射爲了打在座標(x,y)的目標
我認爲你離開了你的atans更換一些代碼。另外,注意x = 0(目標直接在你的上方?)......除以零。 – 2009-12-29 02:13:33
你說得對,謝謝。 – Lucas 2009-12-29 03:28:48