Reputation: 2022
I'm trying to determine if two cubes overlap. I've read up on overlapping rectangles, but I'm not sure how to translate it into the third dimension.
My goal is to generate a number of randomly positioned and sized non-overlapping cubes.
These cubes are represented on a x,y,z Cartesian plane.
Upvotes: 10
Views: 16123
Reputation: 46497
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
Upvotes: 5
Reputation: 716
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/
Upvotes: 15
Reputation: 31
This is just the accepted answer rewritten with the correction. It tests to see if the two axis aligned cuboids have any segment of the X,Y, and Z axis in common, if they dont then it is impossible for them to have a collision. The function assumes there is a collision and performs the tests to check if there isnt.
Function func_Intersect(ByVal cuboid1_MinX As Double, ByVal cuboid1_MaxX As Double, ByVal cuboid1_MinY As Double, ByVal cuboid1_MaxY As Double, ByVal cuboid1_MinZ As Double, ByVal cuboid1_MaxZ As Double, ByVal cuboid2_MinX As Double, ByVal cuboid2_MaxX As Double, ByVal cuboid2_MinY As Double, ByVal cuboid2_MaxY As Double, ByVal cuboid2_MinZ As Double, ByVal cuboid2_MaxZ As Double) As Boolean
func_Intersect = True
If cuboid1_MaxX < cuboid2_MinX Then
func_Intersect = False
ElseIf cuboid2_MaxX < cuboid1_MinX Then
func_Intersect = False
ElseIf cuboid1_MaxY < cuboid2_MinY Then
func_Intersect = False
ElseIf cuboid2_MaxY < cuboid1_MinY Then
func_Intersect = False
ElseIf cuboid1_MaxZ < cuboid2_MinZ Then
func_Intersect = False
ElseIf cuboid2_MaxZ < cuboid1_MinZ Then
func_Intersect = False
End If
End Function
Upvotes: 0
Reputation: 24846
I suppose (did not think much, maybe my condition is not enough) check if all the vertices of first cube are out of the second and inverse: all vertices of second are out of the first.
To check if the vertex is in the cube or not, transform it's coordinates to cube-related coordinate system (apply translation to the cube center and cube rotation). Then simply check each coord (x, y, z) is smaller then half a side
Upvotes: 1
Reputation: 51019
Cubes are made up of 6 rectangular (okay, square) faces.
Two cubes do not intersect if the following conditions are met.
The post you linked can be easily extended. Just add Z.
Upvotes: 3