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.
  2. Dismiss Notice

Box Colliders landing on edges

Discussion in 'Physics' started by REDvsGREEN, Aug 5, 2022.

  1. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    I've been trying and looking for a solution for a problem I have when rolling dices (box colliders) in Unity.

    The dices often land on the edges: (If I roll 6 dices, about every third throw results in this behavior)

    upload_2022-8-5_13-18-49.png

    The physics settings for said dices:

    upload_2022-8-5_13-18-13.png

    This seems to be a general Unity Physics issue since it can easily be replicated on many versions with just the default box collider.

    Things I have tried to do to solve it:
    1. Tried different versions, 2018-2021
    2. Tried all different Collision Detection settings
    3. Tried different mass/weight/drag
    4. Tried different interpolation
    5. When the dice comes to rest, tried applying a small downward force (nothing happens)
    6. When dice comes to rest, calculate direction and apply a force from the side to tilt it (nothing happens until you apply an insane amount of force, basically making the dice fly of screen.)
    7. Change the collider to one with beveled edges (only made it worse)

    I'm at loss now and really don't know what I should try next, so I'm hoping someone out there can help? Anyone has any ideas or seen it themselves?

    Very grateful for any help! Thanks!
     

    Attached Files:

  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    This is a band aid fix but you could try adding some torque based on it's current rotation and velocity. Not sure if that's what you tried with #6.
     
  3. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    Thanks for the suggestion, unfortunately it gives the same issue as nr.6, the dice wont move until large amount is applied and then it shoot off..

    I've tried changing:

    Sleep Treshold (increase)
    Default Contact Offest (Increase)
    Default solver Iterations (increase to 15)
    Contacts Generation (Swap to Legacy)
    Enable Adaptive Force
    Friction Type (all options)
    Contact Pairs mode (all options)
    Broadphase Type (all options)
    Solver Type (both options)

    upload_2022-8-8_11-4-50.png

    Also applying a bouncy material to the dices. After all these tests, the dices still land on the edges, albeit a bit less. Especially making the dices bounce around a lot helps. From 1/3 throws to 1/10 throws a dice lands on the edges..
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,663
    What do you mean the dices "land on the edges"? Do they stop moving on one edge, as if in equilibrium?

    If that's what you mean, I've never heard of or experienced something similar. Could it just be that they go to sleep in that pose?


    Do not increase the sleep threshold, that will make things worse by allowing rigidbodies to sleep even if their kinetic energy is large. The manual defines it as the "mass normalized energy threshold, below which objects go to sleep":
    https://docs.unity3d.com/ScriptReference/Physics-sleepThreshold.html

    So I guess it's something like 1/2 v^2 (instead of 1/2 mv^2 which would be the usual kinetic energy formula). If your dice are very small, their velocity will be too, and this will make it easy for them to go to sleep.

    Instead, try reducing the sleep threshold it or even setting it to zero.

    That would make sense, as bouncing increases kinetic energy, preventing it from going below the sleep threshold.
     
  5. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    In the settings screenshot, the dice is set to IsKinematic. Do you have a script that is setting the rigid body to kinematic before it has finished moving?
     
  6. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    Indeed, the better description would be "they fall asleep as they stand on the edge", they don't land on the edge.

    Yeah sorry, I wrote increase as in increased number of zeroes, so i changed the threshold from 0.001 to 0,00001 and nothing changed.
     
    arkano22 likes this.
  7. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    Good catch, this was just an error from me when testing, the dices are not set to kinematic when I test the "land on the edge" cases, it was just in the screenshot by error my my side.
     
    flashframe likes this.
  8. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,663
    Unity uses PhysX as its default physics engine, which has been used in thousands of commercial games (PhysX, Havok and Bullet are probably the trifecta of physics engines), so it would be weird for such a basic use case to break it.

    I just tried to spawn 1000 default cubes and throw them onto a flat floor (a squashed down box collider) and none of them fell/slept on their edge. Would it be possible for you to provide steps to reproduce this behavior you're getting? Going trough the steps will likely allow us to spot the problem.
     
  9. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    Only other thing that seems odd to me is the local centre of mass. Shouldn't it be 0,0,0?
     
    arkano22 likes this.
  10. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,663
    Good catch, that's definitely an issue.

    The center of mass of a cube should be on 0,0,0 (local coordinates) indeed. It being almost 10 meters away from the cube is a problem. That can cause the cube to balance on an edge, since all mass is concentrated outside the volume of the cube.

    Do you have other colliders below the cube's hierarchy? Unity will automatically calculate the center of mass of rigidbodies using the shape and location of all colliders in its hierarchy. So unless this is the case, or you have manually set the center of mass yourself, there's no other reason for it to have such a weird value.
     
    Last edited: Aug 9, 2022
  11. REDvsGREEN

    REDvsGREEN

    Joined:
    Sep 8, 2019
    Posts:
    34
    Thanks to you guys i started checking all the colliders in the scene, and it looks like they land on the edges close to colliders (I.E they are not landing on the edges, they are leaning towards invincible walls, which has the purpose of making them not fall of the edge)..

    The whole reason why I started this thread is because my customers who use the assets complained about the dices landing on the edges, I will contact a few of them to see if it happens inside their games as well.

    After moving the colliders much further the issue seem to have disappeared.. I will see if I can make it come back somehow.

    Big thanks @arkano22 and @flashframe !
     
    flashframe likes this.