15,850,103 members
See more:
Here's the full code:
https://codepen.io/pelko/pen/MWBpNmL

When a ball collides with bat or walls, I want the ball to be reflected with an angle, how do I do it?

What I have tried:

I'm not sure what to try. 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?
Posted
Updated 11-Jan-23 21:22pm
Richard MacCutchan 12-Jan-23 5:24am
Use a randomiser to adjust the x or y as appropriate so that it travels in a diagonal line.

## Solution 1

You obtain the simplest scenario by neglecting friction.
When the ball hits the top (or bottom) of the wall then its `y` (component of direction) is reversed (negated), while the `x` one stays the same.
On the other hand, when the ball hits left (or right) then its `x`is reversed while the `y` component stays the same.

I'm already doing it. It's causing straight motion.
CPallini 12-Jan-23 4:56am
Because the initial direction is aligned to the y axis.
Try different initial direction components.