1
\$\begingroup\$

I have a BoundingBox object which holds two Vector3's (x, y, z), one for the minimum point and one for the maximum point. (-1.5 0 9 | 1.5 3 10)

What is the best way to check for any intersection or overlap of two BoundingBoxes?

The test for intersection will happen more often than not when there are no points actually intersecting, just faces.

The code that I currently am using is: bb1.max_.x_ > bb2.min_.x_ && bb1.min_.x_ < bb2.max_.x_ && bb1.max_.y_ > bb2.min_.y_ && bb1.min_.y_ < bb2.max_.y_ && bb1.max_.z_ > bb2.min_.z_ && bb1.min_.z_ < bb2.max_.z_;

However this leads to problems when points are, for example -1.5 0 9 | 1.5 3 10 intersecting with -0.5 0 8.5 | 0.5 1.8 10.5... The Z minimum and maximum points for the second object laying entirely outside of the Z min and max for the first object.

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered adapting one of the 2D answers here to 3D? \$\endgroup\$
    – DMGregory
    Jan 17, 2016 at 19:53
  • 1
    \$\begingroup\$ Do you have Axis-Aligned Bounding Boxes (AABB) ? \$\endgroup\$
    – Kromster
    Jan 17, 2016 at 19:58
  • \$\begingroup\$ The image seems a bit confusing. Your boxes are axis-aligned as you need directionality to have orientated bounding boxes, but the second image seems to have one box slanted. Please clarify the text and/or image. \$\endgroup\$ Jan 17, 2016 at 21:25

1 Answer 1

2
\$\begingroup\$

The idea is practically the same for 1D, 2D or 3D or any other dimension. First let's talk about a 1D scenario:

We have two segments along x axis, and we know their start and their ends. let them be s1,e1,s2,e2. So what's the requirement for these two lines to overlap? well, we can say the first line should start before the second one ends, and vise versa. Meaning we have to check: s1 < e2 && s2 < e1. That's all the things we need to check.

Now to solve the problem in higher dimensions we are going to use the fact that a bounding box (a rectangle) is in fact axis aligned. Using this fact we can safely assume if two boxes are going to collied, their projections to axis lines should also overlap. With that in mind, no matter how many dimensions you have, you just have to check above statement (s1 < e2 && s2 < e1) for each and every dimension. Two boxes collied if and only if all these conditions are true.

\$\endgroup\$
4
  • \$\begingroup\$ The code that I currently am using is: bb1.max_.x_ > bb2.min_.x_ && bb1.min_.x_ < bb2.max_.x_ && bb1.max_.y_ > bb2.min_.y_ && bb1.min_.y_ < bb2.max_.y_ && bb1.max_.z_ > bb2.min_.z_ && bb1.min_.z_ < bb2.max_.z_; However this leads to problems when points are, for example -1.5 0 9 | 1.5 3 10 intersecting with -0.5 0 8.5 | 0.5 1.8 10.5... The Z minimum and maximum points for the second object laying entirely outside of the Z min and max for the first object. \$\endgroup\$
    – Deveiß
    Jan 17, 2016 at 20:33
  • \$\begingroup\$ I don't get the problem? 9 < 10.5 and 8.5 < 10... which means these two boxes are overlapping along z axis \$\endgroup\$
    – Ali1S232
    Jan 17, 2016 at 21:01
  • \$\begingroup\$ after adding a lot of logging, I've realized that the engine is actually incorrectly handling floating point numbers, as seen here: pastebin.com/yXmhYmu8. The actual code appears to be working as intended. \$\endgroup\$
    – Deveiß
    Jan 17, 2016 at 21:37
  • \$\begingroup\$ @Deveiß That statement makes no sense. Find the actual reason why your values are not the ones you expect. \$\endgroup\$ Jan 18, 2016 at 1:59

You must log in to answer this question.

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