[Math] Simplest way to determine if two 3D boxes intersect

3d

Given

If given two sets of two points in 3D space (where they are defined to be the corners of the box):

Box1:

P1 = (961.46, 215.15, 1465.44)
P2 = (970.02, 214.93, 1481.77)

Translational Matrix for Box 1:

enter image description here


Box2:

P1 = (1093.52, -499.50, 896.11)
P2 = (1093.12, -505.49, 878.68)

Translational Matrix for Box 2:

enter image description here


What I am looking for

What is the simplest way to determine if the two boxes intersect in space or not? I realize this may not be a simple question but hoping for some guidance at least.

The boxes can of course be tilted in any degree and don't always have lined up axis. I did add my translational matrix I started with to establish the boxes as this contains the two vectors to which the points are oriented to if this is helpful at all.

Best Answer

Assuming that the boxes are axis-aligned (because otherwise they're underspecified), let's say that the corners of the first are $$ P_1 = (x_1, y_1, z_1)\\ Q_1 = (X_1, Y_1, Z_1) $$ with $x_1 < X_1, y_1 < Y_1, z_1 < Z_1$, and for the second $$ P_2 = (x_2, y_2, z_2)\\ Q_2 = (X_2, Y_2, Z_2) $$

Then the way to check for an overlap is this: Compare the intervals $[x_1,X_1]$ and $[x_2, X_2]$, and if they don't overlap, there's no intersection. Do the same for the $y$ intervals, and the $z$ intervals.

If all three interval-pairs DO overlap, then there IS an intersection.

What do I mean by "overlap"? I mean that there's a number $a$ with $x_1 \le a \le X_1$ and $x_2 \le a \le X_2$, for instance.

You can check this easily by checking just endpoints: the intervals overlap if any one of these four conditions is true: $$ x_1 \le x_2 \le X_1 \\ x_1 \le X_2 \le X_1 \\ x_2 \le x_1 \le X_2 \\ x_2 \le X_1 \le X_2 $$

If none of those four is true, then the intervals do not overlap.

For your particular case, $$ P_1 = (961.46, 215.15, 1465.44) \\ Q_1 = (970.02, 214.93, 1481.77) $$ and $$ P_2 = (1093.52, -499.50, 896.11)\\ Q_2 = (1093.12, -505.49, 878.68) $$ we see that the x-intervals don't overlap, and hence the boxes don't overlap. We don't even need to look at the $y$s and $z$s (although it's particularly simple, because the $y$s also don't overlap, and the $z$s also don't overlap! These boxes are as disjoint as possible.

Related Question