0
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Is it possible your min/max arguments in the constructor are backwards? \$\endgroup\$
    – Jimmy
    Commented Apr 24, 2013 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\$ Commented Apr 24, 2013 at 2:50
  • \$\begingroup\$ Do you get any different output from b.Contains(bb)? Surely that ought to return ContainmentType.Contains. \$\endgroup\$ Commented Apr 24, 2013 at 2:55
  • \$\begingroup\$ Logic says yes, but running it says no. \$\endgroup\$ Commented Apr 24, 2013 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\$ Commented Apr 24, 2013 at 2:58

1 Answer 1

1
\$\begingroup\$

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);
\$\endgroup\$
5
  • \$\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\$ Commented Apr 24, 2013 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\$ Commented Apr 24, 2013 at 3:21
  • \$\begingroup\$ Credit to @Jimmy, of course. \$\endgroup\$ Commented Apr 24, 2013 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
    Commented Apr 24, 2013 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\$ Commented Apr 24, 2013 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .