Google New
Google New

Reputation: 107

How to check bounding box in XNA 4.0?

I want to use class BoundingBox in XNA 4.0 to check collision between cube with cube or cube with sphere? I know about BoundingSphere but I don't know use to BoundingBox. Have any good sample about this! Thanks!

Upvotes: 3

Views: 2269

Answers (1)

ZombieSpy
ZombieSpy

Reputation: 1376

You make boundingboxes like this:

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));

More info: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.boundingbox.aspx

Lets say you have BoundingBox1 and BoundingBox2

Then you can check if they intersect with:

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

You can also pass a BoundingSphere in the Intersect function

More info: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.intersects.aspx

Upvotes: 4

Related Questions