|
|
还是在 OgreMath.cpp 里
它判断一个球和一个AABB相交的依据是, 如果 球 不仅仅完全处于 AABB 盒 前/后/左/右/上/下 划分的 6个外部子空间其中一个之内, 则 球 一定 和 AABB 相交.
反过来说就是, 只要 球 同时跨越 此6个子空间中任何2个或2个以上,则 球 必定 和 AABB 相交.
代码如下:
//-----------------------------------------------------------------------
bool Math::intersects(const Sphere& sphere, const AxisAlignedBox& box)
{
if (box.isNull()) return false;
if (box.isInfinite()) return true;
// Use splitting planes
const Vector3& center = sphere.getCenter();
Real radius = sphere.getRadius();
const Vector3& min = box.getMinimum();
const Vector3& max = box.getMaximum();
// just test facing planes, early fail if sphere is totally outside
if (min.x - center.x > radius)
{
return false;
}
if (center.x - max.x > radius)
{
return false;
}
if (min.y - center.y > radius)
{
return false;
}
if (center.y - max.y > radius)
{
return false;
}
if (min.z - center.z > radius)
{
return false;
}
if (center.z - max.z > radius)
{
return false;
}
// Must intersect
return true;
}
这个想法是错误的, 至少是不精确的.
想像一下: 当球无限逼近 AABB 任何一条边(但还没有相切)时, 这个时候用上面这个函数判断 球 是和 AABB 相交了.而实际上, 球 并没有和 AABB 相交.
因为 球体 比较接近 方体(长方体/立方体), 所以因为误差还不那么容易被人察觉.
考虑一个线段或任何不规则物体用这种方法判断是否和 AABB 相交,就可以发现误差很明显了.
一般来说,当物体跟 方体 越接近,则这种方法误差越小, 反之 则 越大.
但是, 对于一个成熟的游戏引擎,即使正球体用这种方式判断是否和球相交, 所产生的误差可能也是不能容忍的吧?
也就是说:这种方法 只 适合 AABB 盒之间的相交测试.因为只有 AABB本身 才是方体.
不想多说了,OVER. [em11] |
|