问题描述
我已经尝试解决这个问题有一段时间了,所以我会尽量保持简单。基本上,当试图让两个 3D 模型发生碰撞时,它们的边界框之间永远不会检测到碰撞(它们相互穿过)。 模型加载良好并出现在场景中,我还可以更改它们的属性,如缩放、旋转和平移,这很好,所以我认为问题不在于模型本身。 每次我在 UpdateScene() 函数中绘制模型时(这会更新每一帧),我也会计算它们的边界框,如下所示:
testModel->Draw();
CalculateAABB(testModel->BoxVertices,test,testBoundingBoxMinVertex,testBoundingBoxMaxVertex);
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,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 顶点做错了,但我'我不知道为什么。
以下是“未碰撞”的模型:
感谢您的阅读,如果您能以某种方式提供帮助,我将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)