0
\$\begingroup\$

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.

Diagram showing cases of separated boxes, overlapping boxes, and boxes in touching contact

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Andy
    Commented Apr 15, 2020 at 7:37

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ This is the correct answer - Bounding Boxes are all well and good, but Axis-Aligned Bounding Boxes are the efficient brother \$\endgroup\$
    – Natalo77
    Commented Apr 15, 2020 at 13:05
  • \$\begingroup\$ 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. :/ \$\endgroup\$
    – DMGregory
    Commented Apr 15, 2020 at 13:16
  • \$\begingroup\$ It seems like OP is describing AABBs however, without knowing of their existence, so I think your answer was good \$\endgroup\$
    – Natalo77
    Commented Apr 15, 2020 at 16:44
  • \$\begingroup\$ 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”? \$\endgroup\$ Commented Apr 15, 2020 at 20:15
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Commented Apr 15, 2020 at 20:20

You must log in to answer this question.

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