# Bouncing Ball Algorithm

• 04-01-2003
Stan100
Bouncing Ball Algorithm
I've been playing around with WriteConsoleOutput() and other CHAR_INFO, SMALL_RECT stuff, and I soon want to be making a Pong clone. I try to make a ball bounce diagnally, but when it gets to the top it messes up. I don't care about my code, or fixing it, especially since it's just a driver program. But does anyone have a good ball algorithm(sp?):rolleyes:
• 04-01-2003
ichijoji
I wrote a couple of bouncing ball/paddle apps a little while ago.
They should still be available here.
• 04-01-2003
funkydude9
Have you tried looking at the code in my game?
• 04-01-2003
ichijoji
The best ball bouncing system that I've found goes like this:
Code:

```struct ballt {     int x, y, dx, dy,radius; }ball; //in main... ball.radius = 10; ball.x = (rand() % (SCREEN_W - ball.radius * 4)) + ball.radius * 2; ball.y = (rand() % (SCREEN_H - ball.radius * 4)) + ball.radius * 2; ball.dx = rand() % 2 * 2 - 1; //-1 or 1 ball.dy = rand() % 2 * 2 - 1; circlefill(buffer,ball.x,ball.y,ball.radius,15); void move_ball() {     ball.x += ball.dx;     ball.y += ball.dy;     if (ball.y <= ball.radius || ball.y >= SCREEN_H - ball.radius)         ball.dy *= -1;     if (ball.x <= ball.radius || ball.y >= SCREEN_W - ball.radius)         ball.dx *= -1; }```
yeah? worked for me
• 04-01-2003
Silvercord
Quote:

Bouncing Ball Algorithm
I would expect a really good bouncing ball algorithm to use a quadratic equation
• 04-01-2003
ichijoji
The appeal of this system is collision with the paddle:
Code:

```if (ball.x <= paddle.width && ball.y >= paddle.y     && ball.y <= paddle.y + paddle.height) {     ball.dx *= -1;     ball.dy = int((float(ball.y - paddle.y + paddle.height / 2) /                   float(paddle.height / 2)) * 3.0); }```
see? easy.
• 04-02-2003
Stan100
Sorry...
Quote:

I would expect a really good bouncing ball algorithm to use a quadratic equation
struct ballt {

Sorry, this is my fault. You see, I'm making a simple ball test. My "ball" is really a char(219) and I just want it to bounce diagnally. Think of pong, with no paddles. I just couldn't do that. Does anyone have an algorithm (am I spelling that right?) for a simple char(219) bouncing "ball". :rolleyes:
• 04-02-2003
Silvercord
Well I'm not going to continue to say too much because I really think this is something you can figure out on your own. I am just going to say that when things bounce, they don't just go at 90 degrees (sometimes it will, it depends on what angle it forms with the surface you are colliding against).

EDIT: if you don't get what I just said, I'll draw a picture, but only if you care enough to ask.
• 04-03-2003
confuted
things bounce off of a wall at the same angle that they collided...so to confuse you, the vector for the ball's approach to the wall mirrored over the wall's normal is the vector of the direction it will leave (pointed the wrong direction, but that's okay)

Code:

``` \    / a\  /b __\/___```
angle a= angle b
• 04-03-2003
frenchfry164
unless, of course, there is some english on the ball
• 04-06-2003
Stan100
Here's an example of what happens when I try:
EDIT: My beutiful Ascii art did not come out right

If it is going up and right, and hits the north part of the screen, it should go down and right (in the most simple case). Well mine hits the top and come back going left and down
• 04-06-2003
Perspective
Quote:

Originally posted by Stan100
Here's an example of what happens when I try:
EDIT: My beutiful Ascii art did not come out right

If it is going up and right, and hits the north part of the screen, it should go down and right (in the most simple case). Well mine hits the top and come back going left and down

if this is 2D ( i assume it is)

you are reflecting both the y and x. that is why it comes back left and down. in general, if it hits the top or bottom, just reflect the y, if it hits the left or right, just reflect the x.
• 04-06-2003
frenchfry164
Quote:

I would expect a really good bouncing ball algorithm to use a quadratic equation
Wouldn't an absolute value equation serve better for this use than a quadratic equation?