Thank you for the reply. I should have included my code that shows where I call the collision function.

I loop through all balls and check each against all the others whilst ensuring that I do not compare the same one with itself


for (var i = 0; i < howManyBalls; i++) {

    ball1 = ballObjects[i]

    for (var j = 0; i < howManyBalls; j++) {

          ball2 = ballObjects[j]

          if (ball1 != ball2) {
                checkForCollisionWithBall(ball1, ball2 )




Is this equivalent to the double loop?

You have the following code

dt = (r1+r2-d)/(vp1-vp2);

What if vp1==vp2???

You also have the following code


CGFloat d=sqrt(dx*dx+dy*dy);
//First calculate the component of velocity in the direction of (dx,dy)
CGFloat vp1= speed.x*dx/d+speed.y*dy/d;
CGFloat vp2= collideeSpeed.x*dx/d+collideeSpeed.y*dy/d;

What if d==0???

It could also be other kind of bug in your code. You need to trace the code (find out value for variables) and try to find out the problem.
It is not easy to find the bug.  Good luck!


How do I deal with the situation where vp1 = vp2 or d = 0?

Thank you