You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

固定初速无阻力弹道抛射物命中目标的发射角度计算问题求助

3D炮塔弹道抛射物瞄准角度计算错误排查

问题背景

期望实现炮塔发射固定初速、无空气阻力的弹道抛射物,命中3D平面指定点位,但自研代码运行效果不符合预期:

  • 目标距离较近时炮塔瞄准偏高
  • 目标距离较远时炮塔瞄准偏低
  • 仅在某一特定距离下可命中目标,无法满足使用需求

从误差变化规律判断大概率是乘除运算逻辑出错,但始终无法定位问题根源,恳请指出错误方向。

问题代码(第一版)

float CalculateAngle(float velocity)
{
    float gravity = -Physics.gravity.y;

    Vector3 modPos = target.position;
    if (modPos.x < 0) modPos.x -= 2 * modPos.x;
    if (modPos.y < 0) modPos.y -= 2 * modPos.y;
    if (modPos.z < 0) modPos.z -= 2 * modPos.z;
    modPos.x /= 10;
    modPos.y /= 10;
    modPos.z /= 10;
    
    float deltaX = modPos.x - FirePoint.position.x;
    float deltaZ = modPos.z - FirePoint.position.z;
    float deltaY = modPos.y - FirePoint.position.y;
    float horzDelta = Mathf.Sqrt(deltaX * deltaX + deltaZ * deltaZ);

    float RHSFirstPart = (velocity * velocity) / (gravity * horzDelta);
    float RHSSecondPart = Mathf.Sqrt(((velocity * velocity) * ((velocity * velocity) - (2 * gravity * deltaY))/ (gravity * gravity * horzDelta * horzDelta)) - 1);
    float tanθ = RHSFirstPart - RHSSecondPart;
    float angle = Mathf.Atan2(tanθ, 1) * Mathf.Rad2Deg;
    if (angle < 0) return angle;
    return -angle;
}

实际运行效果

Close Range
Just Right
Long Range

编辑1:第二版代码问题

参照物理原理解答重新编写代码后,运行时报错:待开平方的数值为负,返回NaN,反复核对公式未发现错误,代码如下:

float CalculateAngle3(float velocity)
{
    float deltaX = 500;
    float deltaY = 20;
    float v = 100;
    float vSqr = v * v;
    float g = 9.81f * 9.81f;

    float a = vSqr * (vSqr - 2 * g * deltaY);
    float b = (g * g) * (deltaX * deltaX);
    float c = a / b - 1;
    float d = Mathf.Sqrt(c); //c is negitive causing an NaN
    float e = vSqr / g * deltaX;
    float tanθ = e - d;

    return tanθ;
}

编辑2:临时解决方案

暂时放弃自行实现,改用成熟第三方逻辑实现功能,调用方式如下:

Vector3 s0;
Vector3 s1;
if (fts.solve_ballistic_arc(FirePoint.position, bomb.StartingVelocity.z, target.position, -Physics.gravity.y, out s0, out s1) > 0)
{
    targetPosition = transform.position + s1;
    SafetyEnabled = false;
}
else
{
    //Don't fire if we don't have a solution
    SafetyEnabled = true;
}

目前仍未找到原有实现的出错原因,保留问题等待解答。


内容的提问来源于stack exchange,提问作者CiberX15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:45:05