0

我需要实现一点人工智能来弄清楚如何用弹丸运动击中目标。

我在维基百科上找到了这个:

击中坐标所需的角度

这看起来正是我需要的东西,特别是因为我在从零高度以上发射弹丸时遇到了额外的问题。然而,我的数学技能不是很好,所以这对我来说完全是一派胡言,我不知道如何将其中的任何一个翻译成代码。

如果有人能用基本运算符(+ - * %)和函数(sin、cos、sqrt 等)将其分解成我能理解的东西,我将不胜感激。

4

2 回答 2

7

如果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 语句是必要的,因为公式可以没有解决方案(如果目标无法以初始速度到达)、一个解决方案 (then angle1 == angle2) 或两个解决方案(如动画所示;这也是您有公式中的 +/- 符号)。

在大多数编程语言中,您还会发现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
于 2009-12-29T01:27:43.177 回答
2

公式很简单,不用担心推导。

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)

该链接上的公式将为您提供发射弹丸以击中坐标(x,y)上的目标所需的角度

于 2009-12-29T00:42:23.483 回答