Public Group

# Bouncing Ball in Pong

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

## Recommended Posts

I am currently working on a pong clone in SDL and i have everything working, menus, paddles, etc. All that is left to do is add the ball, collision and the ball bouncing, but i dont know how to do that.(Make a ball class maybe?). Can someone please tell me the logic behind it. You dont have to tell me the code but just the game logic. Im not very good at math and physics to sorry if i dont understand the math easily.

Thanks

##### Share on other sites
Collisions can be detected fairly straightforwardly - try drawing out the various possibilities on a piece of paper, and think in terms of comparing e.g. ball.left < playground.left, etc. You can then respond by just flipping the sign on the appropriate component of its velocity (i.e. if it hits the left or right wall, do dx *= -1, top or bottom, do dy *= -1). This has limitations: if the ball is moving very quickly, it might clip into the wall and kind of 'bounce' along it. But it's nice and simple. :-)

##### Share on other sites

Collisions can be detected fairly straightforwardly - try drawing out the various possibilities on a piece of paper, and think in terms of comparing e.g. ball.left < playground.left, etc. You can then respond by just flipping the sign on the appropriate component of its velocity (i.e. if it hits the left or right wall, do dx *= -1, top or bottom, do dy *= -1). This has limitations: if the ball is moving very quickly, it might clip into the wall and kind of 'bounce' along it. But it's nice and simple. :-)

Okay, so ill just say what im planning on doing and correct me if i get something wrong.
1) Make a ball class
2) Make a collision detection function using your solution
3) If collision true, call a function to make the ball bounce by flipping the sign

Correct me if im wrong but if i just flip the sign, wont the ball just bounce back and forth in a line?

##### Share on other sites

[quote name='TheUnbeliever' timestamp='1316700506' post='4864676']
Collisions can be detected fairly straightforwardly - try drawing out the various possibilities on a piece of paper, and think in terms of comparing e.g. ball.left < playground.left, etc. You can then respond by just flipping the sign on the appropriate component of its velocity (i.e. if it hits the left or right wall, do dx *= -1, top or bottom, do dy *= -1). This has limitations: if the ball is moving very quickly, it might clip into the wall and kind of 'bounce' along it. But it's nice and simple. :-)

Okay, so ill just say what im planning on doing and correct me if i get something wrong.
1) Make a ball class
2) Make a collision detection function using your solution
3) If collision true, call a function to make the ball bounce by flipping the sign

Correct me if im wrong but if i just flip the sign, wont the ball just bounce back and forth in a line?
[/quote]

You also need the angle at which the ball impacts the plane:

http://en.wikipedia.org/wiki/Deflection_(physics)

This is then used to set the angle at which the ball is bounced away from the plane.

##### Share on other sites
No. Changing the dx,dy (Destination X, Y) coordinates or the speed of the ball in terms of X and Y by multiplying by negative one would change the speed of the ball to the opposite direction.

Ex: Ball is moving at Y: -5 (5 pixels up) per frame. -5 * -1 = 5 so dy = Y: 5 so the ball is moving 5 pixels down per frame.

EDIT:: And what the above poster said if you plan on implemented correct angular collision :S

##### Share on other sites
Okay...I think i sort of get it. Again im really bad at math and SDL's inverse graphing system just messes me up even more. So i get the angle that the ball is moving down at(the speed it moves forward will probably be a constant) so i only need to flip that?

##### Share on other sites

No. Changing the dx,dy (Destination X, Y) coordinates or the speed of the ball in terms of X and Y by multiplying by negative one would change the speed of the ball to the opposite direction.

Ex: Ball is moving at Y: -5 (5 pixels up) per frame. -5 * -1 = 5 so dy = Y: 5 so the ball is moving 5 pixels down per frame.

EDIT:: And what the above poster said if you plan on implemented correct angular collision :S

Just read another thread on this; pong doesn't use exact real world physics, it changes the angle based on the position of impact on the paddle. So the angle is different if the ball hits the side of the paddle rather than the centre.

##### Share on other sites
You also need the angle at which the ball impacts the plane:

In an elastic collision, the inward and outward angles are identical, so reflecting the vector in the plane of the wall (which is axis-aligned here) suffices.

##### Share on other sites

Collisions can be detected fairly straightforwardly - try drawing out the various possibilities on a piece of paper, and think in terms of comparing e.g. ball.left < playground.left, etc. You can then respond by just flipping the sign on the appropriate component of its velocity (i.e. if it hits the left or right wall, do dx *= -1, top or bottom, do dy *= -1). This has limitations: if the ball is moving very quickly, it might clip into the wall and kind of 'bounce' along it. But it's nice and simple. :-)

One way to avoid this I guess is to keep track of the balls last position, and once you detect a hit you move the ball to its last position before flipping the sign.
Not a very fancy solution maybe but keeping track of the last position of an object also has other applications than pong so it could be worth the practice

##### Share on other sites
Still no clue how im gonna do this
how to get the angle?

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 23
• 11
• 25