重叠的立方体
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
接受的答案是错误的并且非常令人困惑。这是我的想法。
确定 x 平面中的重叠
确定 y 平面中的重叠
确定 z 平面中的重叠
如果将所有这些条件与结果组合在一起确实如此,您知道立方体在某个点相交。
信用: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
Determining overlap in the y plane
Determining overlap in the z plane
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/
您应该能够修改 确定两个矩形是否彼此重叠? 很容易达到你的目的。
假设您有
CubeA
和CubeB
。 6 个条件中的任何一个都保证不存在重叠:因此不重叠的条件是:
因此,重叠的充分条件是相反的 (De Morgan)
You should be able to modify Determine if two rectangles overlap each other? to your purpose fairly easily.
Suppose that you have
CubeA
andCubeB
. Any one of 6 conditions guarantees that no overlap can exist:So the condition for no overlap is:
Therefore, a sufficient condition for Overlap is the opposite (De Morgan)
立方体由 6 个矩形(好吧,正方形)面组成。
如果满足以下条件,两个立方体不会相交。
您链接的帖子可以轻松扩展。只需添加 Z 即可。
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.
我想(没有想太多,也许我的条件不够)检查第一个立方体的所有顶点是否都在第二个立方体之外并且相反:第二个立方体的所有顶点都在第一个立方体之外。
要检查顶点是否在立方体中,请将其坐标转换为与立方体相关的坐标系(将平移应用于立方体中心和立方体旋转)。然后简单地检查每个坐标(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
这只是经过更正重写的已接受答案。它测试两个轴对齐的长方体是否具有 X、Y 和 Z 轴共同的任何段,如果没有,则它们不可能发生碰撞。
该函数假设存在碰撞并执行测试以检查是否不存在。
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.