Pong - How to get different velocities based on where ball hits the pad?

Started by
3 comments, last by WhiskyJoe 9 years, 8 months ago

The idea is:

When the pad hits the ball, a center angle on the pad checks how far it is from the center. The further up or down causes an increase of the ball's velocity. When the ball is closer to the center, the ball's velocity decreases.

The only problem I'm having is how to code it. Is this a good way of doing this, or is there a better way to calculate the ball's new velocity?

Advertisement

You're on a good track. Taking the difference between the point at the center of the paddle and the point at the center of the ball, then adjusting the velocity as you see fit based on that vector's length sounds like a clean, simple solution to me, and it scales evenly from the center of the paddle to either edge.

You probably don't even need an angle in this case, simply the (absolute value of the) difference in Y coordinate between the center of the paddle and the center of the ball should suffice.

Indeed, if you just calculate the difference/distance between the opposite side of the paddle and the ball when the collision happens, you get a value that can then be mutated into a velocity. I say mutate it because unless it just happens to be perfect, it won't work right directly. For example, the values you get in the middle will be very small, while the values you get on the edges will be much larger, and I'm understanding you want a smoother scale there.

Another option is to use something I forget the exact name of, where you turn one "scale" into another. For example, the height of your paddle is 50 pixels, and you want values between 10 and -10(to get the velocity on the y axis specifically maybe), you would "convert" from down to -10/10 using a simple division. This is also a form of mutation as I mention above, but it could create a perfectly linear curve, which may or not be what you want.



I personally used a dot product between the centre of the paddle and the position of impact of the ball. I played around with the resulting value to get something that felt right.

Above mentioned options will work out fine as well, there's simply more than one way to make it work, just check out what feels best to you! :)

This topic is closed to new replies.

Advertisement