边界框不碰撞 (DirectX)

时间:2021-01-09 01:10:13

标签: c++ directx collision-detection collision directx-11

我已经尝试解决这个问题有一段时间了,所以我会尽量保持简单。基本上,当试图让两个 3D 模型发生碰撞时,它们的边界框之间永远不会检测到碰撞(它们相互穿过)。 模型加载良好并出现在场景中,我还可以更改它们的属性,如缩放、旋转和平移,这很好,所以我认为问题不在于模型本身。 每次我在 UpdateScene() 函数中绘制模型时(这会更新每一帧),我也会计算它们的边界框,如下所示:

testModel->Draw();
CalculateAABB(testModel->boxVertices, test, testBoundingBoxMinVertex, testBoundingBoxMaxVertex);

CalculateAABB 函数如下所示:

void CalculateAABB(vector<XMFLOAT3> boundingBoxVerts, XMMATRIX& worldSpace, XMVECTOR& boundingBoxMin, XMVECTOR& boundingBoxMax)
{
    XMFLOAT3 minVertex = XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX);
    XMFLOAT3 maxVertex = XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX);

    //Loop through the 8 vertices describing the bounding box
    for (UINT i = 0; i < 8; i++)
    {
        //Transform the bounding boxes vertices to the objects world space
        XMVECTOR Vert = XMVectorSet(boundingBoxVerts[i].x, boundingBoxVerts[i].y, boundingBoxVerts[i].z, 0.0f);
        Vert = XMVector3TransformCoord(Vert, worldSpace);

        //Get the smallest vertex 
        minVertex.x = min(minVertex.x, XMVectorGetX(Vert));    // Find smallest x value in model
        minVertex.y = min(minVertex.y, XMVectorGetY(Vert));    // Find smallest y value in model
        minVertex.z = min(minVertex.z, XMVectorGetZ(Vert));    // Find smallest z value in model

        //Get the largest vertex 
        maxVertex.x = max(maxVertex.x, XMVectorGetX(Vert));    // Find largest x value in model
        maxVertex.y = max(maxVertex.y, XMVectorGetY(Vert));    // Find largest y value in model
        maxVertex.z = max(maxVertex.z, XMVectorGetZ(Vert));    // Find largest z value in model
    }

    //Store Bounding Box's min and max vertices
    boundingBoxMin = XMVectorSet(minVertex.x, minVertex.y, minVertex.z, 0.0f);
    boundingBoxMax = XMVectorSet(maxVertex.x, maxVertex.y, maxVertex.z, 0.0f);
}

当我移动相机时,我会调用几个函数来检查是否有碰撞,例如:

bool checkTestCollision()
{
    if (BoundingBoxCollision(cameraBoundingBoxMinVertex, cameraBoundingBoxMaxVertex, testBoundingBoxMinVertex, testBoundingBoxMaxVertex))
    {
        return true;
    }

    else {
        return false;
    }
}

这就是 BoundingBoxCollision() 函数中发生的事情:

bool BoundingBoxCollision(XMVECTOR& firstObjBoundingBoxMinVertex, XMVECTOR& firstObjBoundingBoxMaxVertex, XMVECTOR& secondObjBoundingBoxMinVertex, XMVECTOR& secondObjBoundingBoxMaxVertex)
{
    //Is obj1's max X greater than obj2's min X? If not, obj1 is to the LEFT of obj2
    if (XMVectorGetX(firstObjBoundingBoxMaxVertex) > XMVectorGetX(secondObjBoundingBoxMinVertex))

        //Is obj1's min X less than obj2's max X? If not, obj1 is to the RIGHT of obj2
        if (XMVectorGetX(firstObjBoundingBoxMinVertex) < XMVectorGetX(secondObjBoundingBoxMaxVertex))

            //Is obj1's max Y greater than obj2's min Y? If not, obj1 is UNDER obj2
            if (XMVectorGetY(firstObjBoundingBoxMaxVertex) > XMVectorGetY(secondObjBoundingBoxMinVertex))

                //Is obj1's min Y less than obj2's max Y? If not, obj1 is ABOVE obj2
                if (XMVectorGetY(firstObjBoundingBoxMinVertex) < XMVectorGetY(secondObjBoundingBoxMaxVertex))

                    //Is obj1's max Z greater than obj2's min Z? If not, obj1 is IN FRONT OF obj2
                    if (XMVectorGetZ(firstObjBoundingBoxMaxVertex) > XMVectorGetZ(secondObjBoundingBoxMinVertex))

                        //Is obj1's min Z less than obj2's max Z? If not, obj1 is BEHIND obj2
                        if (XMVectorGetZ(firstObjBoundingBoxMinVertex) < XMVectorGetZ(secondObjBoundingBoxMaxVertex))

                            //If we've made it this far, then the two bounding boxes are colliding
                            return true;

    //If the two bounding boxes are not colliding, then return false
    return false;
}

在调试时,我发现它确实在“碰撞”时出现了一些 if,但总是在最后一个失败,这意味着我对模型的 BoundingBoxMin 和 BoundingBoxMax 顶点做错了,但我'我不知道为什么。

以下是“未碰撞”的模型:

enter image description here

感谢您的阅读,如果您能以某种方式提供帮助,我将不胜感激。

0 个答案:

没有答案
相关问题