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)?
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."
I think you would have to decompose your concave collider into a collection of convex colliders, then iterate them.
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.
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).
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.
As @Kurt-Dekker 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.
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: However when I import any asset from the asset store, it breaks down: 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
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: To epsilon 0.0005 without errors: