如何在XNA 4.0中检查边界框?

3

我想在XNA 4.0中使用BoundingBox类来检测立方体与立方体或立方体与球体之间的碰撞。我知道BoundingSphere,但不知道如何使用BoundingBox。有没有关于此的好样例!谢谢!


寻找关于XNA类的信息的好方法是通过谷歌搜索或访问http://msdn.microsoft.com/en-us/library/bb196942.aspx。 - ZombieSpy
1个回答

4

你可以像这样创建边界框:

Vector3 CenterOfBox = new Vector3(10,10,10);
int Width = 10;
int Height = 10;
BoundingBox BoundingBox1 = new BoundingBox(CenterOfBox - new Vector(Width/2,Height/2,Width/2),CenterOfBox + new Vector(Width/2,Height/2,Width/2));

更多信息: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.boundingbox.aspx

假设你有BoundingBox1和BoundingBox2

然后你可以使用以下方法检查它们是否相交:

if(BoundingBox1.Intersect(BoundingBox2))
{
    //They hit
}
else
{
    //They don't hit
}

你可以在Intersect函数中传递一个BoundingSphere参数。
更多信息:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.intersects.aspx

谢谢你!你知道如何检查一个ModelBone的BoundingSphere吗? - Google New
@Google 新消息:ModelBone 没有边界,它只是一个位置的容器。您应该获取其 ModelMesh 来计算边界球,ModelMesh 具有属性“BoundingSphere”(或者您可以在骨骼位置创建一个球)。 - ZombieSpy
@GoogleNew 你应该看一下 http://msdn.microsoft.com/en-us/library/bb197344.aspx - ZombieSpy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接