# Help me to bounce the ball of the paddle

**URL:** <https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 19, 2020, 5:53pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540 "2020-11-19T17:53:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AzizurRahman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/azizurrahman/32/11064_2.png) [@AzizurRahman](https://discourse.processing.org/u/AzizurRahman)\
**Post date:** [November 19, 2020, 5:53pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/1 "2020-11-19T17:53:01Z")

</div>

Hi so far I wrote this code which has the ball bouncing from the edges.But I have the paddle here but i want it to make bouncing from the from the paddle I have literally no idea how make the ball bounce from the paddle. Its a bouncing ball game.

//defining constants  
final int BALL\_SIZE=20;  
final int WALL\_TOP=50;  
final int WALL\_LEFT=50;  
final int WALL\_SIZE=400;  
final int PADDLE\_LENGTH=80;  
final int THICKNESS=10; //thickness of wall and paddle

float ballX, ballY;  
float ballSpeedX, ballSpeedY;  
float paddleX, paddleY;

//method to draw paddle  
void drawPaddle(){  
//finding left position of paddle based on mouse position  
paddleX=mouseX-PADDLE\_LENGTH/2;  
//if this is beyound left wall, adjusting it  
if(paddleX\<WALL\_LEFT){  
paddleX=WALL\_LEFT;  
}  
//if this is beyound right wall, adjusting it  
if((paddleX+PADDLE\_LENGTH)\>(WALL\_LEFT+WALL\_SIZE)){  
paddleX=WALL\_LEFT+WALL\_SIZE-PADDLE\_LENGTH;  
}  
//using blue color  
fill(0,0,255);  
//drawing paddle  
rect(paddleX,paddleY,PADDLE\_LENGTH,THICKNESS);  
}

//method to draw the three walls  
void drawWall(int wallLeft, int wallTop, int wallWidth){  
//using yellow color  
fill(255,255,0);  
//drawing top wall  
rect(wallLeft,wallTop,wallWidth,THICKNESS);  
//drawing left wall  
rect(wallLeft,wallTop,THICKNESS,height-wallTop);  
//drawing right wall  
rect(wallLeft+wallWidth,wallTop,THICKNESS,height-wallTop);  
}

//method to draw ball  
void drawBall(float ballX, float ballY, float ballSize){  
//using white color  
fill(255);  
//drawing ball  
circle(ballX,ballY,ballSize);  
}

//method to update x or y position of the ball based on speed  
float moveBall(float ballXY, float speed){  
return ballXY+speed;  
}

void setup(){  
size(500,400);  
noStroke(); //no stroke lines  
//placing ball somewhere on top right  
ballX=width_0.7;  
ballY=height_0.3;  
//ball speed (could be direction also based on the sign)  
ballSpeedX=1;  
ballSpeedY=1;  
//placing paddle somewhere at the bottom  
paddleY=height\*0.9;

}

void draw(){  
//using a gray bg  
background(127);  
//drawing paddle  
drawPaddle();  
//walls  
drawWall(WALL\_LEFT,WALL\_TOP, WALL\_SIZE);  
//and ball  
drawBall(ballX, ballY, BALL\_SIZE);

//moving ball’s x coordinates  
ballX=moveBall(ballX,ballSpeedX);  
//changing x direction if hit left wall  
if((ballX+BALL\_SIZE)\>=(WALL\_LEFT+WALL\_SIZE+THICKNESS)){  
ballSpeedX\*=-1;  
}  
//changing x direction if hit right wall  
if((ballX-BALL\_SIZE)\<=WALL\_LEFT){  
ballSpeedX\*=-1;  
}  
//moving ball’s y coordinates  
ballY=moveBall(ballY,ballSpeedY);  
//changing y direction if hit top wall  
if((ballY-BALL\_SIZE)\<=WALL\_TOP){  
ballSpeedY\*=-1;  
}

}

---

<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:** [November 19, 2020, 6:27pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/2 "2020-11-19T18:27:53Z")

</div>

You have to check, whether

- the ballX is \> left side of paddle and
- ballX \< right side of paddle,
- and ballY \>= upper side of the paddle.

If so, bounce ball, which is  
`ballSpeedY = -1 * abs(ballSpeedY); // abs() is always positive, so the formula makes it always negative `

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Vako](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@Vako](https://discourse.processing.org/u/Vako)\
**Post date:** [November 19, 2020, 7:23pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/3 "2020-11-19T19:23:06Z")

</div>

You need to take in consideration a few things, first collision detection, you have to know when is the ball touching the paddle, and of course where.  
I’d recommend you to give your ball object a direction vector, so when it comes in contact with a “wall” or the paddle you know how to flip it properly

---

<div class="post-metadata">

**Author:** ![AzizurRahman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/azizurrahman/32/11064_2.png) [@AzizurRahman](https://discourse.processing.org/u/AzizurRahman)\
**Post date:** [November 19, 2020, 8:49pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/4 "2020-11-19T20:49:55Z")

</div>

How can write the if statements? My problem is making the if statement work… You said like when ballX is greater than left side of paddle but then It will bounce even if ball and paddle doesn’t collide!

---

<div class="post-metadata">

**Author:** ![AzizurRahman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/azizurrahman/32/11064_2.png) [@AzizurRahman](https://discourse.processing.org/u/AzizurRahman)\
**Post date:** [November 19, 2020, 8:50pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/5 "2020-11-19T20:50:39Z")

</div>

Sorry I am looking for way to make it work without vector function

---

<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:** [November 19, 2020, 8:51pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/6 "2020-11-19T20:51:42Z")

</div>

> [@AzizurRahman](#):
>
> //changing y direction if hit top wall  
> if((ballY-BALL\_SIZE)\<=WALL\_TOP){  
> ballSpeedY\*=-1;  
> }

It is similar to this code

please show your attempt

---

<div class="post-metadata">

**Author:** ![AzizurRahman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/azizurrahman/32/11064_2.png) [@AzizurRahman](https://discourse.processing.org/u/AzizurRahman)\
**Post date:** [November 19, 2020, 9:04pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/7 "2020-11-19T21:04:13Z")

</div>

So I basically added this line to test. But this is bouncing even when my paddle is not there

void bounceOfThePaddle(){

if(ballY-BALL\_SIZE\>paddleY-THICKNESS)  
ballSpeedY\*= -1;  
}

---

<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:** [November 19, 2020, 9:05pm UTC](https://discourse.processing.org/t/help-me-to-bounce-the-ball-of-the-paddle/25540/8 "2020-11-19T21:05:59Z")

</div>

Yeah, you also need to check the **x position** of the ball against the paddle

as I wrote…
