1

Using cocos2d and box2d , i have a few bodies that i need them to be attracted to each other, and a few others to reject one another. This means that when they are close the attract like magnet, or reject and pushed away.

Do i have to program it in the hard way (checking distance between them than applying forces when they are close) , or there is a simpler way for that ?

thanks a lot .

Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

1

Do I have to program it in the hard way?

Yep. There is nothing built into Box2d (or cocos2d) currently to do this.


Edit

Pertaining to your comment:

do you have an idea how to even start checking for each body all his close bodies and distances between all ?

It'll depend on how many bodies you have as to which technique you'll want to use. If you have a LOT of bodies, you might want to look at quad-trees to divide up your space and quickly ignore bodies that aren't close.

If you don't have that many, you can iterate your bodies in O(n^2) time with a naïve, but comparatively simple double for loop.

NB: This is by no means a complete solution, you should consider it pseudo code as it won't be compilable.

for (b2Body *b in myBodies)
{
    for (b2Body *b2 in myBodies)
    {
        if (b == b2) continue;
        float distance = b2Distance(b->GetWorldCenter(), b2->GetWorldCenter());
        if (shouldAttract)
        {
            float angle1 = b2Cross(b->GetWorldCenter(), b2->GetWorldCenter());
            float angle2 = b2Cross(b2->GetWorldCenter(), b->GetWorldCenter());
            b->ApplyForce(distance * angle1); 
            b2->ApplyForce(distance * angle2); 
        }
        /* else if (shouldRepel) */
    }
} 
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • do you have an idea how to even start checking for each body all his close bodies and distances between all ? – Curnelious Dec 17 '13 at 14:33
  • thanks a lot. thats what i was thinking, i don't have too many, less than 15. i was thinking if there is a way to use the contact listener that i already have , to check not only when bodies touch, but when they are close. is there a way to add that ? – Curnelious Dec 17 '13 at 14:59
  • your answer is great . btw did you mean by continue to break out right? because when they are equals its the same body so i have to go to the next.. – Curnelious Dec 17 '13 at 15:02
  • 1
    If I remember correctly bodies count as "colliding" but not "touching" when their bounding boxes intersect. I'll see if I can find the documentation. – James Webster Dec 17 '13 at 15:03
  • Here is the [documentation](http://www.iforce2d.net/b2dtut/collision-anatomy) I was referring to on collisions. – James Webster Dec 17 '13 at 15:04
  • `Did you mean by continue to break out right?`.. It doesn't break, it continues. That means `if b1 == b2, skip to the next b2` – James Webster Dec 17 '13 at 15:06
  • Now problem is that they don't stick to each other like real magnets, but i will solve that :) – Curnelious Dec 17 '13 at 15:27
  • i somehow get always the same very small values for distance- they are 3/5/7.. even when distance is much higher. why is that ? – Curnelious Dec 17 '13 at 16:06
  • It did, i found them , but the main thing is that you helped me a lot. so thanks ! – Curnelious Dec 17 '13 at 19:28