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.

Question [3D] Ball rolling on floor, incorrectly collides with edges of floor pieces and bounces up

Discussion in 'Physics' started by Mornedil, Nov 4, 2022.

  1. Mornedil

    Mornedil

    Joined:
    Feb 7, 2016
    Posts:
    19
    I have a ball rolling across a floor consisting of multiple box colliders that are lined up.
    However, when the ball crosses an intersection between two different colliders, it acts like it's hitting a corner and bounces upwards.

    (6 second video showing the issue)


    How can this be solved?
    Preferably without having to create one big floor collider, since the plan is to create multiple different pieces and dynamically place them at runtime
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    This is a long running 3D physics problem (I seem to recall @MelvMay implemented a solution for 2D). Boxes are your worst choice for this setup; as you can imagine, small precision deviations present the boxes as tilted or raised to one another, combine this with the contact offset and you start to get small edge trips.

    I believe that some improvement can be had by reducing Collider.contactOffset to a "small" number (it is required to be positive, but you could make it near zero, say 0.0001 to 0.000001), this is all theoretical to me, I have no experience of it, so you will need to experiment with values.

    Adding a little bounce to your physics materials may make them more forgiving of these errors when they arise.
    I believe collision detection Continuous will be better than Speculative, so check that.

    I think the best solution would be to combine the meshes, using: https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Mesh.CombineMeshes.html and then weld the duplicate vertices together (be sure to weld the ones with duplicate normals or recalculate a combined normal) this guy has posted his welding code, perhaps it can help you too https://forum.unity.com/threads/mesh-combine-welding-optimization-for-mobile.1092262/ I think combine leaves the children there, so you may need to disable them after you generate the combined mesh.
     
    Mornedil likes this.
  3. Mornedil

    Mornedil

    Joined:
    Feb 7, 2016
    Posts:
    19
    Thank you, that's a lot of good advice.
    I've been experimenting with the contactOffset value but seems like there's a risk of it happening regardless of the value.
    Combining meshes seems like the most lucrative approach, I'll look into that!

    I saw that the Havok physics engine also has an option called "contact welding" that's meant to tackle this issue, but I may go for the manual approach of welding meshes together.
    Have had trouble getting Havok working at all. Despite installing the package and enabling it with the Physics Step script as explained here, Unity seems to still use Unity Physics and won't swap to Havok.
     
    AlTheSlacker likes this.
  4. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Mornedil likes this.