Skip to main content
gamedev.net
🔒 Locked

jumping ball

Started by ehmdjii 6 replies 2.3k views
Original Post
ehmdjii
ehmdjii
hello, this is probably a piece of cake for you guys, but i just cant manage to get the simulation of a jumping ball correct. this is what i do each frame (assume a constant framerate, so there is not timing necessary). gravity is (0,-1,0) force += gravity; force += 0.99; ball.y += force; if (ball.y < 0) { ball.y = -ball.y; force = -force; } however this doesnt seem to work, because when the height is becoming very small, the ball seems to jump higher than it was falling.
Sneftel
Sneftel
Quote:
force += 0.99;
Huh?

BTW, I think you mean "velocity", not "force". The force is constant, and equal to gravity.
ehmdjii
ehmdjii
ok, call force velocity then.
the += 0.99 part is there to simulate somthing like air resistance.
Sneftel
Sneftel
Quote:
Original post by ehmdjii
ok, call force velocity then.
the += 0.99 part is there to simulate somthing like air resistance.

Really? So if my velocity is 1.0 this frame, it should be 1.99 the next frame? and 2.98 the frame after that?
Ezbez
Ezbez
Air resistance has a pretty minimal effect on most bouncing balls. Perhaps what you should be doing to create that effect where the bounces get smaller and smaller each time is to model some loss of velocity during each bounce. That is, when you reverse the velocity (or "force" as you call it) when the ball is below the ground, also multiply it by some value, say .6. This will mean that the ball looses a lot of velocity every time it hits the floor. This is physically pretty accurate.

Also, air resistance is proportional to the speed at which the object is moving, relative to the air. If something is still, does it make sense for air resistance to push it upwards? Certainly not. Instead, air resistance pushes in the direction opposite to where the object is heading. If the object is going down, air resistance is pulling it up. If the ball is going up, air resistance is pulling it down. In short, the air resistance slows it down, rather than adding something to it. To do this, you generally multiply the velocity by a number smaller than one.
ehmdjii
ehmdjii
ok im sorry that was a typo

the line force += 0.99; should be force *= 0.99;
ehmdjii
ehmdjii
ezbez, yes later on i will also reduce the bouncing. but for now lets assume a perfect bounce.

so, does anyone know where the problem is with my code? thanks!
Dirk Gregorius
Dirk Gregorius
First you need to choose an integrator. Let's use a semi-implicit Euler for your example here. We initialize the ball at some height h with a ball radius r and an initial velocity of v. This mean the ball comes in contact with the ground at y_ground = 0 if its vertical position y < r

The general procedure goes something like this then:

// Initializey = h;v = 0;for ( n simulation steps )  {  // Integrate velocity  v = v + g * dt;  // Integrate position  y = y + v * dt  // Collide  if ( y < r )    v = -e * v;  }



That's basically everything. e is the restitution and can be chosen between 0 and 1. For e = 0 you get what is called a plastic collision and if e = 1 you get what is called an elastic collison. For the gravity g use something like g = -10.0f and you should use a timestep of 60 Hz.

Topic Locked

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

Sign in to reply to this topic.