Search Unity

Check if two meshes intersect

Discussion in 'Scripting' started by Emolk, Jul 17, 2021.

  1. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    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)?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    HamCha87 and GroZZleR like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think you would have to decompose your concave collider into a collection of convex colliders, then iterate them.
     
    HamCha87 and Emolk like this.
  5. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    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.
     
  6. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    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).
     
    Emolk likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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.
     
    Emolk and _geo__ like this.
  8. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    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.
     
    HamCha87, Emolk and _geo__ like this.
  9. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    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
     
  10. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    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
     
  11. TinyEasy

    TinyEasy

    Joined:
    May 18, 2022
    Posts:
    21
    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:

    0,00001.jpg

    To epsilon 0.0005 without errors:
    0,0005.jpg
     

    Attached Files:

    adamgolden likes this.