Overlapping cubes

You should be able to modify Determine if two rectangles overlap each other? to your purpose fairly easily.

Suppose that you have CubeA and CubeB. Any one of 6 conditions guarantees that no overlap can exist:

Cond1.  If A's left face is to the right of the B's right face,
           -  then A is Totally to right Of B
              CubeA.X2 < CubeB.X1
Cond2.  If A's right face is to the left of the B's left face,
           -  then A is Totally to left Of B
              CubeB.X2 < CubeA.X1
Cond3.  If A's top face is below B's bottom face,
           -  then A is Totally below B
              CubeA.Z2 < CubeB.Z1
Cond4.  If A's bottom face is above B's top face,
           -  then A is Totally above B
              CubeB.Z2 < CubeA.Z1
Cond5.  If A's front face is behind B's back face,
           -  then A is Totally behind B
              CubeA.Y2 < CubeB.Y1
Cond6.  If A's left face is to the left of B's right face,
           -  then A is Totally to the right of B
              CubeB.Y2 < CubeA.Y1

So the condition for no overlap is:

Cond1 or Cond2 or Cond3 or Cond4 or Cond5 or Cond6

Therefore, a sufficient condition for Overlap is the opposite (De Morgan)

Not Cond1 AND Not Cond2 And Not Cond3 And Not Cond4 And Not Cond5 And Not Cond6

Cubes are made up of 6 rectangular (okay, square) faces.

Two cubes do not intersect if the following conditions are met.

  • None of the faces of 2 cubes intersect.
  • One cube does not completely contain the other.

The post you linked can be easily extended. Just add Z.


The accepted answer is wrong and very confusing. Here is what I have come up with.

Determining overlap in the x plane

    if (cubeA.maxX > cubeB.minX)
    if (cubeA.minX < cubeB.maxX)

Determining overlap in the y plane

    if (cubeA.maxY > cubeB.minY)
    if (cubeA.minY < cubeB.maxY)

Determining overlap in the z plane

    if (cubeA.maxZ > cubeB.minZ)
    if (cubeA.minZ < cubeB.maxZ)

if you AND all of these conditions together and the result is true, you know that the cubes intersect at some point.

Credit: https://silentmatt.com/rectangle-intersection/

Tags:

Geometry