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 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=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
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;
}

}

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

please show your attempt

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;
}

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

as I wrote…

1 Like