Help me to bounce the ball of the paddle

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 THICKNESS=10; //thickness of wall and paddle

float ballX, ballY;
float ballSpeedX, ballSpeedY;

//finding left position of paddle based on mouse position
//if this is beyound left wall, adjusting it
}
//if this is beyound right wall, adjusting it
}
//using blue color
fill(0,0,255);
}

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

}

void draw(){
//using a gray bg
background(127);
//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;
}

}

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

1 Like

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

1 Like

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!

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

It is similar to this code

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