Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Errors about mesh collider when CONVEX is set to true

Discussion in '5.5 Beta' started by Alan-Liu, Nov 3, 2016.

  1. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    391
    I use the following code to create a mesh for convex mesh collider, but it causes some errors.
    Code (CSharp):
    1.         GameObject go = new GameObject();
    2.         MeshCollider collider = go.AddComponent<MeshCollider>();
    3.  
    4.         Mesh newMesh = new Mesh();
    5.         newMesh.name = "test";
    6.  
    7.         Vector3[] vertices = new Vector3[] {
    8.             new Vector3(0.0f, 0.0f, 0.0001f),
    9.             new Vector3(0.1f, 0.0f, 0.0f),
    10.             new Vector3(0.09f, 0.05f, 0.0000f),
    11.             new Vector3(0.05f, 0.07f, 0.0000f),
    12.             new Vector3(0.02f, 0.09f, 0.0f),
    13.             new Vector3(0.0f, 0.1f, 0.0f),
    14.         };
    15.  
    16.         int[] tris = new int[] {
    17.             0, 1, 2,
    18.             0, 2, 3,
    19.             0, 3, 4,
    20.             0, 4, 5
    21.         };
    22.  
    23.         //int[] tris = new int[] {
    24.         //    1, 2, 3
    25.         //};
    26.  
    27.         newMesh.vertices = vertices;
    28.         newMesh.triangles = tris;
    29.         newMesh.RecalculateBounds();
    30.  
    31.         collider.sharedMesh = newMesh;
    32.         collider.convex = true;
    Errors:
    • ConvexHullBuilder::CreateTrianglesFromPolygons: convex hull has a polygon with less than 3 vertices!
    • Gu::ConvexMesh::loadConvexHull: convex hull init failed! Try to use the PxConvexFlag::eINFLATE_CONVEX flag. (see PxToolkit::createConvexMeshSafe)
    • Failed to create Convex Mesh from source mesh "test". Source mesh is likely have too many smooth surface regions. Please reduce the surface smoothness of the source mesh. Alternatively turn on Inflate Mesh and increase the Skin Width sufficiently for this mesh.
    I think the errors are due to all vertices of mesh are nearly on the same plane . But, there are still some strange behaviors:
    • If I set the first vertex to (0.0f, 0.0f, 0.0f), there is no error.
    • If I use commented "tris" variable to create the mesh, so the mesh contains only one triangle, but there are same errors.
    Can somebody explain these two strange behaviors for me? Thank you.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not a convex shape and can't be used as a convex collider. Also the scale is very small which physics doesn't really like.

    --Eric
     
  3. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    391
    The mesh of convex mesh collider dosen't need to be convex, physics engine should create it automatically, if I understand correctly.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It needs a 3D shape to work from, though, not a 2D shape. Or rather, some form of enclosed volume. If you create a similar shape in a 3D app and import it, and try to make a convex mesh collider from it, you'll see that you don't get anything usable. Out of curiosity I did some experimenting with that just now, and the results ranged from nothing at all, to the same error that you got, to (best case) a generic box that wasn't even close to the actual size of the mesh. So yeah, you're going to have to extrude the mesh yourself if that's what you were aiming for with the convex collider.

    --Eric
     
  5. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    391
    But, why it's correct when I set the first vertex to (0.0f, 0.0f, 0.0f)? It's also a 2d shape.
    upload_2016-11-3_15-13-53.png
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just lucky I guess? The boxes I got where nowhere close to the size of the mesh. I think it's just a bad idea to attempt to make a convex mesh from a 2D shape since the results aren't at all consistent, and are presumably undefined.

    --Eric
     
  7. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    391
    OK, it seems that I should not use 2d shape to create convex mesh collider like you said. Thank you.