View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Bouncing algorithm

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Slayer-X  Members

Posted 28 October 1999 - 07:51 PM

I know this probably sounds stupid, but I'm making a pong game as my first game using graphics. I like thinking about things and figuring them out, but I just need this thing done. I was wondering if anyone knew how to do the algorithm that bounces the ball off of the paddle and bounce at different angles without using a random angle or just plain angle reflection.

### #2logistix  Members

Posted 27 October 1999 - 03:51 PM

I wish I had a scanned of it because it's pretty goofy. I just found out about a year ago. If you could find an Atari manual for Pong or Breakout or probably even Combat they break it down.

They basically use a complety arbitrary system where it cycles thorough eight hits and depending on which hit it is and where on the paddle it hits, it shoots out at a predefined angle. I guess it's set up that way to keep things interesting. I always used to think that how fast I spun the controller determined how the ball bounced.

What really cracked me up is that they told you how the ball bounced in the instructions, as if by the time you're on the eighth level in Break-Out you're actually saying "this is the sixth step in the pattern, so I want to hit the ball here" Maybe they wanted to keep people from thinking the computer was cheating.

There are a few internet sites that might have scanned atari manuals, or if not, there's always flea markets.

### #3Gromit  Members

Posted 27 October 1999 - 05:51 PM

It's all pretty easy actually.. I won't give you all the details but here is a start.

First you have to have a structure for the ball with :

velx, vely, angle, speed..etc..

when the ball hits the paddle then you :

velx -= velx;
vely -= vely;
angle -= angle;

etc..

I'm tired and I may just be babbling, so if you still don't find someone to explain it to you better, then I could send you real a code example.

william_reiach@hotmail.com

### #4Anonymous Poster_Anonymous Poster_*  Guests

Posted 27 October 1999 - 06:07 PM

Ok,
A couple of the ways i have seen it done is to change the angle depending on were it is hit, so if it hits the center it bounces straight up, and the further left it goes the more to the left it bounces and the more to the right the further right it bounces. you could also add a tiny bit of randomness.

### #5Anonymous Poster_Anonymous Poster_*  Guests

Posted 27 October 1999 - 07:56 PM

Well, before I checked to see if I got any replies on it I came up with a way to do the bouncing. It's just like the Atari Pong game works. The closer to the center you get, the larger the reflection angle...and if you hit the center it bounces straight up. So far I just need to get the score displayed, the round number, the computer AI, the 2nd player controls and display, and the bouncing in. It's kind of a lot left...but I already have about 500 lines done...and that's about 3/4 of the way done. Thanx for your help.

### #6Auday  Members

Posted 28 October 1999 - 02:31 AM

If u want to base your game on real physics then u should use Gromits way , and may be if u want the edges of your paddle to be rounded ( which makes the game more interesting I guess ) you can use the angle between the speed vector of the ball and the tangent at the point of collision .
To make it more interesting its better to use floats for the position and speed of the ball and the paddles ( you can cast them to integers when you want the Pixel coordinates ) this way you can make the game much more real and also much more interesting .

### #7Anonymous Poster_Anonymous Poster_*  Guests

Posted 28 October 1999 - 07:42 PM

I'll probably rewrite the ball moving section of the game when I can actually get the ball moving, lol. For some reason I'm having problems getting the ball moving much. I probably screwed up on my math, today wasn't a good day to be doing the math for the ball, I wasn't thinking very mathematically. Here's the code I'm using the move the ball. ball_x is the ball's x position, ball_y is the ball's y position, brd_x is the player's board's x position, cmp_brd_x is the computer's board's x position, ball_move_x is how much to move the ball's x position, ball_move_y is how much to move the ball's y position, and g_score is the player's score. The game field is: upper left: (9, 80), upper right: (600, 80), lower left: (9, 440), lower right: (600, 440). I would really appreciate any help anyone could post about this. Here's the code:

