1
General / Re: Ball in Pong only moves in 4 directions
« on: November 22, 2016, 07:53:58 pm »
I'm not incredibly mathematically inclined, so this is something that I kind of struggled with too.
The solution that I came up with is to compute an angle based on where the Ball strikes the paddle. The "angle of reflection" becomes much more severe as the Ball hits closer to one of the extreme ends (top or bottle of the Paddle). Similarly, the angle is less severe the closer to the center the ball strikes. This way, the player can bounce the Ball upwards if they position the paddle in a way that it hits towards the top; likewise for bouncing the Ball downwards.
It looks a little something like this:
The closer to the center/green the Ball hits, then a smaller angle is computed and the Ball's reflection angle isn't changed drastically.
The closer to the edges/orange-red the Ball hits, then a larger angle is computed and the reflection angle is much more extreme.
Mathematically, this is my approach:
Hint: just following the math won't replicate the way I did it entirely. In order to do that, remember that SFML's Y axis is reversed from normal 2D coordinates that we do in math.
Hope this helps.
The solution that I came up with is to compute an angle based on where the Ball strikes the paddle. The "angle of reflection" becomes much more severe as the Ball hits closer to one of the extreme ends (top or bottle of the Paddle). Similarly, the angle is less severe the closer to the center the ball strikes. This way, the player can bounce the Ball upwards if they position the paddle in a way that it hits towards the top; likewise for bouncing the Ball downwards.
It looks a little something like this:
The closer to the center/green the Ball hits, then a smaller angle is computed and the Ball's reflection angle isn't changed drastically.
The closer to the edges/orange-red the Ball hits, then a larger angle is computed and the reflection angle is much more extreme.
Mathematically, this is my approach:
- Calculate the distance between the Ball's center and the center of the Paddle.
- Compute the angle using cmath's sin() function, passing in the distance divided by the maximum desired angle. In my case, I used 50 as my maximum desired angle.
Hint: just following the math won't replicate the way I did it entirely. In order to do that, remember that SFML's Y axis is reversed from normal 2D coordinates that we do in math.
Hope this helps.