我需要实现一点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