# Brick Game/Interactions

**URL:** <https://discourse.processing.org/t/brick-game-interactions/38198>\
**Category:** Processing\
**Tags:** homework\
**Created:** [August 2, 2022, 7:03pm UTC](https://discourse.processing.org/t/brick-game-interactions/38198 "2022-08-02T19:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![searchingforananswer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@searchingforananswer](https://discourse.processing.org/u/searchingforananswer)\
**Post date:** [August 2, 2022, 7:03pm UTC](https://discourse.processing.org/t/brick-game-interactions/38198/1 "2022-08-02T19:03:33Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [August 2, 2022, 7:27pm UTC](https://discourse.processing.org/t/brick-game-interactions/38198/2 "2022-08-02T19:27:31Z")

</div>

Hi @searchingforananswer,

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

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

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![searchingforananswer](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@searchingforananswer](https://discourse.processing.org/u/searchingforananswer)\
**Post date:** [August 2, 2022, 7:35pm UTC](https://discourse.processing.org/t/brick-game-interactions/38198/3 "2022-08-02T19:35:52Z")

</div>

Hello @mnse ,

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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [August 3, 2022, 8:33am UTC](https://discourse.processing.org/t/brick-game-interactions/38198/4 "2022-08-03T08:33:21Z")

</div>

Hi @searchingforananswer,

Take a look [here](https://www.gamedevelopment.blog/collision-detection-circles-rectangles-and-polygons/) (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](https://stackoverflow.com/questions/45370692/circle-rectangle-collision-response) which explains pretty good…

Cheers  
— mnse
