Collision detection ball catching game

Main code
float playerWidth;
float playerX;
float playerY;
float number;
float[] circleY = new float[10];
float circleRadius = 25;
float headRadius = 15;
Player player;

//=============================================================
void setup() {
size( 600, 600 );
player = new Player (width / 2, height / 2, 100);

for (int i = 0; i < circleY.length; i++) {
circleY[i] = random(height);
}
}

//=============================================================
void draw() {

background(#0062AA);
player.display();
player.move();

fill(#81580D);
for (int i = 0; i < circleY.length; i++) {
float circleX = width * i / circleY.length;
ellipse(circleX, circleY[i], 50, 50);

circleY[i]++;

if (dist(mouseX, mouseY, width/2, height/2) < circleRadius + headRadius) {
  circleY[i] = 0;
  
  }
}

}

Player class
class Player {
int x;
int y;

int dx = (int) random( -3, 3);
int dy = (int) random( -3, 3);
Player( int newX, int newY, int newRadius) {
x = newX;
y = newY;
}

void display() {
//helmet
fill(#81F1FF);
ellipse (mouseX, mouseY, 40, 40);

//head
fill(#EAD2A3);
ellipse (mouseX, mouseY, 30, 30);

//eyes
fill(#FFFEFC);
ellipse (mouseX-7, mouseY, 15, 15);
ellipse (mouseX+2, mouseY, 15, 15);

//pupils
fill(#120C00);
ellipse (mouseX - 9, mouseY, 5, 5);
ellipse (mouseX + 2, mouseY, 5, 5);

//eyebrows
fill(#EAD2A3);
arc(mouseX - 7, mouseY - 9, 20, 10, HALF_PI, PI);
arc(mouseX + 3, mouseY - 9, 20, 10, HALF_PI, PI);

//jetpack
fill(#22727C);
stroke(10);
rect(mouseX - 23, mouseY + 25, 10, 20);
ellipse(mouseX - 23, mouseY + 20, 20, 20);
ellipse(mouseX - 23, mouseY + 20, 10, 10);
ellipse(mouseX - 23, mouseY + 20, 1, 1);
line(mouseX - 23, mouseY + 10, mouseX - 23, mouseY -25); 

fill(#FF0004);
 ellipse(mouseX - 23, mouseY - 25, 10, 10);

//fire 
fill(#F59607);
triangle(mouseX - 23, mouseY + 45, mouseX - 13, mouseY + 45, mouseX - 23, mouseY + 80);

fill(#FEFF1A);
triangle(mouseX - 23, mouseY + 45, mouseX - 13, mouseY + 45, mouseX - 23, mouseY + 60);

//torso
fill(#22727C);
stroke(30);
strokeWeight(2);
rect(mouseX - 11, mouseY +10, 20, 40);
rect(mouseX - 11, mouseY +10, 20, 30);
rect(mouseX - 11, mouseY +10, 20, 20);
rect(mouseX - 11, mouseY +10, 20, 10);

}

void move() {
x += dx;
y += dy;
}
}

I want the player to ā€˜catchā€™ the balls. The balls should respawn at the top of the screen when they collide with the players head.
I would really appreciate your help because I am awful at processing. Thanks in advance!

1 Like

Hello and welcome to the forum!

Great to have you here!

This line is wrong:
if (dist(mouseX, mouseY, width/2, height/2) < circleRadius + headRadius) {

please reconsider which distance you need to check, Itā€™s from the player to the current ball, right?

Remark

I recommend the usage of noCursor(); in setup(), looks better without the mouse cursor

Remark

This (when restarting a ball)

circleY[i] = 0;

can be circleY[i] = random(-50, -20);

Warm regards,

Chrisir

a bit of house cleaning:

since you have a player class, you donā€™t need these before setup()

//float playerWidth;
//float playerX;
//float playerY;

same for headRadius (in the line with dist() you want < circleRadius + player.headRadius) { )

(you donā€™t use headRadius in the class and in the constructor you donā€™t use newRadius)

Remark

float number; is not in use

Remark

float circleRadius = 25; : you can move this into draw() since it is only used there.

Chrisir

Yes itā€™s from the player head to the ball that he collects. I also have the problem that right now they all respawn instead of just the ball he collides with.

please correct this line as I stated above

Iā€™m not sure what i should change

change this: dist(mouseX, mouseY, width/2, height/2)

here is an error.

You want to check the player against the current ball.

What is the current ball position?

I changed it now to this
if (dist(circleY[1].circleX, circleY[1].circleYy, mouseX,mouseY) < ellipseRadius + circleRadius) {
but now it says the global variable ā€œcircleXā€ does not exist.

you defined it on the fly in the for-loop, itā€™s not part of the class

so no circleY[1]. before it!

You are almost thereā€¦

small typo here it seemsā€¦

not sure why you put 1 hereā€¦ I mean we are in a for-loopā€¦

this value also seems too big

somehow he destroys the balloon when he is not even near it

do you maybe know how i can make the balls float down faster?

look at this line for that

thanks so much i already changed that line but it was in the wrong place. would you happen to know how i can make the game stop if all the balls have dissapeared?

So this is is solved?

at the moment we respawn the ball with

circleY[i] = 0;

delete the line. To let the balls disappear

Then make a counter to count the balls that have disappeared

When the counter is == length of array we want to STOP.

You can have a boolean variable stop which is initially == false;
in the said event set it to true.

In draw() say:

if (stop) {
    // new screen with text you lost or so
} else {
    what you have in draw now
}

Chrisir

this is the last thing i need, do you know how i can make the balls move down progressively faster? Like they get faster each time i collide with them

right now to determine the spead i use
circleY[x] = circleY[x] + 5;

That would be to replace the 5 with a variable addValue and increase it slowly

You want to use float maybe so you can add small values like 0.2