# Need help with collision detection of arrays

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!

``````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;

}
``````

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

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

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:

``````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.

``````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