使用以下函数来检查AABB和扫描胶囊体之间的相交:
bool TestAABBSweptSphereIntersection(const Vector3& aabbMin, const Vector3& aabbMax, const Vector3& capsuleStart, const Vector3& capsuleEnd, float capsuleRadius)
{
// 计算胶囊体的包围盒
Vector3 capsuleMin = Vector3::Min(capsuleStart, capsuleEnd) - Vector3::One * capsuleRadius;
Vector3 capsuleMax = Vector3::Max(capsuleStart, capsuleEnd) + Vector3::One * capsuleRadius;
// 检查AABB是否和胶囊体的包围盒相交
if (!TestAABBAABBIntersection(aabbMin, aabbMax, capsuleMin, capsuleMax))
return false;
// 在胶囊体路径上检查AABB是否和胶囊体相交
float capsuleLength = (capsuleEnd - capsuleStart).Length();
Vector3 capsuleDir = (capsuleEnd - capsuleStart).Normalized();
Vector3 capsuleRight = Vector3::Cross(Vector3::Up, capsuleDir).Normalized();
Vector3 capsuleUp = Vector3::Cross(capsuleRight, capsuleDir).Normalized();
Vector3 capsuleCenter;
for (float t = 0; t <= capsuleLength; t += capsuleRadius)
{
capsuleCenter = capsuleStart + (capsuleDir * t);
Vector3 capRectMin = capsuleCenter - capsuleRight * capsuleRadius - capsuleUp * capsuleRadius;
Vector3 capRectMax = capsuleCenter + capsuleRight * capsuleRadius + capsuleUp * capsuleRadius;
if (TestAABBAABBIntersection(aabbMin, aabbMax, capRectMin, capRectMax))
return true;
}
return false;
}
其中,TestAABBAABBIntersection()
是用来检查AABB与AABB之间的相交。检查AABB与胶囊体之间的相交是通过检查胶囊体路径上多个包围盒与AABB之间的相交实现的。
上一篇:AABB分离轴定理,寻找重叠深度
下一篇:AABB向中心移动的碰撞响应