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

This topic is 1275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

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.

Edited by WFP

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites

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! :)