【问题标题】:Need help deciphering a formula for Projectile Motion需要帮助破译射弹运动的公式
【发布时间】:2009-12-29 00:28:00
【问题描述】:

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

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

Angle required to hit coordinate

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

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

【问题讨论】:

    标签: artificial-intelligence physics game-physics


    【解决方案1】:

    如果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),atanarcus tangent 函数,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
    

    【讨论】:

    • 我认为你遗漏了你的 atans。另外,请注意 x = 0(目标在您的正上方?)...除以零。
    【解决方案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)
    

    formula on that link 将为您提供发射弹丸以击中坐标 (x,y) 上的目标所需的角度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多