Checking for overlapping

One of the common tasks when making a scene in Blender is to control the overlapping of meshes. We can check whether two meshes intersect with each other using the technology of Bounding Volumes.

The “mathutils” module gives us the ability to easily build Bounding Volume Hierarchy Trees (BVH-Trees) for any meshes in the scene, and use them to check for intersections.

For example, let’s add a UV sphere to the scene (shift + a – Mesh – UV Sphere) and a plane (shift + a – Mesh – Plane). For the plane, let’s add some subdivisions in edit mode.

In order to correctly check for intersections with BVH-Tress, the meshes they are built from must be in the same coordinate system.

Let’s calculate the points of both meshes (plane and sphere) in a single coordinate system – World. To do this, we need to multiply the coordinates of each mesh vertex by mesh’s world matrix.

We also generated lists of mesh polygons with the associated vertices.

With these two lists (vertices coordinates and polygon-vertices ratios), we can build a BVH-Tree for each mesh:

Having two generated BVH trees, we can easily find intersections:

The “overlap” method returns a list of overlapping polygon indices as pairs:

(“polygon index of mesh 1”, “polygon index of mesh 2”)

For a simple understanding are the meshes intersect or not, it is enough to check that there is at least one value in the resulting list:

For a more detailed intersection check, let’s parse the resulting list into two separate lists, each of which belongs to its own mesh:

Now we can select polygons that have intersections on each mesh (in object mode):

4 1 vote
Article Rating
Subscribe
Notify of
guest

2 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Jmcp
Jmcp
2 years ago

Could be this somehow use to detect overlapping uvs?

Nikita
Editor
2 years ago
Reply to  Jmcp

This method detects overlapping of two different meshes. It can’t work with UV-s.
To find UV’s overlapping I think it could be possible to build the BVH-tree for each island and try to detect overlapping of that trees.