I am making a classic pong game. Think of the old school, black and white pong games from the 80’s. I’m at the phase where I would like to develop realistic collision detection. Well, as close to reality.
The collision is focused on the Y coordinates.
i would like the collision to depend upon the angle of the ball hitting the pong paddle. Currently my collision just randomly assigns a number to “randomly” decide which direction the ball goes to.
The ball must know in which direction it’s going, so you can just do a negative inversion on the y axis when it makes contact.
You can tell when it makes contact, because the distance from the ball to the edge of the paddle is less than or equal to the radius of the ball.
Some pong games also adjust the reflection angle depending on how close the ball is to the edge of the paddle. Exploring that though can wait until you get the bounce done right.
My current collision just inverses the y and x axis. It works for the x axis but the problem comes with the Y axis.
The ball will travel at the exact angle every time, no matter where it hits on the paddle. So if I keep my code like it currently is, the ball will just keep traveling back and forth, on the exact same path without ever changing directions.
To negate this, I added a slight random factor for the Y axis, so the ball can slightly move directions, but this is of course based on pure randomness and not actual physics.
I would like the ball to travel depending on where on the paddle it hit.
I just can’t think of the physical logic, like how the ball would react if it hit the center of the paddle? What about at the top of the paddle? The bottom?
What you’ll want is where on the paddle it was hit. If at the direct centre, then there should be no change to the reflection.
Would it help to imagine that the paddle is instead a circle, and that the ball should reflect based on the angle that it hit that circle?
Normalize the paddle width from -π to π. At zero there should be no difference in the reflection.
sin(-π) is -1, sin(0) is 0, and sin(π) is 1.
At -1 the reflection should be angled a lot more to the left, and at 1 the angle should be reflected a lot more to the right.
Those values can be a scaling factor for a variation value.
So, ball direction is stored as a directional vector.
After that direction is reflected, you add on a 90 degree variation multiplied by sin(θ) where θ ranges from -π to π along the edge of the paddle.
Well, in actual physics, its a flat paddle, so it would be a perfect reflection, and you already had your actual physics model. The curve of the standard PONG paddle is 0.
the other example you’re trying to mimic is a semispherical paddle. At which point the reflection angle is calculatable, however there are undesirable outcomes (There are incidental angles of reflection on a sphere such that the line would still go in the ‘wrong direction’).
In full truth, what you’re trying to mimic is half an ellipse, such that the angle of movement is a reflection across an axis of rotation such that said axis is perpendicular to the ellipse at the contact point.