Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Ball Bounce Physics Exit Trajectory

Discussion in 'Physics' started by enragedmrt, Sep 6, 2023.

  1. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    Hey all,

    I'm pretty seasoned in Unity but I have yet to try and use the physics system as a core game mechanic & in doing so I might be driving myself crazy. In this case, the mechanic is simply a bouncing ball. I've stripped back everything to the simplest example.

    In the gifs attached you'll see two balls that have the same rigidbody configuration. They are also simulated in the same scene, w/ the same floor physics material, etc. Everything is identical EXCEPT the launch trajectory. They are slightly different. Very, very, slightly different. I've also included the angular velocity magnitude & speed above the ball so as to better compare them, though I think it's likely unnecessary.

    What you'll see is that each ball has a very different bounce trajectory. One "reflects" almost perfectly the incoming trajectory. The other ends up w/ a more vertical exit trajectory, which seems quite incorrect. The only thing I could think of that would actually result in a more vertical bounce is if the ball had backspin - which it does not.

    Second to backspin, the only other difference I can see while debugging is that the sharper, "incorrect" bounce has more penetration. My guess is that more penetration results in a larger "de-penetration" force that is then added to the resulting velocity. Is that the case? If not, what is causing this sharp exit angle? If it is in fact because of more penetration, how can one expect a physical model to "feel" real if it has such unrealistic behavior in such a simple scenario?

    P.S. I totally plan on just writing my own simulation for this, as the physics engine seems unlikely to produce consistent enough results for a model that could be easily hand-built. Also, given it's a core mechanic & quite simple it makes sense to write my own kinematic code to ensure maximum control. I'm simply digging into this as an academic endeavor - I want to understand my tools thoroughly.

    "Good Bounce" - Exit trajectory near reflection of enter.


    "Bad Bounce" - Exit trajectory much more vertical.
     
  2. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    347
  3. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    Hey, a reply! Yayyyy :D.

    Yeah, I can totally try reducing the timestep. That will obviously result in smaller distances traveled per physics update and thus reduce penetration. In the end, I'll just end up writing custom kinematics for this so it's more stable. Increasing the fixed timestep isn't really tractable given performance constraints.

    Fwiw, I've tried all the collision detections schemes. This video was recorded w/ Discrete. Continuous speculative is worse & Continuous Dynamic results in the same behavior as Discrete.

    My main confusion is... is this really how this works? It's really baffling to me. It seems "trivial" to have the bounce occur "accurately" - but I imagine that's because I'm not writing a generalized physics engine. I know it's just a ball & should behave a particular way.

    In any case, I would have expected a continuous collision check to _stop_ the ball from penetration & reflect correctly....

    Thanks again for the reply!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,601
    I'm not a 3D physics dev but I'm surprised by the penetration you're showing there too. I wonder if it's related to the target collider? Is this a simple MeshCollider plane? If so, maybe you could try a large BoxCollider.
     
  5. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    I believe these recordings are from a box collider. Fwiw, I don't think the collider would affect the amount of penetration. Should just be a function of physics time step & projectile speed. My confusion is simply that I wouldn't expect the amount of penetration to change how the collision is solved. In my mind, the penetration is re-wound, the collision point calculated, and the ball reflected - but that doesn't seem to be the case. I think it might be that the friction applied varies depending on the penetration as a frictionless physics material does not produce the same issues.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,601
    A MeshCollider is far more complex so depending on the contact points calculated for it, it could affect it but as you say, it's a BoxCollider primitive so that won't be it.

    The first thing here I'd be curious about would be the initial contact points it reports to see if they are inside the "ground". Contact points are more "support points" than anything i.e. points where impulses are applied to separate collider bodies and apply restituion/friction.

    I deal with 2D physics not 3D physics so I'm not sure how PhysX would change the Torque in this situation if the contact points were not on the exterior of the colliders in question.
     
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    347
    Can you maybe try with Unity Primitives?
    I have tried myself with different angles and velocities and the bounces seem ok.

    upload_2023-9-11_9-5-16.png

    upload_2023-9-11_9-5-29.png
     
  8. riasillo

    riasillo

    Joined:
    May 23, 2023
    Posts:
    46
    For what it's worth. I've been down the bouncing ball labyrinth the past few months. the main culprit for me on angle of reflection was default contact offset. Depending on your scale, even if objects are scaled at 1 but their size is small for example a ping pong table to scale, ball is 4 cm. 0.04 in unity. sphere radius on collider is 0.02. Default contact offset is .01 See the problem. contact offset is half of your radius. That's no bueno on resolution of angle reflection. if you drop that number too low things get wonky. I went to 0.001 and it resolved reflection issues, but caused some others with raycasting. the short solution is make your balls 1 meter in diameter. The problem with increasing the size of your objects, things get very floaty with scale so then you have to crank up gravity, but then that breaks the system too if you are relying on friction or other forces to be real. bouncing ball games, like basketball, ping pong, pool, you want to keep scale as small as possible without reaching lower limits of unity physics engine. In 3D, I made a beer pong game, basketball and now a pool game. surface and ball need physics material. you can control bounce on both. physics project settings: bounce threshold velocity, drop to 0 or very small number. depenetration velocity, determine the velocity of your object, increase that number to that max velocity. use box collider on surface when you can, if must use mesh, simpler the mesh the better. even with all that, there are still things that don't follow proper physics, like balls bouncing into each other. you have to play with settings and possibly add scripts to apply opposing collision forces, or torques for spin, perpendicular forces to simulate curves etc. I have found unity's physics engine to be lacking fundamental physics that are based on very basic math and I don't understand how those things are not baked in. unless I'm missing it too. hope it helps.