Brick Game/Interactions

Hello everyone! I’m new at coding and processing. I’m looking for an urgent answer for a problem occured while i’m trying to do a Bricker Game. I’m at the phase where i try to the interactions with the ball and the paddle. I tried so hard but couldn’t come with a solution. I’m working with classes and adding codes below. Please help, thank you.

Paddle paddle;
Ball ball;
boolean gameStart = false;
boolean gameFinished = false;
int streak = 0;

void setup(){
  
  size(900, 900);
  paddle = new Paddle();
  ball = new Ball();
}

void draw(){
  
  background(5, 18, 155);
  paddle.display();
  paddle.setLocation(mouseX, height-50);
  ball.display();
  ball.move();
  
 if (paddle.intersect(ball))
  {
      ball.yspeed *= -1;
    }
}

class Ball {

  float x, y, xspeed, yspeed, r;
  
  Ball() {

    x = width/2;
    y = height/2;
    xspeed = 4;
    yspeed = -4;
    r = 15;
    
  }
  
  void display(){
    
    fill(255);
    ellipse(x, y, r*2, r*2);
    
  }
  
  void move(){
    x += xspeed;
    y += yspeed;
    
    if (x > width-r && x > 0) {
      xspeed *= -1;
    }
    
    if (x < r && x < 0) {
      xspeed *= -1;
    }
    
    if (y < r && y<0) {
      yspeed *= -1;
    }
   
   } //move 
   
} //ball

class Paddle {
  
  float x, y;
  float a, b;
  Paddle() {
    x = width/2;
    y = height-50;
    a = 140;
    b = 25;
  }
  
  void setLocation(float x, float y) {
    this.x = x;
    this.y = y;
    
    this.x = constrain(this.x, a/2, width-a/2);

  }
  
  void display() {

    noStroke();
    fill(245, 0, 170);
    rectMode(CENTER);
    rect(x, y, a, b, 28);
    
  }
 }
1 Like

Hi @searchingforananswer,

Please format your code based on the description here…
https://discourse.processing.org/faq#format-your-code

I’ll take a look tomorrow (GMT) and help you to find a solution…

Cheers
— mnse

Hello @mnse ,

I edited like you said, thank you so much:)

Hi @searchingforananswer,

Take a look here (Chapter: Circle vs Rectangle (Axis Aligned)).
There’s a pretty neat description how you can get the intersection.
Give it a try on how far you can adapt the Info from the link and come back if you stuck…

Some Tipps:

  1. I would change your if statement above from questioning
    if (paddle.intersect(ball)) To if (ball.intersect(paddle))
  1. The intersection is only the first step. Afterwards you need to handle the response/reaction.
    It is not always the inversion of the yspeed. consider the hit on the corners or in your case the round corners. BUT keep it simple for the first step.
    If you managed the Step under 1. you can read further detail here which explains pretty good…

Cheers
— mnse

3 Likes