1. ## bouncing a ball

hello friends..... i m a student ....n doing BS in computer science.....
well friends i have class project of bouncing a ball.... we r using OPENGL...
and i m not familiar with OPENGL....
my problem is that I dont know how to make gravity effects on the ball....
so i want help from u guys.....

2. i m waiting .....thanks in advance

3. lol - 2 minutes. That's got to be a new impatient record.

4. i don't understand??

5. Well, can you draw a ball at a given position? If so, then you're halfway there. You just need to change the position of the ball each frame (or time period or whatever). A simple way to do it goes like this:
Code:
```#define Y_ACCELERATION -2

int y_position = 100;
int y_velocity = 0;

while(/* ... */) {
/* ... */
y_velocity += Y_ACCELERATION;
y_position += y_velocity;

/* If the ball reaches the ground, reverse its velocity */
if(y_position <= 0) {
y_position = 0;
y_velocity *= -1;
}
}```
Using doubles for positions would probably be a good idea; you'll likely find that an acceleration of 2 doesn't work too well, and you need 2.5, or something.

What I've done there is treat the Y axis as vertically aligned, with positive values farther away from the ground. Then there's a position, which is acted upon by the velocity, which is acted upon in turn by the acceleration due to gravity.

Note that this is a really simple way of doing things. If you want a more accurate simulation you might have to use some integrals. I haven't really thought about it, because I really don't think that's what you need here.

6. thanks....if i will be having any problem then i will ask again.....

7. well i have a question ......does OPENGL provide any built in function to rotate the bouncing ball after the collision.......?????

8. What don't you understand? Be more specific.
1. To make the ball move, you simply draw it at a different position.
2. Gravity is a form of acceleration here, so you need to simulate acceleration of the ball.
3. Acceleration is just change in velocity, which is just change in position.
4. Just add acceleration to velocity to simulate it, and add velocity to position to simulate that.
5. Bouncing of the ball can be achieved by simply inverting the velocity when the ball hits the ground.

well i have a question ......does OPENGL provide any built in function to rotate the bouncing ball after the collision.......?????
I don't know very much about OpenGL, so I can't answer that question. You shouldn't need to rotate the ball, though (unless I'm missing something?), just negate the velocity. For example, if the ball was traveling at a velocity of -30 (negative is downwards), then just make it travel at +30 (upwards), and you have your bounce.
[/edit]

9. i m saying that ball spin after the collision .... suppose u throw a ball downstairs then after collision with the stairs the ball spins?.......and it also increase its acceleration...right
i want to bounce a ball with the spin also........

10. well ok ....
one more thing..... i dont want to move my bouncing ball comtinuously.......it should be decreasing its speed......so what should i do ?

i m saying that ball spin after the collision .... suppose u throw a ball downstairs then after collision with the stairs the ball spins?.......and it also increase its acceleration...right
i want to bounce a ball with the spin also........
Well, yes, if a ball is spinning and it bounces, the spin is reversed and the spin affects the trajectory. I imagine if a ball with a large horizontal velocity bounces, it might start spinning as well. You can try to simulate these effects if you want to . . . .

It sounds like you want more than just a simple ball bouncing up and down; do you want horizontal (or even 3D) movement? If so, then just keep track of the velocity on each axis, and apply acceleration to the vertical axis to simulate gravity. It's not too hard. The simulation won't be too interesting, though, unless you start it with an interesting velocity, or unless other forces (wind?) act on the ball.

Also, if you have surfaces at different angles, you won't be able to just invert an axis to simulate a ball bouncing off the edge; you'll need to use some trigonometry to figure out the resulting velocities after a collision.

well ok ....
one more thing..... i dont want to move my bouncing ball comtinuously.......it should be decreasing its speed......so what should i do ?
The obvious solution is to make bounces not 100% perfect. You could use, for example,
Code:
`y_velocity *= -0.9;`
to make a bouncing ball recover 90% of its energy.

12. thank u DWK.....
i will ask questions if i 'm having any trouble.....
thnks for ur help.....

13. If you don't need to simulate gravity, a simple euler integration would do -

Code:
```velocity += acceleration * dt;
position += velocity * dt;```
If you want to simulate gravity you'll have to do a bit more, basically youll have to use y = 1/2gt^2 + v0yt + C;

which is something like -

Code:
`  PositionY = .5f * gravity * time* time + VelocityY * time + StartPositionY;`

14. well spidey thanks for ur help .... but its a bounce ....... i mean i dont understand what u saying???
can u plz explain????

15. My explanation was based on advancing the simulation one "step" per frame. Spidey's using formulae that are based on time elapsed instead, which are a better idea (no dependency on frame rate). They're basic physics equations; Wikipedia has some information on them and I'm sure you can find other resources, too. Classical mechanics - Wikipedia, the free encyclopedia