Search Unity

Packed Rigidbody 2Ds Jiggling/Bouncing issue

Discussion in 'Physics' started by wfield, May 15, 2018.

  1. wfield

    wfield

    Joined:
    Jul 24, 2017
    Posts:
    4


    (Link to image if image above doesn't work)

    I'm making something that is supposed to have around 1000 circle particles moving around and attracting together to form blobs. I'm using Rigidbody2Ds with CircleColliders and a fairly simple flocking algorithm. The problem is that when they pack together, they jiggle around and bounce a lot. I think this is because each particle is trying to a calculate the cumilitive force on it from all the particles around it which are all going in slightly different directions. I have tried adjusting the friction and bounciness and setting a maximum velocity and changing the mass. All of this is to no avail. I don't know how to have them not jiggle.

    I need to figure out how to let them maintain a slow subtle motion without bouncing around like this. Any ideas?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You might try to change some parameters in Project Settings/Physics2D, for example: baumgarte.
     
    wfield likes this.
  3. wfield

    wfield

    Joined:
    Jul 24, 2017
    Posts:
    4
    Thanks. Not perfect yet but that definitely is helping. Seems like this will just involve a whole lot of time making little tweaks and testing.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,478
    When you see this kind of thing, what you should take away from it is that the physics system cannot solve the configuration it's presented with. The Rigidbody positions are based upon each individual collider being solved against other colliders individually. Those impulse forces add up and finally, after a number of iterations, a position is calculated however, the impulse isn't applied to only a single body, it's applied to the body and the other body that has the collider being contacted. This means that as a contact is solved, the solution involves moving two bodies (assuming both are dynamic bodies). By moving two bodies apart, it potentially creates an overlap by solving another overlap.

    If those colliders are all packed then in reality you'd expect them not to move however, in reality those colliders are more than likely slightly overlapped and because a pair of bodies are solved then slight pertubations are created. This is made worse if there's gravity or other forces are involved. Even worse if the body is being positioned explicitly.

    One way to solve this is to ensure that the forces involved are small and that the collisions are inelastic (you don't want them bouncing correct?). So the colliders should have zero restitution (bounce) and presumably zero friction. Also you might want to increase the velocity threshold at which the physics system decides whether to treat the collision as elastic/inelastic. You can control that in the 2D physics settings of Physics2D.velocityThreshold.

    In my opinion though, this isn't something that should be using a rigidbody simulation. If a "particle" has a simple radius then you could do this using the C# job-system, calculating distances to neighbours and ensuring separation. Indeed, you'd probably find this much faster.
     
    Last edited: May 17, 2018
    wfield and Tom-Atom like this.
  5. wfield

    wfield

    Joined:
    Jul 24, 2017
    Posts:
    4
    Thanks so much. I set the bounce and friction to zero and upped the velocityThreshold. I also made a density test per boid on an interval to toggle the flocking algorithm for boids that are tightly packed. I think this was a huge thing that I was overlooking that you made me consider - I was still applying forces from the flocking algorithm to densely packed boids but I actually don't need to because when tightly packed, the flocking motion is irrelevant anyway and outter boids prevent the system from becoming completely static so it works. As you say, even though the flocking algorithm isn't applying large forces per boid, the cumulative effect is very large.

    Also, just looked through your git repo for job based physics. May finally convince my boss to invest in Unity Plus! (I'm on 5.6 now).
     
    MelvMay likes this.