Check if two meshes intersect

Say I have object A and object B. Both objects have a mesh renderer with a concave mesh collider.

How can I tell if those objects intersect one another (ie they are touching)?

https://docs.unity3d.com/ScriptReference/Physics.ComputePenetration.html

[quote=“GroZZleR”, post:2, topic: 848763]
https://docs.unity3d.com/ScriptReference/Physics.ComputePenetration.html
[/quote]

This doesn’t work for two concave colliders, re the docs:

“One of the colliders has to be BoxCollider, SphereCollider CapsuleCollider or a convex MeshCollider. The other one can be any type.”

2 Likes

[quote=“Emolk”, post:3, topic: 848763]
This doesn’t work for two concave colliders, re the docs:
[/quote]

I think you would have to decompose your concave collider into a collection of convex colliders, then iterate them.

2 Likes

[quote=“Kurt-Dekker”, post:4, topic: 848763]
I think you would have to decompose your concave collider into a collection of convex colliders, then iterate them.
[/quote]

Is there any way to do this in code?

I’m aiming to be able to detect intersection between any prefab/mesh the developer adds (this is for an asset), so I won’t be able to manually set colliders.

[quote=“Emolk”, post:5, topic: 848763]
Is there any way to do this in code?

I’m aiming to be able to detect intersection between any prefab/mesh the developer adds (this is for an asset), so I won’t be able to manually set colliders.
[/quote]

You can always read the meshData, create your own triangles and check for collisions between them.
Triangle x Triangle intersection code can be found online.
Optimization by checking bounding box first is highly recommend.

This method might be easier than computing your own convex colliders out of non-convex ones (there is an asset on the store which does that btw. but you can’t sell your asset using his code of course).

1 Like

[quote=“Emolk”, post:5, topic: 848763]
Is there any way to do this in code?
[/quote]

I think this area of mesh / graph study is broadly called constructive solid geometry, or CSG. I could be wrong on that but I think the same principles of CSG would apply to subdividing a concave collider.

2 Likes

[quote=“Emolk”, post:1, topic: 848763]
How can I tell if those objects intersect one another (ie they are touching)?
[/quote]
As [mention|8JLFhDQSR/y9hqzDv/BDDw==] mentioned, CSG is probably the direction to look. I recently used pb_CSG to get intersection data, but I didn’t notice a quick “AreMeshesIntersecting” kind of function - what I did was by comparing the resulting mesh data with the meshes I passed in to determine where the points of intersection were.

3 Likes

[quote=“adamgolden”, post:8, topic: 848763]
As [mention|8JLFhDQSR/y9hqzDv/BDDw==] mentioned, CSG is probably the direction to look. I recently used pb_CSG to get intersection data, but I didn’t notice a quick “AreMeshesIntersecting” kind of function - what I did was by comparing the resulting mesh data with the meshes I passed in to determine where the points of intersection were.
[/quote]

Thanks for pointing me to this, really cool library.

This works great when I create a cube and sphere in Unity (though there is some weird artifact) as seen below:

passionateweehairstreak

However when I import any asset from the asset store, it breaks down:

blushingleancollie

limiteddampflies

This is on a fresh project.

Does anybody know what is happening here and could point me in the right direction? I thought it had something to do with the mesh’s being shared in the same asset, but I tried it between separate models/assets and get the same result.

Cheers

I don't know what the issue could be, I'm far from well-informed on the library I just found it while searching for a way to work something out. Something that could save you some frustration though (that happened to me a bunch of times), is if it starts throwing exceptions while trying to intersect, you can increase the k_Epsilon value to like 0.005f in CSG.cs (or higher if you need to). That decreases the precision it's going for.. I think the math breaks down at extreme angles.

I ended up only using it for getting vertices in the result that are spacially "at the surface" of the original mesh - originally I was hopeful of more, but the meshes it generates are really rough aside from basic stuff. At the very minimum, if you do get clean-looking results, after seeing the wireframe you'll probably want to weld and simplify and remap uvs if you're texturing - there can be holes and triangles flying in the air depending on what you're trying for, so to get more out of it, you'll probably also find the geometry3Sharp library helpful. It has things in it for hole filling and so on.. boolean mesh operations as well, interesting tutorial here: http://www.gradientspace.com/tutorials/2017/11/21/signed-distance-fields-tutorial

[quote=“adamgolden”, post:10, topic: 848763]
I don’t know what the issue could be, I’m far from well-informed on the library I just found it while searching for a way to work something out. Something that could save you some frustration though (that happened to me a bunch of times), is if it starts throwing exceptions while trying to intersect, you can increase the k_Epsilon value to like 0.005f in CSG.cs (or higher if you need to). That decreases the precision it’s going for… I think the math breaks down at extreme angles.

I ended up only using it for getting vertices in the result that are spacially “at the surface” of the original mesh - originally I was hopeful of more, but the meshes it generates are really rough aside from basic stuff. At the very minimum, if you do get clean-looking results, after seeing the wireframe you’ll probably want to weld and simplify and remap uvs if you’re texturing - there can be holes and triangles flying in the air depending on what you’re trying for, so to get more out of it, you’ll probably also find the geometry3Sharp library helpful. It has things in it for hole filling and so on… boolean mesh operations as well, interesting tutorial here: http://www.gradientspace.com/tutorials/2017/11/21/signed-distance-fields-tutorial
[/quote]

Your suggestion of increasing the k_Epsilon is absolutely amazing. I have been struggling to try to remove odd artifacts from subtraction operations using the parabox library and could not figure it out until I have changed this. Thank you!!

It went from this with epsilon 0.00001 with errors:

8873889--1211781--0,00001.jpg

To epsilon 0.0005 without errors:
8873889--1211775--0,0005.jpg

8873889--1211781--0,00001.jpg

1 Like