简体   繁体   English

在3D中找到2个任意立方体的交集

[英]Finding the intersection of 2 arbitrary cubes in 3d

So, I'd like to figure out a function that allows you to determine if two cubes of arbitrary rotation and size intersect. 因此,我想找出一个函数,该函数可让您确定任意旋转和大小的两个立方体是否相交。

If the cubes are not arbitrary in their rotation (but locked to a particular axis) the intersection is simple; 如果立方体的旋转不是任意的(而是锁定在特定的轴上),则交点很简单; you check if they intersect in all three dimensions by checking their bounds to see if they cross or are within one another in all three dimensions. 您可以通过检查边界以查看它们是否在所有三个维度上交叉或彼此相交,来检查它们是否在所有三个维度上相交。 If they cross or are within in only two, they do not intersect. 如果它们相交或仅在两个内,则它们不相交。 This method can be used to determine if the arbitrary cubes are even candidates for intersection, using their highest/lowest x, y, and z to create an outer bounds. 此方法可用于确定任意多维数据集是否甚至是相交的候选对象,方法是使用它们的最高/最低x,y和z来创建外边界。

That's the first step. 那是第一步。 In theory, from that information we can tell which 'side' they are on from each other, which means we can eliminate some of the quads (sides) from our intersection. 从理论上讲,根据这些信息,我们可以知道它们彼此在哪个“边”上,这意味着我们可以从交点中消除一些四边形(边)。 However, I can't assume that we have that information, since the rotation of the cubes may make it difficult to determine simply. 但是,我不能假定我们拥有该信息,因为立方体的旋转可能使简单确定变得困难。

My thought is to take each pair of quads, find the intersection of their planes, then determine if that line intersects with at least one edge of each of the pairs of sides. 我的想法是采用每对四边形,找到它们的平面的交点,然后确定该线是否与每对边中的至少一个边相交。 If any pair of sides has a line of intersection that intersects with any of their edges, the quads intersect. 如果任一对边的相交线与它们的任意一条边相交,则四边形相交。 If none intersect, the two cubes do not intersect. 如果没有相交,则两个立方体不相交。

We can then determine the depth of the intersection on the second cube by where the plane-intersection line intersects with its edge(s). 然后,我们可以通过平面相交线与其边缘相交的位置来确定第二个立方体上相交的深度。

This is simply speculative, however. 但是,这仅仅是推测性的。 Is there a better, more efficient way to determine the intersection of these two cubes? 有没有更好,更有效的方法来确定这两个立方体的交点? I can think of a number of different ways to do this, and I can also tell that they could be very different in terms of amount of computation required. 我可以想到许多不同的方法来执行此操作,而且我还可以说,就所需的计算量而言,它们可能有很大不同。

I'm working in Java at the moment, but C/C++ solutions are cool too (I can port them); 目前,我正在使用Java,但是C / C ++解决方案也很酷(我可以移植它们)。 even psuedocode since it is perhaps a big question. 甚至是伪代码,因为这可能是个大问题。

You should take a look at the field of computer graphics. 您应该看一下计算机图形学领域。 They have many means. 他们有很多手段。 Eg Weiler–Atherton clipping algorithm . 例如, Weiler–Atherton裁剪算法 There are also many datastructures that could ease up the process for you. 还有许多数据结构可以为您简化流程。 To mention AABBs ( Axis-aligned bounding boxes ). 提到AABB( 轴对齐的边界框 )。

To find the intersection (contact) points of two arbitrary cubes in three dimensions, you have to do it in two phases: 要在三个维度上找到两个任意立方体的相交(接触)点,必须分两个阶段进行:

  1. Detect collisions. 检测碰撞。 This is usually two phases itself, but for simplicity, let's just call it "collision detection". 这本身通常是两个阶段,但为简单起见,我们将其称为“冲突检测”。

The algorithm will be either SAT (Separating axis theorem), or some variant of polytope expansion/reduction. 该算法可以是SAT(分离轴定理),也可以是多面体扩展/缩小的某种变体。 Again, for simplicity, let's assume you will be using SAT. 再次,为简单起见,让我们假设您将使用SAT。

I won't explain in detail, as others have already done so many times, and better than I could. 我不会详细解释,因为其他人已经做了很多次,而且比我能做的更好。 The "take-home" from this, is that collision detection is not designed to tell you where a collision has taken place; 在“带回家”从这个,是碰撞检测的目的不是告诉你在哪里碰撞已经发生; only that it has taken place. 只是它已经发生了。

  1. Upon detection of an intersection, you need to compute the contact points. 在检测到交叉点时,您需要计算接触点。 This is done via a polygon clipping algorithm. 这是通过多边形裁剪算法完成的。 In this case, let's use https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm 在这种情况下,让我们使用https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm

There are easier, and better ways to do this, but SAT is easy to grasp in 3d, and SH clipping is also easy to get your head around, so is a good starting point for you. 这样做有更好的方法,但是SAT在3d中很容易掌握,SH裁剪也很容易引起您的注意,因此这对您来说是一个很好的起点。

Try using the separating axis theorem. 尝试使用分离轴定理。 it should apply in 3d as it does in 2d. 它应像在2d中一样在3d中应用。

If you create polygons from the sides of the cubes then another approach is to use Constructive Space Geometry (CSG) operations on them. 如果从多维数据集的侧面创建多边形,则另一种方法是在其上使用构造空间几何(CSG)操作。 By building a Binary Space Partitioning (BSP) tree of each cube you can perform an intersection on them. 通过为每个多维数据集构建一个二进制空间分区(BSP)树,您可以对它们执行交集。 The result of the intersection is a set of polygons representing the intersection. 相交的结果是一组代表相交的多边形。 In your case if the number of polygons is zero then the cubes don't intersect. 在您的情况下,如果多边形的数量为零,则立方体不相交。

I would add that this approach is probably not a good real time solution, but you didn't indicate if this needed to happen in frame refresh time or not. 我要补充一点,这种方法可能不是一个好的实时解决方案,但是您没有指出是否需要在帧刷新时间内进行。

Since porting is an option you can look at the Javascript library that does CSG located at 由于移植是一个选项,因此您可以查看位于CSG的Javascript库

http://evanw.github.io/csg.js/docs/ http://evanw.github.io/csg.js/docs/

I've ported this library to C# at 我已将此库移植到C#

https://github.com/johnmott59/CGSinCSharp https://github.com/johnmott59/CGSinCSharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM