# What is the best way to check for intersection of two bounding boxes, given a minimum and maximum Vector3 for each one

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.

• Have you considered adapting one of the 2D answers here to 3D? – DMGregory Jan 17 '16 at 19:53
• Do you have Axis-Aligned Bounding Boxes (AABB) ? – Kromster Jan 17 '16 at 19:58
• 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. – Lars Viklund Jan 17 '16 at 21:25

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.
• 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. – Deveiß Jan 17 '16 at 20:33