0

所以我试图让它正确地与 AABB 相撞。但由于某种原因,它只会发生在 AABB 的一侧。所有其他方面都表现得好像没有碰撞一样。

    float  dmin;
    float  r2 = pow(m_Radius, 2);
    dmin = 0;

    if (m_Center.GetX() < other.GetMinExtents().GetX())
        dmin += pow(m_Center.GetX() - other.GetMinExtents().GetX(), 2);
    else if (m_Center.GetX() > other.GetMaxExtents().GetX())
        dmin += pow(m_Center.GetX() - other.GetMaxExtents().GetX(), 2);

    if (m_Center.GetY() < other.GetMinExtents().GetY())
        dmin += pow(m_Center.GetY() - other.GetMinExtents().GetY(), 2);
    else if (m_Center.GetY() > other.GetMaxExtents().GetY())
        dmin += pow(m_Center.GetY() - other.GetMaxExtents().GetY(), 2);

    if (m_Center.GetZ() < other.GetMinExtents().GetZ())
        dmin += pow(m_Center.GetZ() - other.GetMinExtents().GetZ(), 2);
    else if (m_Center.GetZ() > other.GetMaxExtents().GetZ())
        dmin += pow(m_Center.GetZ() - other.GetMaxExtents().GetZ(), 2);

    if (dmin < r2)
    {
        return true;
    }
4

1 回答 1

0

这只是一种预感......您没有指定要在哪个平台和编译器上进行测试,但是如果 pow() 函数是天真的实现的,它可能会因第一个参数的负值而失败。 根据规范,只要指数是整数,它应该对基数的负值正常工作。但是一个天真的实现的通用 pow(base, exponent) 可能会做一些类似 exp(e, ln(base)*exponent)) 的事情,并且对于 base 的负值会返回 NaN。

这将具有您所描述的效果 - 它仅适用于您的示例代码中所有三个轴都生成正增量的一侧。

在任何情况下,手动进行平方总是更安全,或者实现如下内联: float square(float a) { return a*a; } 以获得更简洁的代码。

于 2018-01-29T12:21:17.620 回答