# Optimizing collision detection

**URL:** <https://forum.kirupa.com/t/optimizing-collision-detection/203095>\
**Category:** Uncategorized\
**Created:** [October 7, 2006, 2:35pm UTC](https://forum.kirupa.com/t/optimizing-collision-detection/203095 "2006-10-07T14:35:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sigfa](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sigfa](https://forum.kirupa.com/u/Sigfa)\
**Post date:** [October 7, 2006, 2:35pm UTC](https://forum.kirupa.com/t/optimizing-collision-detection/203095/1 "2006-10-07T14:35:17Z")

</div>

This is really start to eat me. So, I`m making an arcade/shooter game, inspired by Asterax, if anyone still remembers the old Mac game. The game is level based and you have to shoot asteroids with your space ship. every next level increases the asteroid count by one, starting at 3, so at level 10 you would have 13 asteroids. Now, not minding the graphics rendering lag, i really feel like my code should be optimized, as at level 15 the cpu seems to be at full usage.

As it is now, I have a for… in loop on every shot and the ship, that checks the distance to every single asteroid on the field, every frame. I know, very bad programming. What I had in mind was putting a single loop on \_root to check all objects for collision (checking the distance seems to be easier on the cpu then the flash`s hitTest function), that would probably work better, but at level 30 it would prolly` be the same.

Is there any other way?  
Thanks in advance, Sigfa.

---

<div class="post-metadata">

**Author:** ![Sigfa](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sigfa](https://forum.kirupa.com/u/Sigfa)\
**Post date:** [October 23, 2006, 2:12pm UTC](https://forum.kirupa.com/t/optimizing-collision-detection/203095/2 "2006-10-23T14:12:26Z")

</div>

yea, testing the x distance first seems reasonable.

of course, all the asteroids are on an array, and when one is destroyed it gets spliced, otherwise the for …in loop would overload the cpu.

I`ll think about that x distance first, although I don`t think it will make much of an improvement.

---

<div class="post-metadata">

**Author:** ![Thinker2501](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@Thinker2501](https://forum.kirupa.com/u/Thinker2501)\
**Post date:** [October 24, 2006, 11:44pm UTC](https://forum.kirupa.com/t/optimizing-collision-detection/203095/3 "2006-10-24T23:44:40Z")

</div>

When checking from the array, you start at the end of the array and work your way to the front. So you start at the end of the array, get x dist, hit check if needed. then if there is contact pop the element and delete the object. Then on the next cycle of the for loop you subtract on from the index you are checking and you loop will be unaffected by the removal of the element on the previous cycle.

---

<div class="post-metadata">

**Author:** ![Sigfa](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@Sigfa](https://forum.kirupa.com/u/Sigfa)\
**Post date:** [October 25, 2006, 8:45am UTC](https://forum.kirupa.com/t/optimizing-collision-detection/203095/4 "2006-10-25T08:45:01Z")

</div>

It is highly unlikely, that four or more objects collide in the same frame, so your idea won`t be much of an improvement, but I`ll add it just for the heck of it. only one line of code anyway.
