重叠的立方体

发布于 2024-10-17 23:18:38 字数 218 浏览 2 评论 0原文

我正在尝试确定两个立方体是否重叠。我已经阅读了重叠矩形,但我没有确定如何将其转换为三维。

我的目标是生成许多随机定位和大小的不重叠的立方体。

这些立方体在 ax,y,z 笛卡尔平面上表示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

熊抱啵儿 2024-10-24 23:18:38

接受的答案是错误的并且非常令人困惑。这是我的想法。

确定 x 平面中的重叠

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

确定 y 平面中的重叠

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

确定 z 平面中的重叠

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

如果将所有这些条件与结果组合在一起确实如此,您知道立方体在某个点相交。

信用:https://silentmatt.com/rectangle-intersection/

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/

冷夜 2024-10-24 23:18:38

您应该能够修改 确定两个矩形是否彼此重叠? 很容易达到你的目的。

假设您有 CubeACubeB。 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
              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

因此不重叠的条件是:

Cond1 or Cond2 or Cond3 or Cond4 or Cond5 or Cond6

因此,重叠的充分条件是相反的 (De Morgan)

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

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
遗忘曾经 2024-10-24 23:18:38

立方体由 6 个矩形(好吧,正方形)面组成。

如果满足以下条件,两个立方体不会相交。

  • 2 个立方体的面都不相交。
  • 一个立方体并不完全包含另一个立方体。

您链接的帖子可以轻松扩展。只需添加 Z 即可。

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.

妳是的陽光 2024-10-24 23:18:38

我想(没有想太多,也许我的条件不够)检查第一个立方体的所有顶点是否都在第二个立方体之外并且相反:第二个立方体的所有顶点都在第一个立方体之外。

要检查顶点是否在立方体中,请将其坐标转换为与立方体相关的坐标系(将平移应用于立方体中心和立方体旋转)。然后简单地检查每个坐标(x,y,z)是否小于半边

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

失而复得 2024-10-24 23:18:38

这只是经过更正重写的已接受答案。它测试两个轴对齐的长方体是否具有 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文