# Pong ball movement

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

## Recommended Posts

Hi I've made a pong game in directx9 and I got movement with the pong ball but the movement is repeated in a patern that is obvious to learn. Does anyone know a good way to make it bounce of tha walls and paddle at different angles. By the way I suck at geometry. I've tried to add an extra move to the x and y values but that but makes the ball go faster and slower, not a real definate speed. Any help would be appreciated thanks. If you want to see what I am doing you can download the source and exe. I programed it in c++ 6 and directx 9. Test Pong Download

##### Share on other sites
you have acceleration and slowdowns because adding some small deflection changes the magnitude of the velocity (magnitude = length = norm of a vector).

so, you need to 're-normalise' the velocity to conserve kinetic energy (the ball will move at constant speed).

two solutions,

1 add the deflections, but renromalise the velocity
2) rotate the velocity vector by 'x' degrees (which I think is better.

both refer to vector maths, with 2 adding a bit of trigonometry into the mix. if you don;t know vector maths or anything I just said above, I strongly suggest you look into it.

http://www.gamedev.net/reference/articles/article1832.asp

since it;s a simple solution, to achieve 1, all you need to do is

1)
float speed = Velocity.Magnitude();Velocity += Vector(frand(3) - frand(3), frand(3) - frand(3)); // max of 3 pixels deflection.Velocity *= (speed / Velocity.Magnitude());

2)
RotateVector(Velocity, frand(10)-frand(10)); // rotate vector by -10->10 degreesfloat Pi(){    static const float pi = (float) atan(1.0f) * 4.0f;    return pi;}float DegreesToRadians(float deg){    static const float deg_to_rad_constant = Pi() / 180.0f;    return deg * deg_to_rad_constant;}void RotateVector(Vector& V, float degrees){    float rad = DegToRad(degrees);    float co = cos(rad);    float si = sin(rad);        float x = V.x * co + V.y * si;    float y =-V.x * si + V.y * co;    V.x = x;    V.y = y;}

##### Share on other sites
So I set the speed of the ball to 10.0f, and then I pass into the rotate function 90 degrees, the ball would go straight up right. I can't seem to get that to work.

##### Share on other sites
No, that function causes you to face a direction relative to the direction you were facing. The angle is how many degrees to the left you want to turn. If you want to face a direction relative to right on the screen then you feed in (1,0). If you want the angle to be how much to the right to turn then reverse the sign on the si terms. Left and right here assuming the coordinates increase from top/left to bottom/right as is normal in Windows.

##### Share on other sites
How would I calculate the new Angle with the ball hitting the wall at a certain angle. Like for example the ball hits the top wall going right so that means the x value is positive and the y value is negative, and the angle it hits at is 45 degrees how would I calculate the opposite degree so I could rotate the ball x and y?

##### Share on other sites
So does anyone have a clue, or a tutorial on how to do this. Ive read that article and I can't think of what I need to do to do this.

##### Share on other sites
Have you tried searching the forum archives? I just searched on the word "Pong" and there are about 50 different threads going back to 2001. If I were you, I would *seriously* take a look at all these past discussions!

##### Share on other sites
The assumption was that you negated the appropriate component to perform the bounce to start with. Then you are just modifying that direction by a small amount.

• 13
• 18
• 29
• 11
• 20