A catching game

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?

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);
}

}

please format your code posting by pasting it into the

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


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

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)

1 Like