Pong reflector math?

6.1k Views Asked by At

I am making a Pong clone and I am treating my variables in this way:

direction = a value from 0 to 2*pi 
distX = cos(direction)*speed of the ball
distY = sin(direction)*speed of the ball

on update:
ball's x position += distX
ball's y position += distY

I can already handle the flipping of directions correctly when a ball hits a wall (just reflect at 90 degrees) but what about a paddle? I don't know how to best handle the angle and speed of the ball after it connects.

Does it depend on where it hits on the paddle? Does it matter if the paddle is moving? etc

I don't know a reasonable way to handle the math here.

2

There are 2 best solutions below

7
On BEST ANSWER

There's no "right" way of doing this. You're the one who's writing the code, and unless you've got some strict specifications, you can handle it how you prefer much.

Basically, you can handle the ball-paddle collision the same way you handle a ball-wall collision, i.e. by simply calculating the reflection angle, and assigning it to direction.

If you want to increase the reflection angle proportionally with the paddle speed, you can simply apply some kind of weight to the new direction.

But it's really up to you.

Anyway, if you provide, for example, the code where you handle the paddle speed, someone could give you further indication.

1
On

I presume your paddles are rectangles then? For simplicity, lets assume that the ball may only strike the face of the paddle (that is, not the corners or the sides).

Q: Does it depend on where it hits on the paddle? A: If the paddle is an axis-aligned rectangle and the ball may only hit its surface, then no.

Q: Does it matter if the paddle is moving? A: This is your choice. Game physics are not real physics, so you are free to do whatever makes the game play the best.

The first thing is that rotating the ball's direction by 90 degrees does not accurately deflect the ball off the wall. Consider the extreme case where the ball hits the wall dead on. If I rotated the ball by 90 degrees, the ball would then travel parallel next to the wall.

I believe what you are currently doing, and just stated a little confusingly, that if the ball hits a vertical wall, you reverse the horizontal direction of the ball. This is, of course, not equivalent to rotating the ball's direction by 90 degrees.

If I wanted to impart speed from the paddle to the ball, I would treat the ball's movement as a vector and the paddle's movement as a vector. I'd then add these two vectors together. To control how much velocity is imparted to the ball, I'd use a coefficient (lets call it k), and if the ball needs to maintain a constant speed, I'd normalize the ball's final velocity vector (but since you are storing speed instead of x,y components, that isn't necessary).

if (ball hits paddle):
  k = 0.5;
  // paddle only moves horizontally, which simplifies the math
  paddle_vx = paddle_speed * paddle_xdirection;
  ball_vx = cos(ball_direction) * speed + k*paddle_vx; 
  ball_vy = - sin(ball_direction) * speed // flips from the collision
  ball_direction = atan2(ball_vy, ball_vx);
  ball_speed = 100; // always constant