Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Mesh collider incorrectly simplifying mesh

Discussion in 'Physics' started by duckanddicegames, Aug 2, 2023.

  1. duckanddicegames

    duckanddicegames

    Joined:
    Jul 15, 2022
    Posts:
    3
    Hello! I'm working on a game where the player can grow and shrink in Unity 2023.1.6f1. The items for this game can grow and shrink too, so they need to have precise collisions. To achieve that I have stuck to low-poly objects (about 200-800 tris per object) and I manually create convex mesh colliders by splitting the model into convex chunks of 100 tris or so.

    I'm currently stuck on how to get the mesh colliders for the items to work properly since the mesh collider component is behaving weirdly. I've attached an example of the collider I want and the collider I get.

    good.png

    bad.png

    You can see how the mesh collider component is ignoring the wireframe of the mesh completely.

    wireframe.png

    The mesh in question has 160 vertices and 158 triangles, so the complexity of the mesh should not be an issue according to the documentation. I am also quite certain that the mesh is convex because it's just a cylinder with bevels. I have tried other simpler shapes like a ring to discard complexity or shape issues and the problem still occurs.

    ring_good.png

    ring_bad.png

    Is this something wrong with Unity? I have tried to change many model import options and mesh cooking options without any result whatsoever. I would like to solve this without remaking the model. Can anyone help?
     
  2. duckanddicegames

    duckanddicegames

    Joined:
    Jul 15, 2022
    Posts:
    3
    For anyone who has the same error as me, I managed to "fix" the issue by scaling the models 10x in Blender and re-importing them. I still have no idea what caused the issue or why this solves it. Probably has something to do with precision.

    This solution is a workaround. If anyone knows why this happened and how to avoid it in the future that would be cool to know.
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Interesting!. I just did a little test by setting a cylinder's scale to 0.01 in the import settings and hitting Apply. When enabling Convex in the mesh collider settings many of the collider's triangles were removed just like yours.

    I guess we gotta keep those verts apart if we don't want to risk losing them. Seems reasonable..
     
    duckanddicegames likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Maybe it relates to the constraints used in the cooking options?

    EDIT: Noted that the OP said they experimented with said options; presumably not using mesh clean-up/welding options.
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Yeah that was the first thing I tried. It seems the moment the 'Convex' flag is enabled Unity merges nearby verts regardless of any settings. Although it's possible it only does this if the overall scale of the mesh is very small.
     
  6. duckanddicegames

    duckanddicegames

    Joined:
    Jul 15, 2022
    Posts:
    3
    Diving a bit deeper into the problem I found out that cooking options were not a factor in this (at least in my case). I tried with no cooking and all cooking and the results were consistent: tiny meshes worked badly and normal-sized meshes worked fine. The one affecting variable that I found was just the scale of the model.

    As far as I know, when unity is given a mesh and told to make it convex there has to be some processing there to encapsulate all of the vertices into a polygon. I'm just shooting blanks at this point, but maybe if it's computing the convex polygon and a vertex is just a little outside the polygon it ignores it (probably to merge faces in the same plane accounting for floating point errors). The resulting "bad" collider was really close to the real one and there were no console errors.

    In hindsight, I agree with you in that this is reasonable behaviour and not a bug. I was trying to create tiny models and then upscale them when it's probably best to do the opposite. Maybe for future reference, it just needs to be documented somewhere that very small meshes do not work well with mesh colliders.
     
    zulo3d likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    In the end, Unity uses the PhysX library to do all this geometry cooking (pxCooking::createConvexMesh): https://gameworksdocs.nvidia.com/Ph...xguide/Manual/Geometry.html?highlight=cooking

    I don't know much about this but I do know that if you have any scaling in your Transform then it'll be scaled before being processed so maybe that scaling brings verts too close together (Unity again uses the default tolerances for this by the looks of it).

    That's about all I know about it.
     
    duckanddicegames likes this.