# How to reflect the ball at an angle?

**URL:** https://forum.kirupa.com/t/how-to-reflect-the-ball-at-an-angle/656254
**Category:** programming
**Created:** [January 12, 2023, 4:33am UTC](https://forum.kirupa.com/t/how-to-reflect-the-ball-at-an-angle/656254 "2023-01-12T04:33:08Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)
#### Post date: [January 12, 2023, 4:33am UTC](https://forum.kirupa.com/t/how-to-reflect-the-ball-at-an-angle/656254/1 "2023-01-12T04:33:08Z")

</div>

Here’s the full code:  
https://codepen.io/pelko/embed/preview/MWBpNmL?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=MWBpNmL

When a ball collides with bat or walls, I want the ball to be reflected with an angle, how do I do it?  
I suspect there’s lots of physics to it. So, should I stop this project here? I’d have to probably build a part of game engine…Can anyone help here?

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [January 12, 2023, 11:24pm UTC](https://forum.kirupa.com/t/how-to-reflect-the-ball-at-an-angle/656254/2 "2023-01-12T23:24:57Z")

</div>

There doesn’t have to be a lot of physics. The question is, how do you want to calculate the angle? Does the location it bounces off the paddle determine the angle it flies from. If you want to get even more detailed, it would be less the position of the paddle (since the paddle is a straight surface) and more on the direction the paddle is moving the moment it makes impact.

You can get fancy, or you can keep it fairly simple. Instead of thinking about the angle, think about it in terms of the horizontal speed the ball is moving in.

Right now, the ball has no horizontal speed it is just moving vertically. We can set the midpoint of the paddle to be one where the horizontal speed of the ball is incremented by zero. The current horizontal speed is maintained.

The leftmost edge of the paddle can be something like:

```auto
horizontalSpeed -= 5;

```

The rightmost edge of the paddle can be something like:

```auto
horizontalSpeed += 5;

```

You can adjust the amount equally for all other positions within the paddle’s surface. When the ball hits a wall on the left or right side, just flip the sign of the horizontal speed. If the ball hit the leftmost wall with a `horizontalSpeed -= 2`, it will bounce away with a `horizontalSpeed += 2`.

This avoids you having to worry about angles at all. Thoughts on taking an approach like this?

Cheers,  
Kirupa
