# How to reflect the ball at an angle?

Here’s the full code:

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?

2 Likes

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:

``````horizontalSpeed -= 5;
``````

The rightmost edge of the paddle can be something like:

``````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