Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Unity bounce synchronously

Discussion in 'Physics' started by petit_morceau_de_ciel, Oct 13, 2022.

  1. petit_morceau_de_ciel

    petit_morceau_de_ciel

    Joined:
    Jan 24, 2021
    Posts:
    8
    I'm trying to get some balls to bounce rhythmically. I have 10 balls, each with speed v, v * 1, v * 2 ... to v * 10 bouncing off some walls. In theory they should bounce in half the time each if they have double speed. At first it works fine. But after a while it gets desynchronized. And they no longer bounce at the "mathematically correct" intervals. I have tried with the 2d physics engine (0 friction, 1 bounce) and with kinematic bodies doing the rebounds in code with OnCollisionEnter2D, but the same happens in both cases, they get out of sync. I suppose this is because the calculations of the physics engine are not exact and after a while the synchrony is lost. Is there any way to make the balls bounce synchronously ]1, each in exactly half the time of the other if they have twice his speed? Now I'm testing with bounces without angles, from top to bottom to top... , but it should work also with bounces with angles.
     
    Last edited: Oct 13, 2022
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,913
    All existing physics engines use numerical integration to evolve body state over time (which usually means integrating velocity to get displacement), so a small amount of error is introduced with each physics step, see: https://www.haroldserrano.com/blog/algorithms-in-game-engine-development

    While this error can be made arbitrarily small by reducing the fixed timestep size, it's not possible to get rid of it completely: you can't get absolutely perfect results in a simulation, they will always drift away from the expected result over time. How fast this happens depends on your timestep size and the quality of the integrator used (symplectic Euler and RK4 are among the most widely used ones, I'd bet that PhysX and Box2D both use either of these).
     
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,409
    As above. In the end, it's an approximation for speed not scientific accuracy but there also may be things you're doing that make this far worse than it needs to be although if you want it to stay perfectly in-sync forever then you need to try a different approach such doing a basic Verlet sim or some simple nemeric animation.

    You can improve it by spending more CPU time. Ensure the Rigidbody2D use continuous collision detection and not discrete. Also turn-off inelastic contacts by reducing Physics2D.VelocityThreshold to its minimum.
     
  4. petit_morceau_de_ciel

    petit_morceau_de_ciel

    Joined:
    Jan 24, 2021
    Posts:
    8
    Thanks for the answers.
    I already assumed it would be like that.
    I have done the tweaks you suggest, and some others, but not much improvement in synchronization is achieved.