title: Collision detection using QuadTree in AS3
body: <p>Ok. I managed to make and use QuadTree for my collision Detection alghorithm and it works just fine. I have my enemies and put them in the QuadTree, than retrieve the candidates that could possibly collide with my hero. That is hitTestObject() many agains one.</p>
  
  <p>The problem I reached is how to test fastly whether some of the enmies collide with my hero's bullets. Roughly I have 4-6 bullets on the stage at same time. In this case I have hitTstObject 4-6 bullets against many enemy objects, which in turns gives me for cycle in for cycle so even using quad tree after a while things start to lag on the stage :)</p>
  
  <p>I used this tutorial <a href="http://gamedev.tutsplus.com/tutorials/implementation/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space/" rel="nofollow">quadtree in java</a> to develop m alghorithm but it works fine only in the above case. What should I do in this circumstance? Use another algorithm for many agains many or ?</p>
  
  <p>Roughly this is the code</p>
  
  <pre><code> bulletsQuadTree.clear();
   for (var bIndex:uint; bIndex &lt; allEnemies.length; bIndex += 1 )        
   {
    bulletsQuadTree.insert(allEnemies[bIndex]);
   }
  
  for (var bc:uint = 0; bc &lt; bullets.length; bc += 1 )
  {
      var enemiesCollideBullets:Array = new Array();
      bulletsQuadTree.retrieve(enemiesCollideBullets, bullets[bc]);
  
      for (var dc:uint = 0; dc &lt; enemiesCollideBullets.length; dc += 1 )
      {
          if (enemiesCollideBullets[dc].hitTestObject(bullets[bc]))
          {
              enemiesCollideBullets[dc].destroy();
              enemiesCollideBullets.splice(dc, 1);
          }
      }
  }
  </code></pre>
  
  <p>So this happens on each frame which are many operations per frame :(</p>
  
  <p>Each bullet is treated as a hero and an array of enemies is returned for each bullet that could possibly collide with. </p>