我已经使用 SAT 实现了碰撞检测,如果另一个bbox
高于或低于边界框,我会得到误报。
我将边界框的每个(现在)轴对齐面投影到它自己和其他bbox
角。
返回的碰撞数据是正确的(depth, stepheight)
,但它为边界框上方或下方的每个对象返回碰撞的事实为ofc
false。
我根据一个有效的简单aabb
碰撞检查检查我的结果。
以下是代码:
//------------------------------
BoundingBoxIntersectionResult BoundingBox::IntersectionSAT(BoundingBox & other)
{
// check shortest edge
f32 distances[6] = {
(other.mMaxVec.x - this->mMinVec.x),
(this->mMaxVec.x - other.mMinVec.x),
(other.mMaxVec.y - this->mMinVec.y),
(this->mMaxVec.y - other.mMinVec.y),
(other.mMaxVec.z - this->mMinVec.z),
(this->mMaxVec.z - other.mMinVec.z)
};
i32 faceIndex = 0;
Vec3 faceNormal;
f32 collisionDepth = 0.0f;
// for each face normal, get the minimum and maximum extens of the projection
// of all corner points of both shapes
// if they dont overlap, there is no intersection
// check each normal
for (ui32 i = 0; i < 6; i++)
{
// CornerPointsWorld represents the world space corner positions
SATReturn ret = this->SATTest(this->mNormals[i], this->mCornerPointsWorld);
SATReturn ret2 = this->SATTest(this->mNormals[i], other.mCornerPointsWorld);
float d1 = ret.minAlong - ret2.maxAlong;
float d2 = ret2.minAlong - ret.maxAlong;
if ((d1 > 0.0f) || (d2> 0.0f))
{
// return a false collision event, because we got a seperating axis
return { false, 0.0f, 0.0f, Vec3(), BBOX_SIDE_LEFT };
}
}
// check each normal of the other bbox
for (ui32 i = 0; i < 6; i++)
{
SATReturn ret = this->SATTest(other.mNormals[i], this->mCornerPointsWorld);
SATReturn ret2 = this->SATTest(other.mNormals[i], other.mCornerPointsWorld);
float d1 = ret.minAlong - ret2.maxAlong;
float d2 = ret2.minAlong - ret.maxAlong;
if ((d1 > 0.0f) || (d2> 0.0f))
{
// return a false collision event, because we got a seperating axis
return { false, 0.0f, 0.0f, Vec3(), BBOX_SIDE_LEFT };
}
// get collision data
if (i == 0 || distances[i] < collisionDepth)
{
faceIndex = i;
faceNormal = this->mNormals[i];
collisionDepth = distances[i];
}
}
// get step height needed to climb this object
f32 stepHeight = other.mMaxVec.y - this->mMinVec.y;
return { true, collisionDepth, stepHeight, faceNormal, BoundingBoxSide(faceIndex) };
}
//------------------------------
SATReturn BoundingBox::SATTest(Vec3& normal, Vector<Vec3>& corners)
{
SATReturn ret;
ret.maxAlong = MIN_FLOAT;
ret.minAlong = MAX_FLOAT;
// for each point
for (ui32 i = 0; i < corners.GetSize(); i++)
{
f32 dot = Vec3::Dot(corners[i], normal);
if (dot < ret.minAlong) ret.minAlong = dot;
if (dot > ret.maxAlong) ret.maxAlong = dot;
}
return ret;
}
其中面法线定义为:
Vec3 mNormals[6] =
{
Vec3(1, 0, 0), // left
Vec3(-1, 0, 0), // right
Vec3(0, 1, 0), // up
Vec3(0, -1, 0), // down
Vec3(0, 0, 1), //back
Vec3(0, 0, -1), // front
};
我添加了一个屏幕截图来显示问题:
它为边界框下方或上方的所有对象返回误报。