#### Archived

This topic is now archived and is closed to further replies.

# Some Physics for a pong like game

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

## Recommended Posts

I need help with my pong game. As it stands now, I have a ball flying around the screen with some collision detection when it hits a paddle. The problem is that I have no idea how to change the the speed and direction of the ball when it hits the paddle in different places. Here is some code that I have (Condensed): if([collision test with horizontal paddle]) { BallDY = -1 * BallDY; } Sure it works when the ball hits the center of the paddle, but it looks shitty when it hits the edge of the paddle, because every pong game I''ve played has the ball hitting the edge and flying back in the direction that it came from. Any help would be great. thanks

##### Share on other sites
if (collision detect for right side) {
bounce a direction
}
else if (collision detect for center) {
bounce a direction
}
else if (collision detect for left) {
bounce a direction
}

of course, the direction should be modified by the angle the ball is already at, but u should be able to play around with it to get it right

##### Share on other sites
First of all,

BallDY = -1 * BallDY;

takes more time to execute than

BallDY = -BallDY;

since a multiplication takes more time than simply XORing the high bit of an integer or whatever you''re using to save your velocity...

last bit -->0101

next thing... if you want to be simple, you can do the following to implement what you want...

{
BallDY = -abs(BallDY);
BallDX = -BallDX;
}

Okay... That''s not optimized AT ALL... and if I were not sleeping while writing this bit of code (who knows) it would work...

what the if statement is testing for is to see if the ball is about to hit the right paddle, to see if the ball is hitting the upper edge of the paddle, and to see if you''re still moving the right paddle to go up, which in real life will make the ball go in the direction it came from (like a contra thing)...

This is the simple way of doing what you want.. There are of course ways to do this using trigonometry and more physics math by calculationg the angles and shit... I''m not gonna get into that right now... If you still need more info you''re welcome to ask..

Anyways... Hope this helped you a bit.. I''m out..

..-=ViKtOr=-..

##### Share on other sites
btw, KEY_UP(ARROW_UP) should''ve been KEY_DOWN(ARROW_UP)

and by those two i''ve meant it like a macro that checks to see if a key is up or down... anyways..

##### Share on other sites
Hehehe, I do believe he said he was workig a Pong game... and will have plenty of free clock-ticks to use fp''s if wants, never mind xor''ing instead of BallDY *= -1; if it really is just pong... not 3D Turbo-Pong-Mania or something... Explore first, then design, code, re-design, code, re-design, code & optimize. (plz don;t think i''m against optimizing, I would save those clock-ticks too, just not for pong

Are you thinking of the varied velocity of the ball, as in games like arknoid? You need to know where the balls hits the paddle relative to its center (beit opt''ed integer math or sluggish fp %''s) and adjust the BallDX & BallDY by 20% or so of the off-center hit. It''s been a long time since I played, but it seemed like arknoid used a quadratic on the Dx & linear on the DY...

##### Share on other sites
Thanks for all the help so far.
I''m really looking for the code to modify the velocity of the ball. Some Questions: When a ball hits an object that is still, does the ball just bounce off in the opposite Y direction with no loss of X velocity or does it do something weird or random. What happens when the paddle itself is moving?
Smaple code would be great.

##### Share on other sites
I gave you some pseudo code for when the paddle is moving... It doesnt use any physics, just simple solution that checks for some collisions and does something according to them.... also, pong is a great game that you have to try to optimize once you''re sure how it works... it''s doesnt require that much coding, and it would be more easy for a beginner to optimize it... that''s TO LEARN, now whether it''s gonna be good for the end user or not... anyways...

touche

1. 1
2. 2
Rutin
13
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633261
• Total Posts
3011127
• ### Who's Online (See full list)

There are no registered users currently online

×