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)
if (bb.Contains(b) == ContainmentType.Contains)
if (bb.Contains(b) == ContainmentType.Intersects)

if (bb.Intersects(b))

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.

  • 1
    \$\begingroup\$ Is it possible your min/max arguments in the constructor are backwards? \$\endgroup\$ – Jimmy Apr 24 '13 at 2:47
  • \$\begingroup\$ 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 =/ \$\endgroup\$ – Theodore Enderby Apr 24 '13 at 2:50
  • \$\begingroup\$ Do you get any different output from b.Contains(bb)? Surely that ought to return ContainmentType.Contains. \$\endgroup\$ – Seth Battin Apr 24 '13 at 2:55
  • \$\begingroup\$ Logic says yes, but running it says no. \$\endgroup\$ – Theodore Enderby Apr 24 '13 at 2:58
  • \$\begingroup\$ If anyone has XNA and can run that second code and get a working result I'd really like to know. \$\endgroup\$ – 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);
  • \$\begingroup\$ 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 :)! \$\endgroup\$ – Theodore Enderby Apr 24 '13 at 3:19
  • \$\begingroup\$ 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. \$\endgroup\$ – Seth Battin Apr 24 '13 at 3:21
  • \$\begingroup\$ Credit to @Jimmy, of course. \$\endgroup\$ – Seth Battin Apr 24 '13 at 3:25
  • \$\begingroup\$ @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 }); \$\endgroup\$ – Deukalion Apr 24 '13 at 9:58
  • \$\begingroup\$ @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! \$\endgroup\$ – Theodore Enderby Apr 24 '13 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.