Finding the intersection of 2 arbitrary cubes in 3d

限于喜欢 提交于 2019-12-05 18:01:14

You should take a look at the field of computer graphics. They have many means. E.g. Weiler–Atherton clipping algorithm. There are also many datastructures that could ease up the process for you. To mention AABBs (Axis-aligned bounding boxes).

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. Again, for simplicity, let's assume you will be using 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

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.

Try using the separating axis theorem. it should apply in 3d as it does in 2d.

If you create polygons from the sides of the cubes then another approach is to use Constructive Space Geometry (CSG) operations on them. By building a Binary Space Partitioning (BSP) tree of each cube you can perform an intersection on them. 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

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

I've ported this library to C# at

https://github.com/johnmott59/CGSinCSharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!