# Collision detection ball catching game

**URL:** <https://discourse.processing.org/t/collision-detection-ball-catching-game/25200>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 5, 2020, 1:03pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200 "2020-11-05T13:03:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 1:03pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/1 "2020-11-05T13:03:40Z")

</div>

**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!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 2:19pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/2 "2020-11-05T14:19:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 2:29pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/3 "2020-11-05T14:29:27Z")

</div>

a bit of house cleaning:

since you have a player class, you don’t need these before setup()

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

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 2:37pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/4 "2020-11-05T14:37:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 2:57pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/5 "2020-11-05T14:57:34Z")

</div>

> [@Chrisir](#):
>
> if (dist(mouseX, mouseY, width/2, height/2) \< circleRadius + headRadius) {

please correct this line as I stated above

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 3:08pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/6 "2020-11-05T15:08:42Z")

</div>

I’m not sure what i should change

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 3:09pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/7 "2020-11-05T15:09:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 3:56pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/8 "2020-11-05T15:56:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 4:13pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/9 "2020-11-05T16:13:16Z")

</div>

> [@FabsFabs](#):
>
> circleX

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…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 4:14pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/10 "2020-11-05T16:14:54Z")

</div>

> [@FabsFabs](#):
>
> ].circleYy

small typo here it seems…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 4:15pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/11 "2020-11-05T16:15:37Z")

</div>

> [@FabsFabs](#):
>
> circleY[1].

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

> [@Chrisir](#):
>
> \< circleRadius + headRadius

this value also seems **too big**

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

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 6:36pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/13 "2020-11-05T18:36:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 6:43pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/14 "2020-11-05T18:43:45Z")

</div>

> [@FabsFabs](#):
>
> `circleY[i]++;`

look at this line for that

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 5, 2020, 7:00pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/15 "2020-11-05T19:00:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2020, 7:06pm UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/16 "2020-11-05T19:06:02Z")

</div>

> [@FabsFabs](#):
>
> i already changed that line but it was in the wrong place.

So this is is solved?

> [@FabsFabs](#):
>
> how i can make the game stop if all the balls have disappeared

at the moment we respawn the ball with

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

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

```

Chrisir

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 6, 2020, 12:42am UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/17 "2020-11-06T00:42:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FabsFabs](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@FabsFabs](https://discourse.processing.org/u/FabsFabs)\
**Post date:** [November 6, 2020, 1:01am UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/18 "2020-11-06T01:01:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 6, 2020, 6:31am UTC](https://discourse.processing.org/t/collision-detection-ball-catching-game/25200/19 "2020-11-06T06:31:28Z")

</div>

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
