Search Unity

Bug Object collide separately with every triangle of a Meshcollider

Discussion in 'Physics for ECS' started by Nams33, Mar 22, 2023.

  1. Nams33

    Nams33

    Joined:
    Oct 14, 2018
    Posts:
    6
    Hello,

    While I am currently trying to handle this kid of issue with physics, where an object (blue) is moving on the ground (green left) and get stopped by an other ground (green right) object on the same level.


    I tried creating a mesh collider manually :
    Code (CSharp):
    1.              
    2.                 vertices.Add(new float3(-5, 0, 0));
    3.                 vertices.Add(new float3(0, 0, -5));
    4.                 vertices.Add(new float3(0, 0, 5));
    5.                 vertices.Add(new float3(5, 0, 0));
    6.                 triangles.Add(new int3(0,1,2));
    7.                 triangles.Add(new int3(1,2,3));
    8.  
    But I figured out that the issue persist. My (blue) object still stop around (0,0,0) when moving on the x axis because of the decollision.
    Is it supposed to be the normal behavior ? I would have expected the decollision to happen on the whole mesh level, not on a per triangle level.

    Edit 1 : This is more easily reproductible by creating a plane with a Physics Shape whose Shape Type is set to mesh

    Edit 2 : I observe the same behavior with compound colliders
     
    Last edited: Mar 22, 2023
    Selmar likes this.
  2. Sima_Havok

    Sima_Havok

    Joined:
    Dec 16, 2019
    Posts:
    52
    In continuous collision detection (predictive physics) prediction works on a leaf shape level. This means for collision against mesh you can collide with any of the triangles withing motion range and in case of compound you can collide with any underlying shape within motion range. So colliding against any triangle is expected.

    This video is a must for getting familiar with speculative contacts (applied to both Unity.Physics and Havok.Physics):
    Overview of Havok Physics in Unity - Unite Copenhagen - YouTube

    Detailed discussion on this topic can be found on the thread below
    Question - Sphere collider receives strong impulse on contact with mesh collider edge - Unity Forum
     
    Nams33 likes this.
  3. Nams33

    Nams33

    Joined:
    Oct 14, 2018
    Posts:
    6
    Thank you a lot for theese info.

    For information, for the moment, we tacled the problem by doing the 2 following steps:
    1- We override the normals in the ModifiableContactHeader by the normal of the triangle, instead of the normal of the contact. This leads, for a MeshCollider representing a plane, to all normals pointing in the same direction.
    2- As we had issues moving along edges (i.e. MeshCollider representing a cube), we disabled contacts where the contact normal was not going in the same way as the triangle normal.

    if (math.dot(manifold.Normal, WorldNormal) < 0.90f) // We only keep alive contacts with triangles whose normal is oriented in the same way as the contact point normal
    manifold.JacobianFlags = JacobianFlags.Disabled;
     
    daniel-holz likes this.
  4. Sima_Havok

    Sima_Havok

    Joined:
    Dec 16, 2019
    Posts:
    52
    Glad to help. Regarding your step 1 that is the most performant way to deal with most of the welding issues. It works perfectly for flat meshes and for concave sections of the mesh. Once you start hitting convex parts of the mesh "rotating" the normal can result in object initially penetrating new separation plane.

    For the step 2 my advise is to use the most specialized collider you can. What I mean by that for your cube you should use BoxCollider. If specialized collider doesn't exist than you should check the Convex flag on MeshCollider every time you are dealing with the "general" convex body. Mesh Collider with Convex flag checked will provide single contact, no matter how much faces your convex mesh collider has. That way you wouldn't have to disable any contact and your perf would be better because physics will have less work to do.
     
    daniel-holz likes this.