如果两个矩形相互重叠,您应该能够修改确定吗?很容易达到你的目的.
假设你有CubeA和CubeB.6个条件中的任何一个都保证不存在重叠:
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
CubeB.Y2 < CubeA.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
Run Code Online (Sandbox Code Playgroud)
所以没有重叠的条件是:
Cond1 or Cond2 or Cond3 or Cond4 or Cond5 or Cond6
Run Code Online (Sandbox Code Playgroud)
因此,重叠的充分条件恰恰相反(De Morgan)
Not Cond1 AND Not Cond2 And Not Cond3 And Not Cond4 And Not Cond5 And Not Cond6
Run Code Online (Sandbox Code Playgroud)
接受的答案是错误的,非常令人困惑。这是我想出的。
确定 x 平面中的重叠
if (cubeA.maxX > cubeB.minX)
if (cubeA.minX < cubeB.maxX)
Run Code Online (Sandbox Code Playgroud)
确定 y 平面中的重叠
if (cubeA.maxY > cubeB.minY)
if (cubeA.minY < cubeB.maxY)
Run Code Online (Sandbox Code Playgroud)
确定 z 平面中的重叠
if (cubeA.maxZ > cubeB.minZ)
if (cubeA.minZ < cubeB.maxZ)
Run Code Online (Sandbox Code Playgroud)
如果您将所有这些条件 AND 在一起并且结果为真,您就知道立方体在某个点相交。
信用:https : //silentmatt.com/rectangle-intersection/