** beginning of code **
void move_ball(int ball_x, int ball_y)
{
// check to see if the ball hits a side barrier
// if so bounce ball

if (ball_x > 600)
{
ball_move_x = - 10;
sound(1000);
}
else if (ball_x < 9)
{
ball_move_x + 10;
sound(1000);
}

// check to see if the ball has passed a paddle
// if so add or subtract points, and change round

else if ((ball_x > brd_x + 63 | | ball_x < brd_x) && ball_y > 440)
g_score[0] = g_score[0] - 20;
else if ((ball_x < cmp_brd_x + 63 | | ball_x < cmp_brd_x) && ball_y < 80)
g_score[0] = g_score[0] + 50;

// Player 1

// Left 22.5 degree

else if (ball_x > brd_x && ball_x < (brd_x + 9)
&& ball_y < 442)
{
ball_move_x = - 2.5;
ball_move_y = - 1;
sound(800);
}
// Left 45 degree
else if (ball_x > (brd_x + 9) && ball_x < (brd_x + 18)
&& ball_y < 442)
{
ball_move_x = -1;
ball_move_y = -1;
sound(800);
}
// Left 67.5 degree
else if (ball_x > (brd_x + 18) && ball_x < (brd_x + 27)
&& ball_y < 442)
{
ball_move_x = -.4;
ball_move_y = -1;
sound(800);
}
// Center 90 degree
else if (ball_x > (brd_x + 27) && ball_x < (brd_x + 34)
&& ball_y < 442)
{
ball_move_x = 0;
ball_move_y = -1;
sound(800);
}
// Right 67.5 degree
else if (ball_x > (brd_x + 34) && ball_x < (brd_x + 45)
&& ball_y < 442)
{
ball_move_x = .4;
ball_move_y = -1;
sound(800);
}
// Right 45 degree
else if (ball_x > (brd_x + 45) && ball_x < (brd_x + 54)
&& ball_y < 442)
{
ball_move_x = 1;
ball_move_y = -1;
sound(800);
}
// Right 22.5 degree
else if (ball_x > (brd_x + 54) && ball_x < (brd_x + 63)
&& ball_y < 442)
{
ball_move_x = 2.5;
ball_move_y = -1;
sound(800);
}

// Computer

// Left 22.5 degree

else if (ball_x > cmp_brd_x && ball_x < (cmp_brd_x + 9)
&& ball_y < 82)
{
ball_move_x = -2.5;
ball_move_y = 1;
sound(400);
}
// Left 45 degree
else if (ball_x > (cmp_brd_x + 9) && ball_x < (cmp_brd_x + 18) && ball_y < 82)
{
ball_move_x = -1;
ball_move_y = 1;
sound(400);
}
// Left 67.5 degree
else if (ball_x > (cmp_brd_x + 18) && ball_x < (cmp_brd_x + 27) && ball_y < 82)
{
ball_move_x = -.4;
ball_move_y = 1;
sound(400);
}
// Center 90 degree
else if (ball_x > (cmp_brd_x + 27) && ball_x < (cmp_brd_x + 34) && ball_y < 82)
{
ball_move_x = 0;
ball_move_y = 1;
sound(400);
}
// Right 67.5 degree
else if (ball_x > (cmp_brd_x + 34) && ball_x < (cmp_brd_x + 45) && ball_y < 82)
{
ball_move_x = .4;
ball_move_y = 1;
sound(400);
}
// Right 45 degree
else if (ball_x > (cmp_brd_x + 45) && ball_x < (cmp_brd_x + 54) && ball_y < 82)
{
ball_move_x = 1;
ball_move_y = 1;
sound(400);
}
// Right 22.5 degree
else if (ball_x > (cmp_brd_x + 54) && ball_x < (cmp_brd_x + 63) && ball_y < 82)
{
ball_move_x = 2.5;
ball_move_y = 1;
sound(400);
}
else
{
ball_x = ball_x + ball_move_x;
ball_y = ball_y + ball_move_y;
}

// Show score

outtextxy(61, 0, &score[0]);

// Stop bounce sound

nosound();

// Move ball

ball_x = ball_x + ball_move_x;
ball_y = ball_y + ball_move_y;

}
** end of code **
Thanx for any help.

### #8Anonymous Poster_Anonymous Poster_*  Guests

Posted 28 October 1999 - 07:50 PM

And I could use some help on my computer AI. I did this at the same time so it is probably all screwed up too. comp_board_x is the computer's board x position, cmp_brd_x is the global x position, comp_board_y is the computer's board y position, and cmp_brd_y is the global y position. Here it is:

** beginning of code **
comp_board_x = cmp_brd_x; // assign local to global comp board x
comp_board_y = cmp_brd_y; // assign local to global comp board y
int direction = 0; // the comp board direction

switch(rand()%1)
{
case 0: // don't change direction
break;

case 1: // change direction
{
switch(direction)
{
case 0: // move computer board left
{
comp_board_x = comp_board_x + 10;
} break;

case 1: // move computer board right
{
comp_board_x = comp_board_x - 10;
} break;

case 2: // keep board still
{
comp_board_x = comp_board_x;
} break;

default:break;
}
}
default:break;
}
if (comp_board_x < 5)
comp_board_x = comp_board_x + 1;
else if ((comp_board_x + 60) > 600)
comp_board_x = comp_board_x - 1;

line(comp_board_x, comp_board_y, (comp_board_x + 60), comp_board_y);

cmp_brd_x = comp_board_x; // change the computer's board x
cmp_brd_y = comp_board_y; // change the computer's board y

** end of code **

Sorry about the screwed up looking code, the lines got long and were wrapped in this window.

### #9Anonymous Poster_Anonymous Poster_*  Guests

Posted 28 October 1999 - 07:51 PM

Nevermind the screwed up code thing....I just noticed it didn't wrap it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.