我想在我的 XNA 项目中进行碰撞,我尝试使用 BoundingBoxes。我想与房子的墙壁发生碰撞,所以我使用这段代码为模型中的每个网格制作 BoundingBox:
public void CreateBB()
{
Model model = Game1.ModelRes[nazwaModelu];
Matrix worldTransform = world;
foreach (ModelMesh mesh in model.Meshes)
{
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
int vertexBufferSize = meshPart.NumVertices * vertexStride;
float[] vertexData = new float[vertexBufferSize / sizeof(float)];
meshPart.VertexBuffer.GetData<float>(vertexData);
for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
{
Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), worldTransform);
min = Vector3.Min(min, transformedPosition);
max = Vector3.Max(max, transformedPosition);
}
}
Game1.boxory.Add(new BoundingBox(min, max));
}
}
我收到了 6 个相等的边界框。每个 BoundingBox 都是相同的,因此碰撞仅适用于一面墙。是这段代码的问题吗?(或者可能是导出具有许多网格的模型)?