# Help with rebound function

• 08-05-2007, 10:44 PM
fernando
Help with rebound function
Hi, I am trying to make a pong game in java and this is the rebound function for the left paddle. now just to set a few variables:
Code:

```x_pos is the balls x position y_pos is the balls y position x_speed is the x speed of the ball radius is only for the ball and is the balls radius. paddle1_x you can guess paddle1_y you can also guess...```
Now i did not have a radius for my paddles so the paddle stretches 40 y down and 10 x in width... so without any further ado, heres the code:

Code:

```public void bounceToLeft(){  if(x_pos-radius<=paddle1_x+10&&x_pos-radius>=paddle1_x){   if(y_pos<=paddle1_y-40&&y_pos>=paddle1_y){   x_speed=-1;   }  } }```
I call this function in the run method of the applet. Does anyone know why it doesn't work?

Thanks
• 08-06-2007, 07:12 AM
hardwired
Code:

```public void bounceToLeft(){     if(y_pos + 2*radius > rightPaddle_y &&            // ball will hit top of paddle       y_pos < rightPaddle_y + rightPaddle_height &&  // ball will hit paddle bottom       x_pos + 2*radius > rightPaddle_x)              // ball hits paddle side         x_speed *= -1;                                // toggle speed direction } public void bounceToRight() {     if(y_pos + 2*radius > leftPaddle_y &&                // hit top of paddle       y_pos < leftPaddle_y + leftPaddle_height &&        // hit paddle bottom       x_pos + x_speed < leftPaddle_x + leftPaddle_width) // hit paddle side         x_speed *= -1; }```