I’m just making your regular pong game. I came to a point where I need to calculate how the ball needs to fly after it hit a pad.
Any ideas?
It depends.
If you want it quick-and-dirty (how most pong games work): simply flip xVelocity.
Yeah, that’s what I’d do, with a little nudge on yVelocity based on the speed of the bat.
Thats all? The ball would fly back and forward in the same track forever? Did I miss something?
Combine my suggestion with Simon’s and you have a less predictable path.
If you want the real deal google ‘reflection vectors’ - but IMHO it’s overkill for pong!
also nudge direction based on how far of the center of the bat - else it wont ever do anything but up and down
Thanks for your replies. I have thought of a way but I’m unsure about some formulas:
I have taken time, and speed out of the picture completely! I want the ball to hit the pad and go in the direction it hit the pad in.
For example, it hits the buttom of the pad - the ball turns and goes 45 degrees (south). Or if it hits the middle of the pad it would go straight 90 degrees from the pad.
How do I detect where the ball hits and then apply that to the balls curve? What formulas should I use?
I need to detect where the pad is, and where the ball (x-coord) was when it hit the pad right?
Ok, so you’re disregarding the ball’s incoming velocity and the pad velocity - the ball’s outgoing direction depends solely on where it hits the pad.
Assuming you know:
- The ball center y-coordinate - by
- The ball x-velocity - bvx
- The pad center y-coordinate - py
- The pad size - ph
The ball velocity after striking the pad will be (-bvx, (by - py) / ( ph / 2 ) * bvx )