Geometry 重叠立方体
我想确定两个立方体是否重叠。我已经读过了,但我不知道如何把它转换成三维空间 我的目标是生成一些随机定位和大小不重叠的立方体Geometry 重叠立方体,geometry,Geometry,我想确定两个立方体是否重叠。我已经读过了,但我不知道如何把它转换成三维空间 我的目标是生成一些随机定位和大小不重叠的立方体 这些立方体表示在x、y、z笛卡尔平面上。您应该能够相当容易地根据自己的目的进行修改 假设您有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
这些立方体表示在x、y、z笛卡尔平面上。您应该能够相当容易地根据自己的目的进行修改 假设您有
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
因此,重叠的充分条件正好相反(De Morgan)
立方体由6个矩形(好的,正方形)面组成 如果满足以下条件,则两个立方体不相交
- 两个立方体的面都不相交
- 一个立方体并不完全包含另一个立方体
若要检查顶点是否在立方体中,请将其坐标变换为与立方体相关的坐标系(将平移应用于立方体中心和立方体旋转)。然后简单地检查每个坐标(x,y,z)是否小于半个边接受的答案是错误的,并且非常混乱。这是我想到的 确定x平面中的重叠
if (cubeA.maxX > cubeB.minX)
if (cubeA.minX < cubeB.maxX)
if (cubeA.maxY > cubeB.minY)
if (cubeA.minY < cubeB.maxY)
if (cubeA.maxZ > cubeB.minZ)
if (cubeA.minZ < cubeB.maxZ)
if(cubeA.maxX>cubeB.minX)
if(cubeA.minX
确定y平面中的重叠
if (cubeA.maxX > cubeB.minX)
if (cubeA.minX < cubeB.maxX)
if (cubeA.maxY > cubeB.minY)
if (cubeA.minY < cubeB.maxY)
if (cubeA.maxZ > cubeB.minZ)
if (cubeA.minZ < cubeB.maxZ)
if(cubeA.maxY>cubeB.minY)
if(立方最小<立方最大)
确定z平面中的重叠
if (cubeA.maxX > cubeB.minX)
if (cubeA.minX < cubeB.maxX)
if (cubeA.maxY > cubeB.minY)
if (cubeA.minY < cubeB.maxY)
if (cubeA.maxZ > cubeB.minZ)
if (cubeA.minZ < cubeB.maxZ)
if(cubeA.maxZ>cubeB.minZ)
if(cubeA.minZ
如果你把所有这些条件加在一起,结果为真,你就知道立方体在某个点相交
信用证:这只是用更正改写的公认答案。它测试两个轴对齐的长方体是否有X、Y和Z轴的任何共同部分,如果没有,则不可能发生碰撞。 该函数假定存在冲突,并执行测试以检查是否存在冲突
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
函数func\u Intersect(ByVal cuboid1_MinX为双精度,ByVal cuboid1_MinX为双精度,ByVal cuboid1_MinY为双精度,ByVal cuboid1_MinZ为双精度,ByVal cuboid1_MaxZ为双精度,ByVal cuboid2_MinX为双精度,ByVal cuboid2_MinY为双精度,ByVal cuboid2_MaxY为双精度,ByVal cuboid2_MinZ为双精度,ByVal cuboid2_MaxZ为双精度双精度)布尔型
func_Intersect=True
如果长方体1\u最大值<长方体2\u最小值,则
func_Intersect=False
如果长方体2最大<长方体1最小,则
func_Intersect=False
如果长方体1最大<长方体2最小
func_Intersect=False
如果长方体2最大<长方体1最小
func_Intersect=False
如果长方体1最大<长方体2最小
func_Intersect=False
如果长方体2最大<长方体1最小
func_Intersect=False
如果结束
端函数
你所说的边是什么意思?可能是面?你的算法假设立方体是轴对齐的吗?抱歉打扰你^^@Dave,是的。就像前面问题的答案一样。如果这个假设是错误的,问题就变得更复杂了。可能没有人注意到,但帖子的第5条中有一个错误“CubeA.Y1