# Categorize contact & overlap cases for 2D & 3D axis-aligned bounding boxes

My question is similar to this one. I want to determine whether two axis-aligned boxes are not only intersecting, but if they are intersecting and just touching on one edge or face, or if they are overlapping in some way.

Below is an example of these two options.

• I would calculate the 4 corners points and do simple comparison if blue.top.right.x < red.top.right.x and (not to the left of the whole thing. Then check y if it's within x.
– Andy
Commented Apr 15, 2020 at 7:37

For each axis, compute whether the intervals spanned by the two boxes are...

•  -1: overlapped (minA < maxB & maxA > minB)

•   0: adjacent (minA == maxB or minB == maxA)

• +1: separated (minA > maxB or minB > maxA)

Return the greatest result you found on any axis.

(ie. The boxes are "overlapping" only if their intervals overlap on every axis. Being "adjacent" on any axis trumps overlapped, and "separated" trumps all)

The numbers I used to label the cases are arbitrary - I picked -1,0,1 to represent the sign of the distance between the boxes: positive if separated, negative if overlapped, and exactly zero if just in contact.

• This is the correct answer - Bounding Boxes are all well and good, but Axis-Aligned Bounding Boxes are the efficient brother Commented Apr 15, 2020 at 13:05
• Ah, from the diagram I just assumed we were dealing exclusively with AABBs, since no rotated boxes were shown. Maybe I should have asked to confirm. :/ Commented Apr 15, 2020 at 13:16
• It seems like OP is describing AABBs however, without knowing of their existence, so I think your answer was good Commented Apr 15, 2020 at 16:44
• Yes this is assuming AA. However, for the bottom right example, wouldn’t this return 0, aka adjacent, even though it should return overlapped? (Since we are returning “the greatest result on any axis”? Commented Apr 15, 2020 at 20:15
• By my reading, the two intervals overlap fully in the y axis (both boxes occupy exactly the same height range), and overlap partially in the x axis (the blue box intrudes partway into the red), so there is no "adjacent" result to confuse the outcome. They overlap on every axis, so we report they are overlapping. Commented Apr 15, 2020 at 20:20