I'm using box2d (the c++ one).

I found a method for an arriving behavior (on http://www.red3d.com/cwr/steer/gdc99/ ), which works well, problem is, the bodies are bumping into each other and it results in an ugly shaking effect.

How do you make sure bodies keep away from each other after I applied the arriving behavior ? Or the other way around ?

I'm wondering if it might be a better idea to completely remove the arriving behavior to implement the separate algorithm.

edit: even setting a low restitution results in shaking

  • \$\begingroup\$ done ! yes they're individuals \$\endgroup\$ – jokoon May 8 '13 at 11:26
  • \$\begingroup\$ Have you considered just making them repel each other, like with a magnetic field? (Similar things are discussed under "separation" on the page you linked.) \$\endgroup\$ – Anko May 8 '13 at 11:30
  • \$\begingroup\$ good idea, but no, but I wonder how I can do that with box2d, it would require to make queries for bodies around each one, aren't queries expensive ? \$\endgroup\$ – jokoon May 8 '13 at 14:43

Each body finds the bodies closest to it. It then attempts to steer away from the sum of the neighbors. The closer a neighbor is, the more it adds to the repel force.

enter image description here

If each body does this, they will spread out away from each other. This combined with a cohesion algorithm keeps the bodies grouped, but not on top of each other.

enter image description here

Both of these are covered in the link you shared.

| improve this answer | |
  • \$\begingroup\$ question is, how do you find the "closest" bodies ? I guess it requires a query of some sort. \$\endgroup\$ – jokoon May 8 '13 at 14:46
  • \$\begingroup\$ If you don't have too many bodies, looping through all of them measuring the distance should work perfectly. If you're worried about performance, I don't think measuring distances is that performance intense. \$\endgroup\$ – Andre May 8 '13 at 14:51
  • 1
    \$\begingroup\$ There are questions on finding the nearest and questions about optimizing it. Use one of the methods mentioned in those answers to organize pointers to your bodies. Then you can quickly find the nearest bodies when doing separation and cohesion. \$\endgroup\$ – MichaelHouse May 8 '13 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.