# Question About Multiple Collisions At Once (Kinda)

**URL:** <https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461>\
**Category:** Coding Questions\
**Created:** [July 7, 2020, 8:37pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461 "2020-07-07T20:37:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)\
**Post date:** [July 7, 2020, 8:37pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/1 "2020-07-07T20:37:13Z")

</div>

So, I’m in the process of making a tower defense game. Currently the way it works is that when an enemy touches the collider of the tower, the tower locks on to that enemy and no others until that enemy is out of range or is dead. However, this system seems to prioritize enemies that were created first rather than enemies that are farther up on the track. Is there a way to compare not two, but multiple variables (these would store info on how far the enemy has travelled) so the tower could choose the enemy that has travelled the furthest instead of the enemy that was drawn the first?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 8, 2020, 2:50am UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/2 "2020-07-08T02:50:54Z")

</div>

Yes, your case as you explained indicates it is possible. To move forward in this discussion, you need to provide some minimum code that reproduces your observation and shows your approach.

Kf

---

<div class="post-metadata">

**Author:** ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)\
**Post date:** [July 8, 2020, 4:04am UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/3 "2020-07-08T04:04:14Z")

</div>

Okay, it’s not the cleanest or most efficient of code but this is the current code for targeting enemies:

towers[5] stores a boolean for if an enemy has been identified.  
towers[6] stores the enemy sprite  
towers[7] stores the “range” sprite that displays the range of the tower and is the collider for if an enemy is within range  
towers[8] stores the rest of the properties of the enemy sprite

Again, I know it isn’t the best of code, but I hope you can help.

```auto
  for(var x = 0; x < towers.length; x++){
    for(var y = 0; y < enemies.length; y++){

      if(towers[x][7].isTouching(enemies[y][0]) && towers[x][5] === false){
        towers[x][5] = true;
        towers[x][6] = enemies[y][0];
        towers[x][8] = enemies[y];
      }
      if(towers[x][5] === true){

        towers[x][0].rotation = atan2(towers[x][6].x-towers[x][0].x, towers[x][6].y-towers[x][0].y) * -1;   

        if(towers[x][7].isTouching(towers[x][6]) === false){
          towers[x][5] = false;
          towers[x][6] = null;
        }
      }
    }
  }

```

If there was a way for be to compare multiple numbers at once, and find which one is the highest, that would greatly help.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 8, 2020, 5:10am UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/4 "2020-07-08T05:10:13Z")

</div>

Wouldn’t passing your array through a sorting function solve this issue. Sorting by distance to wall.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 8, 2020, 3:49pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/5 "2020-07-08T15:49:18Z")

</div>

You would need to either sort it as described by paulgoux or loop through the data twice. The first one is to determine and select based on range and the second one will do the current task based on these results from the first loop.

Kf

---

<div class="post-metadata">

**Author:** ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)\
**Post date:** [July 8, 2020, 6:50pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/6 "2020-07-08T18:50:31Z")

</div>

Huh, I didn’t even know a sorting function existed in javascript until you mentioned it. Thanks!

---

<div class="post-metadata">

**Author:** ![The99thTroll](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@The99thTroll](https://discourse.processing.org/u/The99thTroll)\
**Post date:** [July 8, 2020, 7:07pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/7 "2020-07-08T19:07:34Z")

</div>

Errrr, I’m trying what paulgoux suggested and to do so I’ve created an array called distances, but when I try to push my distance I get a type error. Uh, what am I doing wrong?

```auto
function createNormalEnemy(){
  var normalEnemy = createSprite(-50 , 100, 25, 25);
  normalEnemy.addImage("normal enemy", normalEnemy_img);
  normalEnemy.scale = 0.15;
  distance = 0;
  distances.push(distance);
  enemies.push([normalEnemy, 4, 20, false, false, "normal", distance]);
}

```

EDIT: Okay, that appears not to be the main issue. Using sort completely breaks my targeting code. My towers don’t lock onto any enemies any more. Current code below

```auto
      distances.sort(function(a, b){return b-a});

      if(towers[x][7].isTouching(enemies[y][0]) && towers[x][5] === false && enemies[y][7] === distances[0]){
        towers[x][5] = true;
        towers[x][6] = enemies[y][0];
        towers[x][8] = enemies[y];
      }
      

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 8, 2020, 9:20pm UTC](https://discourse.processing.org/t/question-about-multiple-collisions-at-once-kinda/22461/8 "2020-07-08T21:20:15Z")

</div>

You might might need to make a copy of the array first, then the targeting system will use the original data.
