# Thread: How to create a bouncing ball?

1. ## How to create a bouncing ball?

Hi,

I am trying to make a well known game (I forget the name) where you control a rectangle 'racket' at the bottom of the screen, move the racket left and right and deflect a ball up the screen to hit and destroy blocks.

Well I'm stuck with how to keep the ball bouncing around the screen:

http://img169.imagevenue.com/img.php...122_1099lo.jpg

In the image I have linked to above I've wrote some if statements to dectect when the ball has reached the side of the screen, at which point I plan to call a function to move the ball in it's deflected angle, but :

A matches E, B matches D, C matches G and D matches B.

The only difference is their preceeding states (the direction of the ball before it hits the edge) so I figure I need to use this in combination with the if statement, but I am unsure how.

I considered using a switch statement, but as that only has 1 argument, thought this won't work.

So basically I am looking for some advice on how best to achieve a continusously bouncing ball around the screen.

( I am aware I have not yet addressed balls which hit the corners exactly, ie: 0,0)

If anyone can offer me some advice, I would be most appreciative.

Thanks!

2. So, you are looking at 2D collision detection, right? That should be possible to find with google.

--
Mats

3. No, I'm wanting to achieve a continusously bouncing ball around the screen, I'm not yet looking at that.

I am at an internet cafe as I have no intenet at home so hoped I could ask here and check back later in the day in the hope for some advice.

So, basically I'm looking for advice on how to choose the if statement to execute - how to include the balls previous direction/state so as to execute the right option next, so that as a whole there are no matching options (previous state + option).

4. Depends on how you want to do it. If you want to do it in an ms-dos console search for how to get the coordinates of the cursor in the screen. If you want to do it on a win32 window there's an example on how to do it in forger's win32 tutorial... http://www.winprog.org/tutorial/ Or more specifically http://www.winprog.org/tutorial/animation.html

5. Well, the colision detection is just a comparison like you suggest. There should just be two checks for each dimension (X, Y) - something like
Code:
`ball.x <= left || ball.x >= right || ball.y >= top || ball.y <= bottom`
.
Then you need to reflect the incoming angle as the bounce angle. A very quick think about it makes me thing that if dx and dy is the movement per timeunit, I'm pretty sure that you just reverse the direction of the side you hit (so if you hit on the x axis, dx = -dx).

--
Mats

6. >> ball.x <= left || ball.x >= right || ball.y >= top || ball.y <= bottom

Depends where the .y and .x coordinates are taken from. You might need to include a ball.height and a ball.width in those calculations too.

7. Well, a simple bouncing algorithm is actually very easy.

First, declare two variables for the x and y velocity of the ball, and multiply it by -1 each time the ball hits the edge of the screen.

int xVel = 1;
int yVel = 1;

Your game update function should contain something like this (X and Y are the ball coordinates):

X += xVel;
Y += yVel;

if(X < 0 || X > SCREEN_WIDTH)
xVel *= -1;

if(Y < 0 || Y > SCREEN_HEIGHT)
yVel *= -1;

Well, that's all.

8. Isn't that what I said?

And xVel *= -1 is the same as xVel = -xVel;

--
Mats