Collision detection Ball

import static javax.swing.JOptionPane.showMessageDialog;
import java.util.Random;
Random rand = new Random();

float x = 450;
float Ball = 0;
int RandomSpawnWidth;
float speedBall = 3;
int score = 0;
float speedPlayer = 10;
PFont Font;
float BallX = 25;
float BallY = 25;

void setup()
{

size(800, 600);
RandomSpawnWidth = rand.nextInt(600);
Font = createFont(“arial.ttf”, 24);
textFont(Font);
}

Background background = new Background(); //first class
Grass grass = new Grass(); // second class

void draw()
{
float ellipseRadius = 25;
float circleRadius = 25;
background.display();
if (keyPressed) {
if (key == ‘a’ || key == ‘A’) {
x = (x-speedPlayer);
}
}
if (keyPressed) {
if (key == ‘d’ || key == ‘D’) {
x = (x+speedPlayer);
}
}
if (x >= 800) {
x = 800;
}

if (x <= 80) {
x = 80;
}
fill (#00F727);
ellipse (400, 700, 900, 500); // landscape

fill (#674700);
rect(400, 400, 20, 50); // middle tree stump

fill (#018E02);
ellipse (410, 400, 50, 50); // middle tree leafs

fill (#674700);
rect(500, 450, 21, 51); // right tree stump

fill (#018E02);
ellipse (510, 445, 50, 50); // right tree leafs
fill (#674700);
rect(50, 500, 150, 400); // left tree stump

fill (#018E02);
ellipse (110, 350, 350, 350); // left tree leafs
fill (#E52525);
rect(x-60, 545, 30, 50); //body

fill (#DEACAC);

ellipse(x-45, 540, 50, 50); // head

fill (#000000);
rect(x-60, 532, 13, 10); //left eye sunglasses
rect(x-50, 532, 11, 3); //middle piece sunglasses
rect(x-42, 532, 13, 10); //right eye sunglasses

ellipse(x-45, 551, 10, 5); //mouth
ellipse(x-55, 595, 13, 5); //left foot
ellipse(x-35, 595, 13, 5); //right foot

fill (#FEFF0D);
ellipse(10, -5, 250, 250); //sun
if (mousePressed) {
fill (#E0E0E0);
ellipse(10, -5, 250, 250); //moon
}
grass.display();
fill(#FFFFFF);
text("Score: " + score, 693, 33);

Ball = Ball + speedBall;
if (Ball > 600) {
showMessageDialog(null, “Close One!”);
System.exit(0);
}

if (Ball < 600);
{
fill (#FF780F);
ellipse(RandomSpawnWidth, 0+Ball, 50, 50);

if (dist(BallX, BallY, 50 ,50) < ellipseRadius + circleRadius) {
BallY = 0;

    speedBall = speedBall + 0.5; 
    Ball = Ball + speedBall; 
    score++; 
    speedPlayer = speedPlayer + 0.25; 
  }
}

}
Background class
class Background {

void display() {

int[] colors = {123, 151, 212, 201, 52, 102, 190, 87, 145, 78, 220, 40, 15, 129, 248, 123, 124, 125, 20, 52, 62, 73, 81, 21, 215};
background (rand.nextInt(colors[20]));

}
}
Grass class
class Grass {

void display(){
for (int x = 0; x <= 805; x = x+10) {
fill(#1CFF34);
triangle( x-15, 620, x+15, 620, x, 570 ); //I used the grass of the practice examn as an example for my grass.
}
}
}
Hey! I’m making a catching ball game with collison detection. But the collision detection won’t work. Can somebody help me ASAP. Thanks in advance! Hope someone can help me quick!

I assume this is your issue

why not

if (dist(BallX, BallY, x,y) < ellipseRadius + circleRadius) {

when x,y is your player?

Hey and welcome to the forum!

Warm regards,

Chrisir

Hi thank you! But if I fill in the x and the y it gives an error that y hasn’t got a variable if I make a y-variable of zero, it works but the ball is still not colliding with the character. Do you know how to fix that? It does the same thing when I change the x and the y of every characters part.

Kind Regards,

Mitch

you don’t use BallY, only Ball

if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius+44) {

Okay so I did what you said and I removed the y variable so I filled 590 instead of y (and ofc the circleRadius+44). But unfortunately it still doesn’t collide. WAIT update: it works now! Only problem is that I need the ball perfectly alligned with both middles

1 Like

I added 44 here also

Yes I know but I seems that only catching with centre works, another problem is that the ball spawns at the same X point if you do manage to catch it. Still thanks tho! Taking little steps till it works

Okay another update, it now spawns at different positions if you manage to catch it, but the hitbox is still a little weird, maybe you know how to increase the margins of where the 2 “ellipses” collide

Now I added
float TopSideBall = Ball - 25;
float BottomSideBall = Ball + 25;
float LeftSideHead = x - 50;
float RightSideHead = x + 50;
but it still doesn’t increase the hitbox margin

other than dist you can test if ballX > left side player && ballY < right side player

you can change BallX : BallX = random(20,width-20);

if ballX right side > left side player && ballY left side < right side player

And do I need the make variables ballX right side, left side player, ballY left side and right side player? Sorry I don’t get it

No, you have ballX and ball already

just check either with dist()

OR with if(ballX> x -30 && ballX < x+30....

Okay now I have this: if (BallX > x - 30 && BallX < x+30);

    BallX = random(20, width-20);

But it still doesn’t work :frowning:

if(ballX> x -30 && ballX < x+30....

I had some … at the end, to show you that you have to write more, you have to check Ball value too

anyway, post your entire code then

not a clear description…

Hmmm, the ball value is 0 right because I made it float Ball = 0; in the beginning. But what do I need to write more? What I had was that the ball and the head of the character didn’t collide. It worked with the if(dis) but then you had need to collide the ball and the head perfectly centered otherwise it won’t register it as that they collided.

that’s true. Then you can increase the value by +44 or whatever:

if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius** + 44 ** ) {

post your entire code as I said

Okay, now I made it again with if dis btw.

import static javax.swing.JOptionPane.showMessageDialog;
import java.util.Random;
Random rand = new Random();

float x = 450;
float Ball = 0;
int RandomSpawnWidth;
float speedBall = 3;
int score = 0;
float speedPlayer = 10;
PFont Font;
float BallX = 25;
float BallY = 25;

void setup()
{

size(800, 600);
RandomSpawnWidth = rand.nextInt(600);
Font = createFont(“arial.ttf”, 24);
textFont(Font);
}

Background background = new Background(); //first class
Grass grass = new Grass(); // second class

void draw()
{
float ellipseRadius = 25;
float circleRadius = 25;
background.display();
if (keyPressed) {
if (key == ‘a’ || key == ‘A’) {
x = (x-speedPlayer);
}
}
if (keyPressed) {
if (key == ‘d’ || key == ‘D’) {
x = (x+speedPlayer);
}
}
if (x >= 800) {
x = 800;
}

if (x <= 80) {
x = 80;
}
fill (#00F727);
ellipse (400, 700, 900, 500); // landscape

fill (#674700);
rect(400, 400, 20, 50); // middle tree stump

fill (#018E02);
ellipse (410, 400, 50, 50); // middle tree leafs

fill (#674700);
rect(500, 450, 21, 51); // right tree stump

fill (#018E02);
ellipse (510, 445, 50, 50); // right tree leafs
fill (#674700);
rect(50, 500, 150, 400); // left tree stump

fill (#018E02);
ellipse (110, 350, 350, 350); // left tree leafs
fill (#E52525);
rect(x-60, 545, 30, 50); //body

fill (#DEACAC);

ellipse(x-45, 540, 50, 50); // head

fill (#000000);
rect(x-60, 532, 13, 10); //left eye sunglasses
rect(x-50, 532, 11, 3); //middle piece sunglasses
rect(x-42, 532, 13, 10); //right eye sunglasses

ellipse(x-45, 551, 10, 5); //mouth
ellipse(x-55, 595, 13, 5); //left foot
ellipse(x-35, 595, 13, 5); //right foot

fill (#FEFF0D);
ellipse(10, -5, 250, 250); //sun
if (mousePressed) {
fill (#E0E0E0);
ellipse(10, -5, 250, 250); //moon
}
grass.display();
fill(#FFFFFF);
text("Score: " + score, 693, 33);

Ball = Ball + speedBall;
if (Ball > 600) {
showMessageDialog(null, “Close One!”);
System.exit(0);
}

if (Ball < 600);
{
fill (#FF780F);
ellipse(RandomSpawnWidth, 0+Ball, 50, 50);

if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius+44) {

  //if (BallX > x - 30 && BallX < x+30); 

    BallX = random(20, width-20);

}
}
}

you had RandomSpawnWidth here !!!

 ellipse(BallX, 0+Ball, 50, 50);  // RandomSpawnWidth