Jump to content
  • Advertisement
Sign in to follow this  
skinx

bouncing ball

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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=0
vel.y = -abs(vel.y)
}

Share this post


Link to post
Share on other sites
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=0
vel.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.

Share this post


Link to post
Share on other sites
thanks guys for the code lines.. but i dun understand it really.. erm.. where does those lines be placed? at the animation part?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!