# Need help with collision detection of arrays

**URL:** <https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 21, 2019, 10:50pm UTC](https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717 "2019-01-21T22:50:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![squirrel1969](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@squirrel1969](https://discourse.processing.org/u/squirrel1969)\
**Post date:** [January 21, 2019, 10:50pm UTC](https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717/1 "2019-01-21T22:50:27Z")

</div>

Hello!  
I am trying to have a game where you shoot falling balls with a laser. I used an array to make the balls fall, so when I shoot one of the multiple balls they all reset. How do I make it so that you have to shoot all the balls that fall, not just one of them?

I tried making the variable count to add on to itself when it comes in contact with a ball, and then if its equal to the number of balls the array makes (exp) to reset

This is what I have so far, my teacher also says we’re not allowed to use objects, classes, and other things like that so please let me know a way to do this without using those. Thank you!

```auto
int y ;

 int ballx=mouseX;
int bally=mouseY;

int mx = mouseX;
int my=400;

int [] num=new int [4];
int []numx=new int[4];

int rand =int(random(-2,7));

float xball=400;
float yball=400;

boolean win =false;

int exp = int(random(3,6));

float count = 0;

void setup(){
  size(600,600);

  
}

void draw(){
  xball=constrain(xball,0,600);
  yball=constrain(yball,400,600);
  
 background(255);

 
  line(0,400,600,400);

  
  if(y<400)
  y=400;
  
  ellipse(xball,yball,40,40);
    
      ellipse(xball,my,20,20);
      my-=10;

 exp = int(random(1,5));
 
    for ( int i=0; i<num.length; i++)   
  {
     fill(int(random(255)),int(random(255)),int(random(255)));
      for( int j =0; j<numx.length;j++)
      {
        
  ellipse(numx[j],num[i], 40, 40);
  
  num[i]+=rand;
       rand =int(random(1,3));

       ellipse(xball,yball, 20, 20);
      
     
  if (num[i]>=height)
  {   
    for(i=0;i<num.length; i++)
 {
          num=expand(num, exp);  
  numx=expand(num,exp); 
  num[i]=-50;
   
 }

    
 for ( j=0; j<numx.length; j++) 
 {
    
   numx[j]=int(random(height)); 
 }
  }
  
   else if(dist(xball,my,numx[j],num[i])<=30)
 {
   count+=0.2;
 //println(count);
  if(count>=exp)
  {
   win=true;
   println(count);
   count=0;
    
 }
  
  if(win==true)
  {
     for(i=0;i<num.length; i++)
 {
          num=expand(num, exp);  
  numx=expand(num,exp); 
  num[i]=-50;
   
 }
    
 for ( j=0; j<numx.length; j++) 
 {
    
   numx[j]=int(random(height)); 
 }
    
  }
  
  win=true;
 }
  
      }
  }
  
  if(keyPressed)
  {
   if (key ==CODED)
  {
    if (keyCode==LEFT)
      xball-=10;
      
    else if (keyCode == RIGHT)
     xball+=10;

    else if (keyCode ==UP)
     yball-= 10;

    else if (keyCode ==DOWN)
      yball+=10;
  }
  }
 
}

void mouseReleased(){
  
my=400;
  
}

```

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 22, 2019, 12:28am UTC](https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717/2 "2019-01-22T00:28:25Z")

</div>

Edit: I just noticed you said you can’t use classes 😐

As for your question, you should have a boolean to tell whether or not a ball has been hit. An easy solution is adding a boolean array of the same length to act as ‘markers’ for the individual balls, and not update a ball if its corresponding boolean is false, and then setting win = true when all booleans in the list are false.

You could also add these balls to an arrayList instead, so you can just remove the balls when they get hit and set win=true when the arrayList is empty

Also the random colors make the game quite jarring 😬

---

<div class="post-metadata">

**Author:** ![squirrel1969](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@squirrel1969](https://discourse.processing.org/u/squirrel1969)\
**Post date:** [January 22, 2019, 12:59am UTC](https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717/3 "2019-01-22T00:59:11Z")

</div>

Hello!  
Thank you for your help, just a quick question, how do I make the boolean array correspond with the individual balls?

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 22, 2019, 1:11am UTC](https://discourse.processing.org/t/need-help-with-collision-detection-of-arrays/7717/4 "2019-01-22T01:11:59Z")

</div>

I guess its not very obvious since its a separate array

Assuming its the same size, you can actually just use it inside of the same for loop you use to update the balls and grab/set the value with i.

For instance, **bool[0]** could correspond to **ball[0]** , so this code would work:

```auto
for(int i = 0; i < balls.length; i++){
balls[i].update
if(collision){
bool[i]=true
}
}

```

but instead of ball I think it would be numx in your case

Also I’m unsure what the purpose of this for loop is.

```auto
for ( int i=0; i<num.length; i++) {
fill(int(random(255)), int(random(255)), int(random(255)));
...
}

```

Edit: looking at your code a little further, your solution using a nested loop won’t work as you want it to. The code will update many times in one frame, which probably isn’t intended. I’d try to find a new solution for updating the balls
