# A catching game

**URL:** <https://discourse.processing.org/t/a-catching-game/16906>\
**Category:** Libraries\
**Created:** [January 6, 2020, 4:24pm UTC](https://discourse.processing.org/t/a-catching-game/16906 "2020-01-06T16:24:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morgy\_B](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Morgy\_B](https://discourse.processing.org/u/Morgy_B)\
**Post date:** [January 6, 2020, 4:24pm UTC](https://discourse.processing.org/t/a-catching-game/16906/1 "2020-01-06T16:24:51Z")

</div>

Hi I am making a ball catching game using a controller and I am having trouble with collision detection. I want it so when the ball comes into contact with the bucket at the bottom the ball will disappear and the score will go up. i am yet to do the score part but wanted to focus on the ball collision first. Can anyone help?

```auto
import org.gamecontrolplus.gui.;
import org.gamecontrolplus.;
import net.java.games.input.*;

ControlIO control;
ControlDevice stick;
Configuration config;
ControlDevice gpad;

int radius =30, dX = 1, dY = 0;
float x=400, y=750, speed=4;

PImage img;

float px, py;
boolean trailOn;
ArrayList shadows = new ArrayList();
ArrayList trail = new ArrayList();

Drop [] drops = new Drop[5];
public void setup() {
size(1000, 700);
for (int i = 0; i < drops.length; i++){
drops[i] = new Drop();
}

img = loadImage(“Gun.png”);

// Initialise the ControlIO
control = ControlIO.getInstance(this);
// Find a device that matches the configuration file
stick = control.getMatchedDevice(“joystick”);
if (stick == null) {
println(“No suitable device configured”);
System.exit(-1); // End the program NOW!
}
// Setup a function to trap events for this button
stick.getButton(“SHADOW”).plug(this, “dropShadow”, ControlIO.ON_RELEASE);
}

// Poll for user input called from the draw() method.
public void getUserInput() {
px = map(stick.getSlider(“X”).getValue(), -1, 1, 0, width);
trailOn = stick.getButton(“TRAIL”).pressed();
}

// Event handler for the SHADOW button
public void dropShadow() {
// Make sure we have the latest position
getUserInput();
shadows.add(new PVector(px, py, 40));
}

public void draw() {
getUserInput(); // Polling
background(255, 255, 240);

for (int i = 0; i < drops.length; i++){
drops[i].show();
drops[i].fall();

// Draw shadows
for (PVector shadow : shadows)
ellipse(shadow.x, shadow.y, shadow.z, shadow.z);
if (trailOn)
trail.add(new PVector());
else
trail.clear();
if (trail.size() > 1) {
stroke(132, 0, 0);
for (int n = 1; n < trail.size(); n++) {
PVector v0 = trail.get(n-1);
PVector v1 = trail.get(n);
line(v0.x, v0.y, v1.x, v1.y);
v0 = v1;
}

}

// Show position

noStroke();
fill(255, 64, 64, 64);
imageMode(CENTER);
image(img,px,700, 300,300);

}

}

class Drop {
float x = random(width);
float y = random(-200,-100);
float yspeed = random (4, 10);
void fall(){
y = y + yspeed;

if(y > height) {
y = random(-200,-100);
}
}
void show (){
stroke(138,43, 226);
ellipse(x,y,24,24);
}

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 6, 2020, 4:35pm UTC](https://discourse.processing.org/t/a-catching-game/16906/2 "2020-01-06T16:35:10Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

---

<div class="post-metadata">

**Author:** ![Morgy\_B](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Morgy\_B](https://discourse.processing.org/u/Morgy_B)\
**Post date:** [January 8, 2020, 6:20pm UTC](https://discourse.processing.org/t/a-catching-game/16906/3 "2020-01-08T18:20:44Z")

</div>

i have done this please can you help me i am really struggling to get the code right

---

<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:** [January 8, 2020, 7:11pm UTC](https://discourse.processing.org/t/a-catching-game/16906/4 "2020-01-08T19:11:46Z")

</div>

Check if the x of the ball \> left side of bucket and \< right side of the bucket (\< x+bucketSize)

same for y (top and lower side)
