• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1shinypixel  Members

226
Like
0Likes
Like

Posted 24 July 2014 - 12:33 PM

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?

### #2WFP  Members

2434
Like
0Likes
Like

Posted 24 July 2014 - 01:20 PM

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, 24 July 2014 - 01:22 PM.

### #3SmkViper  Members

5364
Like
2Likes
Like

Posted 25 July 2014 - 07:55 AM

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.

### #4kburkhart84  Members

2887
Like
0Likes
Like

Posted 28 July 2014 - 10:20 AM

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.

### #5Rld_  Members

1777
Like
0Likes
Like

Posted 29 July 2014 - 01:34 AM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.