Skip to main content
gamedev.net
🔒 Locked

bouncing ball

Started by skinx 4 replies 650+ views
Original Post
skinx
skinx
hi all, i would like to enquire and ask help from u guys out there.. erm.. how to create a bouncing ball using openGL with the acceleration decreases when the ball hit the ground?
Zakwayda
Zakwayda
The 'bouncing' part doesn't have anything to do with OpenGL, and any solution you choose should work regardless of API.

It's a slightly different problem, but I posted some 'bouncing' code in this thread, which you may be able to adapt to your needs.
iMalc
iMalc
If you are talking about realistically animating a bouncing ball, then the only acceleration that should come into play is gravity, and gravity should be constant.
If you want your ball to bounce up to less than the height it reached on the last bounce, then the solution for that is to simulate air friction by constantly multiplying the balls velocity by a number very close to, but just under, 1.
When the ball hits the ground you simply reverse it's vertical velocity

Here's some code for a bouncing ball...
const int GROUND_HEIGHT = 1;const int GRAVITY = 0.098;const int AIR_FRICTION_MULTIPLIER = 0.999;if (pos.y < GROUND_HEIGHT)    vel.y = -abs(vel.y)vel.y += GRAVITY;vel *= AIR_FRICTION_MULTIPLIER;pos += vel;renderTheBall(pos);
_Madman_
_Madman_
Quote:

if (pos.y < GROUND_HEIGHT)
vel.y = -abs(vel.y)


I had issue with this some time ago. If time delta is a large enough, ball stucks somwhere in plane.

1)
\/
-----------

2)
----\/-----

3)
-----------
\/

case 1 is when ball is falling down or flying up and everything is ok.

but cases 2 and 3 can occour, when pos.y is below plane. Imagine that ball is falling and test stops in case 3 because of large timestep, velocity is reversed but next timestep is not 1, but 2 instead (because of friction). Velocity gets reversed again and you get case 3 again, prolly even further below plane.

I found a simple way to fix this by adding 1 more line:
if (pos.y < GROUND_HEIGHT){pos.x=0vel.y = -abs(vel.y)}
______Madman
iMalc
iMalc
Quote:
Original post by _Madman_
Quote:

if (pos.y < GROUND_HEIGHT)
vel.y = -abs(vel.y)


I had issue with this some time ago. If time delta is a large enough, ball stucks somwhere in plane.

1)
\/
-----------

2)
----\/-----

3)
-----------
\/

case 1 is when ball is falling down or flying up and everything is ok.

but cases 2 and 3 can occour, when pos.y is below plane. Imagine that ball is falling and test stops in case 3 because of large timestep, velocity is reversed but next timestep is not 1, but 2 instead (because of friction). Velocity gets reversed again and you get case 3 again, prolly even further below plane.

I found a simple way to fix this by adding 1 more line:
if (pos.y < GROUND_HEIGHT){pos.x=0vel.y = -abs(vel.y)}
That problem will happen if you use:
vel.y = -vel.y;
You'll see that I posted
vel.y = -abs(vel.y);
which will only ever convert a positive into a negative. A negative will stay negative. Zeroing the x position must be a mistake. Perhaps you meant to write:
pos.y = GROUND_HEIGHT;
however, to do it absolutely properly you'd make the ball travel through a vector composed of the portion up until the collision, plus the reflected portion of the distance remaining. Here's the whole thing:
const float GROUND_HEIGHT = 240;const float CEILING_HEIGHT = 0;const float LEFT_SIDE = 0;const float RIGHT_SIDE = 320;const float GRAVITY = 0.098;const float AIR_FRICTION_MULTIPLIER = 0.999;vel.y += GRAVITY;vel *= AIR_FRICTION_MULTIPLIER;pos += vel;if (pos.y > GROUND_HEIGHT){    vel.y = -abs(vel.y);    pos.y = 2 * GROUND_HEIGHT - pos.y;}else if (pos.y < CEILING_HEIGHT){    vel.y = abs(vel.y);    pos.y = 2 * CEILING_HEIGHT - pos.y;}if (pos.x > RIGHT_SIDE){    vel.x = -abs(vel.x);    pos.x = 2 * RIGHT_SIDE - pos.x;}else if (pos.x < LEFT_SIDE){    vel.x = abs(vel.x);    pos.x = 2 * LEFT_SIDE - pos.x;}renderTheBall(pos);
Which reflects it off the ground by the distance it travlled into the ground, thus eliminating any unwanted sliding effects to make it look more natural. You still have timesteps to factor in if you have variable timesteps of course.
skinx
skinx
thanks guys for the code lines.. but i dun understand it really.. erm.. where does those lines be placed? at the animation part?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.