2
\$\begingroup\$

I'm using cocos2d to program a game and am using box2d for collision detection. Everything in my game is a circle and for some reason I'm having a problem with some times things are not being detected as a collision when they should be.

I'm thinking of rolling up my own collision detection since I don't think it would be too hard.

Questions are:

  1. Would this approach work for collision detection between circles?
    a. get radius of circle A and circle B.
    b. get distance of the center of circle A and circle B c. if the distance is greater than or equal to the sum of circle A radius and circle B radius then we have a hit
  2. Should box2d be used for such simple collision detection? There are no physics in this game.
\$\endgroup\$

3 Answers 3

7
\$\begingroup\$

What you've suggested will work if you only care about collision detection, but there is a difference between collision detection and collision response.

For the latter, Box2D provides contact friction, and realistic (mass-based) transfer of linear inertia, and most importantly, transfer of angular/rotational inertia. These all lend to the look and feel that you may already be used to, in which case I'd advise keeping Box2d.

If you seriously need only very simple collision response mechanics that just pushes the two bodies some distance away from each other, and your game's motion is mostly kinematic (think platformers) rather than simulated physics (think racing games), then yeah, you're all good to rip out Box2d.

EDIT: Also, you may want to look at a solid Box2D example before you abandon it. Circle collisions work just fine, and you may have a discrepancy between actual body size, and rendered size. Box2D bodies will sometimes interpenetrate, but that only happens when they're being pressed together very tightly with no space to push away from each other.

\$\endgroup\$
1
  • \$\begingroup\$ I would add that collision detection can have weird results at high speeds for normal dynamic bodies. Bodies can pass through each other (tunneling) when their position changes sufficiently in a given time step. This can be avoided by setting the bullet flag of the body definition to true (at the cost of performance). \$\endgroup\$ Commented Feb 21, 2014 at 23:33
0
\$\begingroup\$
  1. Your approach for collision detection will work as you're checking collisions only between circles, you can use Pythagoras to get the distance between the points. The method you described is often used and it's called circle to circle collision detection.

  2. If you are not going to use anything else provided by box2d, I don't think you need it.

\$\endgroup\$
0
\$\begingroup\$

What you are describing is Pythagorean theorem and is very suitable to this task (however square roots can 'slow').

//in c++
bool sphereIntersection(object *A,  object *B)
{
    float dist;
    float factorX = A->position.x - B->position.x;
    float factorY = A->position.y - B->position.y;
    dist = sqrtf(factorX*factorX + factorY*factorY);

    if (dist < (A->radius + B->radius)/2)
        return true;
    return false;
}

Although box2d supports intersection only tests, with no response, through the isSensor parameter, this makes the whole library overkill. The advantage that you would get from using box2d is other algorithms that it uses (eg. sweep and prune).

If you have a lot of objects you could write your own broadphase to make the intersection tests faster

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .