#### Archived

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

# bouncing ball!!!!1

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

## Recommended Posts

I want to prgram a bouncing ball. I have already programmed the ball but now i am stuck. I dont know how to move the ball down towards the bottom of the window and after bounce how is it going to move upwards ????????? please help me with the easiest solution!!!! Thank you.

##### Share on other sites
How do you want the ball to bounce? Do you want it to model a real bouncing ball, or do you just want the thing to bounce elastically off the sides of the screen?

##### Share on other sites
if you want a simple pong type ball you basically need a x and y direction. so:
int xdir,ydir;

so lets say that the ball is moving up and to the left:

xdir=2;
ydir=2;

lets say that the ball hits the left or right side of the window. we need to reverse the direction:

xdir*=-1;

now lets say that the ball hits the top or bottom of the window. again, we need to reverse the direction:

ydir*=-1;

so basically the code is somthing like this:
   if xdir<widowLeft or xdir>windowRight{ xdir*=-1;}if ydir<widowTop or ydir>windowBottom{ ydir*=-1;}

pretty simple stuff i you understand it.

edit: wow... i'm retarded today...
[edited by - PmanC on February 22, 2004 1:34:06 AM]

[edited by - PmanC on February 22, 2004 1:37:41 AM]

[edited by - PmanC on February 22, 2004 1:38:33 AM]

[edited by - PmanC on February 22, 2004 1:43:12 AM]

##### Share on other sites
I would really like the ball to move elastically off the sides of the screen.

##### Share on other sites
Hi
Take this code and When ever it hits on of the sides Multiply xdir with say something like 1.1
Example:

if (xdirwindowRight) xdir =xdir * -1.1f;
if (ydirwindowBottom) ydir = ydir *-1.1f;

and then every frame apply gravity:
Const int gravity = 0.1;

GLDrawScene()
{
...
...

x -=gravity;
}

if however you want the ball to look like a cartoon elastic you will need some complex animation where its Horizontal Size gets bigger while its Vertical Size Shrinks ect.. and hold it there until its say 50% of its original size either bigger or smaller and the invert direction

1. 1
2. 2
3. 3
4. 4
5. 5

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

• Total Topics
632911
• Total Posts
3009186
• ### Who's Online (See full list)

There are no registered users currently online

×