【UE4 C++】运用AABBTree快速精确检测StaticMesh是否发生相交

bool GeometryProcessingUtils::IsIntersection(UStaticMeshComponent* SMCA, UStaticMeshComponent* SMCB)
{
    // quick AABB intersect test
    if (!SMCA || !SMCB) return false;
    SMCA->UpdateBounds();
    SMCB->UpdateBounds();
    if (!FBoxSphereBounds::BoxesIntersect(SMCA->Bounds, SMCB->Bounds)) return false;
    
    // quick mesh intersect test
    if (!SMCA->GetStaticMesh() || !SMCB->GetStaticMesh()) return false;
    const FTransform WXform[2]{ SMCA->GetComponentTransform(), SMCB->GetComponentTransform() };
    const FMeshDescription* MD[2]{ SMCA->GetStaticMesh()->GetMeshDescription(0), SMCB->GetStaticMesh()->GetMeshDescription(0)};
    if (!MD[0] || !MD[1]) return false;

    FDynamicMesh3 DM[2];
    FMeshDescriptionToDynamicMesh Converter;
    for (int32 Idx = 0; Idx < 2; ++Idx)
    {
        Converter.Convert(MD[Idx], DM[Idx]);

        // to world space
        MeshTransforms::ApplyTransform(DM[Idx], FTransform3d(WXform[Idx]));
        if (WXform[Idx].GetDeterminant() < 0) DM[Idx].ReverseOrientation(false);
    }
    
    FDynamicMeshAABBTree3 Spatial[2]{ &DM[0], &DM[1] };
    return Spatial[0].TestIntersection(Spatial[1]);
}

你可能感兴趣的:(UE4,AABB,相交检测)