【发布时间】:2012-03-23 00:42:55
【问题描述】:
所以,我正在尝试开发 3d 乒乓球游戏,但在将球与桌子碰撞时遇到了问题。我有一个球的边界球和桌子的边界框,但交点不是那么准确,我猜测边界框是不正确的,因为球与球拍的碰撞很好。
我正在尝试显示边界框,以便更容易查看错误所在,但我似乎无法使用我的代码实现我找到的教程,或者只是不知道如何实现。而且,由于我的桌子不动,我创建 AABB 是正确的吗?如果有人可以提供帮助,将不胜感激,或者如果有人可以提出更好的方法将球与盒子(如果有的话)碰撞,谢谢。我已经粘贴了我的边界框检测,以及球体和盒子之间的碰撞。如果需要更多代码,我会发布它。谢谢
private bool Ball_Table(Model model1, Matrix world1)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);
if(table_box.Intersects(sphere1))
return true;
}
return false;
}
.
protected BoundingBox UpdateBoundingBox(Model model, Matrix worldTransform)
{
// Initialize minimum and maximum corners of the bounding box to max and min values
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
// For each mesh of the model
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
// Vertex buffer parameters
int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
int vertexBufferSize = meshPart.NumVertices * vertexStride;
// Get vertex data as float
float[] vertexData = new float[vertexBufferSize / sizeof(float)];
meshPart.VertexBuffer.GetData<float>(vertexData);
// Iterate through vertices (possibly) growing bounding box, all calculations are done in world space
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);
}
}
}
// Create and return bounding box
table_box = new BoundingBox(min, max);
return table_box;
}
【问题讨论】:
标签: 3d xna collision bounding-box