# Bounding Boxes and XNA

So heres some straight up code for you:

BoundingBox b  = new BoundingBox(
Vector3.Up   + Vector3.Right    + Vector3.Forward,
Vector3.Down + Vector3.Backward + Vector3.Left);
BoundingBox bb = new BoundingBox(
Vector3.Up/2   + Vector3.Right/2    + Vector3.Forward/2,
Vector3.Down/2 + Vector3.Backward/2 + Vector3.Left/2);

Debug.GraphicsManager.DrawLine(b, Color.Purple);
Debug.GraphicsManager.DrawLine(bb, Color.Yellow);

if (bb.Contains(b) == ContainmentType.Disjoint)
Debug.DisplayVariable("Disjoint", "");
if (bb.Contains(b) == ContainmentType.Contains)
Debug.DisplayVariable("Contains", "");
if (bb.Contains(b) == ContainmentType.Intersects)
Debug.DisplayVariable("Intersects", "");


What this does is create two bounding boxes, that /obviously/ intersect. DrawLine is a function that accepts a bounding box and draws it. Math and visuals tells us that these intersect and contain.

But all I get is disjoint.

So I started a new blank fresh clean project and only added this to the update code:

BoundingBox b = new BoundingBox(
Vector3.Up + Vector3.Right + Vector3.Forward,
Vector3.Backward + Vector3.Left);
BoundingBox bb = new BoundingBox(
Vector3.Up / 2 + Vector3.Right / 2 + Vector3.Forward / 2,
Vector3.Backward / 2 + Vector3.Left / 2);

if (bb.Contains(b) == ContainmentType.Disjoint)
System.Console.WriteLine("Disjoint");
if (bb.Contains(b) == ContainmentType.Contains)
System.Console.WriteLine("Contains");
if (bb.Contains(b) == ContainmentType.Intersects)
System.Console.WriteLine("Intersects");

if (bb.Intersects(b))
System.Console.WriteLine("intersex");


and this has the same problem.

So I'm stumped. Does XNA just not have good bounding box collison code? Or am I doing something wrong? should I write my own collison code?

Thanks for reading, no this is not debug for me, I simply don't understand.

edit: I also have mouse picking set up and it can identify and pick all my "models" based on Ray.Intersects(model.boundingbox) no problem.

• Is it possible your min/max arguments in the constructor are backwards? – Jimmy Apr 24 '13 at 2:47
• I just tried flipping them with no success. I'm also not sure if that matters. In a fresh document, I believe what I'm doing should work =/ – Theodore Enderby Apr 24 '13 at 2:50
• Do you get any different output from b.Contains(bb)? Surely that ought to return ContainmentType.Contains. – Seth Battin Apr 24 '13 at 2:55
• Logic says yes, but running it says no. – Theodore Enderby Apr 24 '13 at 2:58
• If anyone has XNA and can run that second code and get a working result I'd really like to know. – Theodore Enderby Apr 24 '13 at 2:58

The problem is is the order of your constructors. This works correctly. I put all the negative static Vector3 values into the min, and the positives into the max.

BoundingBox b = new BoundingBox(
Vector3.Left + Vector3.Forward,
Vector3.Backward + Vector3.Up + Vector3.Right);
BoundingBox bb = new BoundingBox(
Vector3.Left / 2 + Vector3.Forward / 2,
Vector3.Backward / 2 + Vector3.Up / 2 + Vector3.Right / 2);

• Wow hey thanks. I assumed stuff like that didn't matter, since I was getting the right dimensioned bounding box. I'm going to read more into the bounding box logic. I appreciate your time :)! – Theodore Enderby Apr 24 '13 at 3:19
• I checked out the BoundingBox docs myself, I couldn't see you using them wrongly, so I put them into a project like you asked. I was a little surprised that the constructors couldn't accommodate odd inputs, but the API can't do everything, I guess. – Seth Battin Apr 24 '13 at 3:21
• Credit to @Jimmy, of course. – Seth Battin Apr 24 '13 at 3:25
• @Theodore Enderby, you can just put in any random Vector3's when creating a boundingbox, you just have to use BoundingBox.CreateFromPoints(IEnumerable<Vector3>) :) then you can add them in any order. Like BoundingBox.CreateFromPoints( new Vector3[] { Vector3.Up + Vector3.Right + Vector3.Forward, Vector3.Backward + Vector3.Left }); – Deukalion Apr 24 '13 at 9:58
• @Seth&&@Jimmy, back in the game program, it was my mins and maxes were flipped :p @Deukalion, wow, I can't believe I overlooked that method. Thanks for the enlightenment! – Theodore Enderby Apr 24 '13 at 15:29