Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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?

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?
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.

1 solution

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 xis reversed while the y component stays the same.
Share this answer
bhaskar977 12-Jan-23 4:12am    
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900