Some Physics for a pong like game

Started by
6 comments, last by dethsled 23 years, 10 months ago
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
Advertisement
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
If all these errors are so fatal, why am I still alive?
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...

if (Ball.X+Ball.Width >= RightPaddle.X-10 && (Ball.Y > Paddle.Y && Ball.Y < Paddle.Y + 15) && KEY_UP(ARROW_UP))
{
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=-..
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..
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...
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.
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement