Simplest way to determine if two 3D boxes intersect?

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.


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.


For arbitrary boxes, you'll want to do the easy cases first.

For each box, calculate the location of its centroid (you can do that by averaging the coordinates for the eight corners), say $\vec{c}$ and $\vec{C}$. Also calculate the distance from the centroid to the nearest and furthest corners, say $r_{min}$ and $r_{max}$ for the box with centroid at $\vec{c}$, and $R_{min}$ and $R_{max}$ for the box with centroid at $\vec{C}$.

If $\left(\vec{c} - \vec{C}\right)\cdot\left(\vec{c} - \vec{C}\right) \lt (r_{min} + R_{min})^2$, the boxes must intersect, because their inscribed spheres intersect.

If $\left(\vec{c} - \vec{C}\right)\cdot\left(\vec{c} - \vec{C}\right) \gt (r_{max} + R_{max})^2$, the boxes cannot intersect, because their circumscribed spheres do not intersect.

Otherwise, you need to test whether one box contains the other, or if the two boxes intersect.

There are many ways to implement a box-box collision test, but unfortunately I have no idea which one of them is "simplest". It probably depends on many factors, like whether the boxes tend to rotate or stay in the same orientation, whether their relative sizes change, and so on. Thus far I haven't needed an intersection test between non-axis-aligned boxes, because spheres are so much easier and faster.

The fact that OP is looking for the simplest box-box intersection test indicates an X-Y problem to me. Non-axis-aligned boxes are a poor approximation for objects, because they are so computationally expensive compared to spheres (or clusters of spheres). The other use cases I can think of are something like simple physics engines (boxes colliding), where you actually want to find the collision point, line, or plane, and not just test whether there is a collision or not.