Bouncing Ball in Pong

Started by
23 comments, last by chosenkill6 12 years, 7 months ago
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
Advertisement
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. :-)
[TheUnbeliever]

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 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.
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
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?

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.
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.
[TheUnbeliever]

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
Still no clue how im gonna do this
how to get the angle?

This topic is closed to new replies.

Advertisement