Search Unity

Question Very messed up collisions with mesh colliders

Discussion in 'Physics' started by GeriB, Sep 5, 2020.

  1. GeriB

    GeriB

    Joined:
    Apr 26, 2017
    Posts:
    193
    Hi. I'm trying to get the simplest car setup ever ever but physics keep getting on the way. It's driving me crazy!

    It's a car that moves forward using an AddForce and turns with an AddTorque. It's literally 3 lines of code and it works on flat ground (huge box collider):


    But it just won't work with a custom mesh + Mesh Collider:

    Although all colliders involved are using a Physics Material with everything set to 0 it behaves as if it's getting stuck to the ground and bounces randomly when it should behave in a simillar manner to when the ground is flat (previous clip)

    I'm on version 2019.4.8, all the Physics Settings are set to default values. The "car" has a Rigidbody with Continous collision detection and everything un-constrained.

    I have 0 idea what the heck is going on and I have years of experience with Unity... Any help would be inmensly appreciated.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    As your car is a box collider then the physics engine has to resolve a collision every time the box drives over a collider face with different angle.
    upload_2020-9-5_10-47-54.png
    It's a collision with penetration, so the physics engine has to de-penetrate the box from the ground collider. This no different to standard collider-collider collisions, so the same strategies are used to resolve the collision - basically applying an impulse to put colliders away from each other.

    You may try setting low values to Rigidbody.maxDepenetrationVelocity, but I think you will still encounter the issue.

    Maybe using a beveled box could work better, as the collision may be resolved using the bevel face providing some additional smoothness. I don't think this will remove the issue completely though.

    Maybe using capsule colliders instead of a box collider could give better results.

    Possibly using a CharacterController and its methods instead of a box collider + AddForce/AddTorque will work acceptably, as the character controller should handle these situations correctly. But I don't know if that would work for all the stunts you have in your scene (specially the loops).

    Not surprisingly, the best solution for cars is using WheelColliders. You may configure the suspension parameters (distance, spring, damper) and set all the tire friction parameters to zero. Use an script for that, as the inspector doesn't allows 0 in some fields. This will give you exactly what you have already (friction-less object), but driven on top of four springs that will allow your car to drive smoothly move over custom colliders. As long as the box collider stays above the ground collider, you car will drive smoothly over it.
     
  3. GeriB

    GeriB

    Joined:
    Apr 26, 2017
    Posts:
    193
    Thanks for the insightfull response.

    I tried changing the Rigidbody.maxDepenetrationVelocity but I see no effect in the simulation regardless of the value I assign to it :/

    And the collider has thes capsule like shape, it probably shouldn't get stuck this bad. I even made a weird contraption with a sphere collider and a box collider to test and this keeps happening: upload_2020-9-5_12-21-21.png

    The character controller approach will probably work but I rather keep the full physicality, it will make interactions with other objects easier. But worst case I could give it a go.

    I think this project is cursed :confused:
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Then I think the best choice is WheelColliders. Or implement a spring-damper system, with in the end is the same as the WheelColliders as I described above, and the result tends to be more stable with them.
     
    GeriB likes this.