Back

Technologies:

javascript
Tolerim
a day ago

### What is the process for causing a ball to bounce away from a paddle at a certain angle based on where it makes contact?

I'd like to implement a feature where the ball bounces off at a specific angle based on the paddle's location. For example, if the ball hits the top of the paddle, it should bounce upwards. Although I tried using the below code, the ball just flips around and continues moving in the same direction.

``````if (ballx < 15 && bally < paddle1 + paddle1height / 2 && bally > paddle1 - paddle1height / 2) {
angle += 180;
}
if (ballx > 585 && bally < paddle2 + paddle2height / 2 && bally > paddle2 - paddle2height / 2) {
angle += 180;
}
``````
Is there a way to achieve this behavior? If so, please provide your suggestions. Thank you!

Tolerim
a day ago
To make the ball bounce off at an angle depending on where it hits the paddle, you need to calculate the angle of reflection and then use that to update the angle of the ball. Here's an example of how you can do this:
``````if (ballx < 15 && bally < paddle1 + paddle1height / 2 && bally > paddle1 - paddle1height / 2) {
// Ball hit paddle 1