There are a few people working on a pong type Opengl games here in this forum, as an example and learning tool for myself I have starting writing a simple pong type program using GLUT.
Currently my ball bounces around the screen with set boundaries and will be deflected by the paddle. My question is on how the ball is deflected by the paddle and walls.
I use a simple check if any x or yaxis of the ball comes to a boundary, change direction that makes all direction changes at a 90degree angle.
What is the function for the deflection of an object bouncing off a surface?
For reflection off the xaxis, negate the y component of the ball’s velocity. For reflection off the yaxis, negate the x component of the ball’s velocity.
[EDIT]
Or do you need the ball to bounce off of a surface that is oriented in any which way?
[/EDIT]
j
[This message has been edited by j (edited 04112002).]
Coredump, it is no actual physics to say that, as it is not exactly true for photons and the like. The probability of this case is the highest though but there exist other possibilities
I guess my big problem is writing the code so that the object hits at an angle other then 90 degrees.
Wall

 / 45 in
*
 \ 45 out

Also on the paddle, I think in the old pong game depending where you hit the paddle you got a varying degree of change in the angle in which the ball bounced off it.
Paddle
 15
 45
 90 degree at center hit…
 45
 15
I am sure that I will come up with something, but right now my brain is fried from work…
Originally posted by j:
[b]For reflection off the xaxis, negate the y component of the ball’s velocity. For reflection off the yaxis, negate the x component of the ball’s velocity.
[EDIT]
Or do you need the ball to bounce off of a surface that is oriented in any which way?
[/EDIT]
j
[This message has been edited by j (edited 04112002).][/b]
i wrote a version of pong a while ago. The angle thing is resolved when you have a (x,y) trajectory, and on collision with a y orientated object (vertical), simply change it to (x. y), which will cause the ball to bounce back in the opposite direction with an inverse angle like
/
/
/
/
/
\etc
if you want to change the angle depending on the where the collision with the paddle occurs, simply calculate the position of the ball relative to the center of the paddle. If the ball Y is < paddlemiddle Y then ball y = some value… or instead of some value try (paddlemiddleY  ballY) or (ballY  paddlemiddleY) if bally > paddlemiddleY.
j is correct (if I understood him right). The easy way to do deflection is:
If the ball hits something vertical, reverse it’s x value.
If the ball hits something horizontal, reverse the y value.
If its not vertical or horizontal you have to use vectors to determine correct deflection. I.e. if the ball is moving with vector v, and the object has normal N, then the deflected vector v1 is given by v1 = v  2 * dot(v, N) * N. This also works for vertical and horizontal cases.
The paddle is different. Essentially you have to adjust N by a linear or cosine relationship to where it hit the paddle. Give me a little while to do the maths and I’ll update the post.
Basically that is what I cam currently doing, just looking to improve it.
if (ball_state > 0) // Manage ball if in motion
{
// pos + rate of movement * direction
ball_pos_x = ball_pos_x + 0.5 * ball_direction_x;
ball_pos_y = ball_pos_y + 0.5 * ball_direction_y;
}
if ((ball_pos_x > 490)   (ball_pos_x < 10)) ball_direction_x = ball_direction_x * 1; // Hit an x screen boundry change direction
if ((ball_pos_y > 240)   (ball_pos_y < 10)) ball_direction_y = ball_direction_y * 1;
Originally posted by Weazel:
[b]i wrote a version of pong a while ago. The angle thing is resolved when you have a (x,y) trajectory, and on collision with a y orientated object (vertical), simply change it to (x. y), which will cause the ball to bounce back in the opposite direction with an inverse angle like
/
/
/
/
/
\etc
if you want to change the angle depending on the where the collision with the paddle occurs, simply calculate the position of the ball relative to the center of the paddle. If the ball Y is < paddlemiddle Y then ball y = some value… or instead of some value try (paddlemiddleY  ballY) or (ballY  paddlemiddleY) if bally > paddlemiddleY.
you see what i mean?
enjoy[/b]
[This message has been edited by nexusone (edited 04122002).]
[This message has been edited by nexusone (edited 04122002).]
[This message has been edited by nexusone (edited 04122002).]
Here are two ways to make the ball bounce off the paddle.
Assume paddle has three sections. Left, Middle and Right. If it hits the middle normal horizontal deflection applies. If it hits the left or right then you want skewed deflection.
left mid right
xxxx
x1 x2 x3 x4
For shewed deflection you need to determine a value for the deflection axis, N. This is the distance from the closer end over the length of that section.
If you are using a linear relationship this value is used create N as a weighted average between the paddle’s normal, Np, and a maximum deflection axis (Nl for left, Nr for right).
if (x1 < Ball.x < x2)
N = Skew * Np + (1  Skew) * Nl
else if (x3 < Ball.x < x4)
N = Skew * Np + (1  Skew) * Nr
If you are using an angular relationship this value is converted to an angle for the deflection axis, A, between 0 and a maximum angle (+/Amax for left or right) which is used to create N.
if (x1 < Ball.x < x2)
A = Amax * Skew
else if (x3 < Ball.x < x4)
A = Amax * Skew
N = (sin(degtorad(A), cos(degtorad(A))
Then use the general deflection code to get the final direction the ball moves in. v is initial velocity of the ball and v1 is the final velocity of the ball.
v1 = v  2 * dot(v, N) * N
You might also want to create a maximum deflection angle. Otherwise if the ball angle of incidence is large it will still be deflected off screen.