title: Ball vs Ball collision Optimization
body: <p>I'm working on a project that has balls colliding, The way I detect this collision is simple. It takes an object from the array and then compares itself with all the objects in the array. How collision is detected is by checking if the center point distance is lower than the two radius's. </p>
  
  <p>This works great but when you have 100 plus objects at the same time, there is a lot of redundancy. Meaning why would a single object check for the position of an object on the other side of the screen, where the chances of it colliding are low.</p>
  
  <p>I found a theory on this that an object should check only other objects in an area larger than itself. Then if there is another object in that area, detecting collision would start. But this just creates extra checks since the object has to check all objects if it's in the area and then check if the object is colliding. </p>
  
  <p>Is there a method to efficiently detect collision? </p>
  
  <pre><code>public function newHandler():void
  {
      for ( var i:int = 0; i &lt; _objectArrayLayer1.length; i++ )
      {
          mcBall1 = _objectArrayLayer1[i];
  
              for ( var j:int = i + 1; j &lt; _objectArrayLayer1.length; j++)
              {
                  mcBall2 = _objectArrayLayer1[j];
  
                  p1 = new Point(mcBall1.nX, mcBall1.nY);
                  p2 = new Point(mcBall2.nX, mcBall2.nY);
                  distance = Point.distance(p1,p2);
  
                  radius1 = mcBall1._radius ;
                  radius2 = mcBall2._radius ;
  
                  if (distance &lt;= radius1 + radius2)
                  {
                      solveBalls( mcBall1, mcBall2 );
                  }
              }
          }
  </code></pre>