Search Unity

Mesh collider skipping and bouncing

Discussion in 'Physics' started by Cramonky, Jun 8, 2015.

  1. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Hey guys,

    I've recently begun working on a mini golf game, and my plan for it was to use a large portion of Unity's physics to handle the golf ball, and for simple/flat holes this works fine. However, a major issue comes in when I need to make more complex holes like this one: http://i.imgur.com/2yYHyNY.png

    My intent was to use mesh colliders for the ground, since I need the accuracy. However, when using mesh colliders, the ball tends to skip and hop as it rolls or slides across the ground. This is a very big problem and I was wondering if anybody had any ideas as to how I could fix this (short of writing my own physics or something)?
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If you dont want to write any code, than your only option might be to increase the amount of polygons your complex holes have so that you can smooth them out. If you used blender, put a sub division modifier on the mesh and apply it on export.

    Backup your model in case you make any mistakes.
     
  3. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Thanks for the reply, but I've actually tried that already, and that doesn't fix the problem.

    Something else I have noticed is that the ball doesn't jump at all when rolling across a more flat mesh collider like this: http://i.imgur.com/qgpfjMv.png
     
  4. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Im still rather new to unity myself. This option has been known to fix physics anomolies like jittering, but Im not exactly sure just how bad it can affect a game's performance since it can eat up more processing power.

    Edit > Project settings > time and set Fixed timestep to a slightly lower number like .001 or lower. This allows more physics calculations to be performed.
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    .001 would be 1000 times a second. Waaaaaay overkill.
    Try .01
     
  6. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Changing the physics timestep doesn't fix the issue either.
     
  7. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Did you reapply the mesh collider after you made the ground smooth? Maybe it didnt update.
     
  8. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Yeah the mesh collider definitely is up to date.

    Also this issue doesn't only happen on the ground, it happens with walls as well.
     
  9. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Well, its hard to tell what is going on and what you mean by bouncing and skipping. If you mean the ball is not rolling completely smoothly, than I still think it might just be your terrain is just not smooth enough. If you can tell the ground has polygons, then the ball will bounce or move weird when it hits a flat polygon.

    Maybe turn on interpolation on the rigidbody.
     
  10. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
  11. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    mind posting your scene so I can go inside and test myself?
     
  12. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
  13. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    The problem seems to be Continous dynamic being enabled. I think I read somewhere that the way continuous dynamic prevents from going inside things is by just completely stopping. When your ball goes way to fast, it probably just stops in the direction its going, but then then the physics pushes it way up in the air.

    Turn your rigidbody to discrete and you will notice the problem stops, but now if you go way to fast you will go through things.
    The problem can still happen with discrete, but its probably a lot less.
     
  14. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Weird, I still have the problem even when using discrete... Are your physics settings different than the ones I posted earlier? My fixed timestep is 0.01 as well.
     
  15. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    No, the more I look at it, the more I think I can still see the little jumps. It was just the initial force at the start that removing continuous dynamic fixed. The force was like 2750, and I had to do some code changing since you didn't give everything and had some missing references.

    EDIT - Im gana message your inbox.
     
    Last edited: Jun 8, 2015
  16. Zireael07

    Zireael07

    Joined:
    Apr 27, 2016
    Posts:
    47
    I am having a similar problem. Whether it's discrete or continuous dynamic, my cars bounce randomly. Maybe it's related to the size of the mesh collider?
     
  17. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Setting it to descrete or continuous collision made no difference, I only first thought it did, but was mistaken. Unfortunately, from looking at my conversation with him in my inbox, we found no fix and he said he might try terrain colliders.
     
  18. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    This problem was actually fixed for the most part in some update a while back (I forget which one).

    However, there is one important thing related to this problem that may not be apparent at first:
    Mesh colliders that use meshes with split edges or "flat" shading will cause other colliders to bounce when they roll or slide over edges.

    Take these 2 meshes for example: https://i.gyazo.com/cb972bb2822ed231027b35898bd3d74d.png

    The mesh in front has no split edges and so colliders will (most likely) have no problems when rolling or sliding over it. You can tell that it has no split edges because of how smooth it looks.

    The mesh in the back however does have split edges, and so colliders may bounce when they roll over this meshes edges. You can tell that it has split edges because of the noticeable edges between each rectangle.

    With completely flat surfaces it's basically impossible to tell whether a mesh has split edges or not unfortunately. But if you are exporting from Blender you need to make sure your collision meshes are set to "Smooth" shading, otherwise their edges will be split and you will have bouncing problems.
     
    Zireael07 and HiddenMonk like this.
  19. Zireael07

    Zireael07

    Joined:
    Apr 27, 2016
    Posts:
    47
    Even more reason for me to extract just the pieces from the free city model I'm using. Thanks for the tip!
     
  20. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Are you sure that changing the shading to smooth does anything (for collision)? You are saying "The mesh in front has no split edges..", but isnt that wrong? It still has all the split edges, the only change is the vertices normals.
    This video explains it well.


    From my quick tests, it doesnt seem like the smooth shading is changing the actual normals returns by OnCollisionStay contact normals or raycast / spherecast.
    Here is my test video with the OnCollisionStay contact normals, both smooth and non smooth seem to return the exact same results.


    All in all, smooth shading is just for visuals as far as I know.

    Do you have any proving examples of smooth shading doing something, or is it maybe just the update that fixed everything? I am really curious since if this smooth shading thing actually does something, then it should very much be documented.
     
  21. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    186
    Well...

    I just did some tests of my own and it looks like I was wrong about basically everything I said in my post.

    The reason I thought this would be the case is because a while back I was messing around with combining meshes in Unity. Something I discovered is that if you have a set of meshes like this (made up of individual quads in this case): https://i.gyazo.com/0b9b7486a3dad0a6293dc5fcf3ea21b2.png and you combine them using Unity's builtin Mesh.CombineMeshes() function, colliders will bounce and hop when rolling or sliding over the combined mesh.

    So what I had to do was write my own mesh combiner that kind of "repaired" the mesh as it was combining, merging triangles with shared vertices and whatnot. I just assumed that imported meshes would behave the same way, but it seems like I was wrong.
     
    Last edited: May 16, 2016
    HiddenMonk likes this.
  22. mar10

    mar10

    Joined:
    Jan 30, 2014
    Posts:
    11
    HI there, when I updated my project to 5.3.5f1 I noticed that with some of my map buildings that have mesh colliders; with mild jagged edges that when my capsule collider makes contact with the edges it becomes jittered and then suddenly teleports in 5.1.1f1 this does not happen so I have been stuck using that version of unity and will likely build the final game in that version. I hope someone in unity can shed some light. thanks.
     
  23. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    592
    I belive you do not use for every physic calculations the FixedUpdate method. And yes, the Physic engine from NVidia was not the best choice. I also got problems driving over small gaps with wheel collider. My vehicles jumped and turned to the back.
     
  24. mkgame

    mkgame

    Joined:
    Feb 24, 2014
    Posts:
    592
    Play around with Rigidbody "Collision Detection" and "Interpolate". May much more expensiver, but for one ball absolutely okay.