8
votes

I want to simulate a free fall and a collision with the ground (for example a bouncing ball). The object will fall in a vacuum - an air resistance can be omitted. A collision with the ground should causes some energy loss so finally the object will stop moving. I use JOGL to render a point which is my falling object. A gravity is constant (-9.8 m/s^2).

I found an euler method to calculate a new position of the point:

deltaTime = currentTime - previousTime;
vel += acc * deltaTime;
pos += vel * deltaTime;

but I'm doing something wrong. The point bounces a few times and then it's moving down (very slow).

Here is a pseudocode (initial pos = (0.0f, 2.0f, 0.0f), initial vel(0.0f, 0.0f, 0.0f), gravity = -9.8f):

display()
{
     calculateDeltaTime();
     velocity.y += gravity * deltaTime;
     pos.y += velocity.y * deltaTime;

     if(pos.y < -2.0f) //a collision with the ground
     {
        velocity.y = velocity.y * energyLoss * -1.0f;
     }

}

What is the best way to achieve a realistic effect ? How the euler method refer to the constant acceleration equations ?

2
What happens if you just take the energy loss out of the equation? - redbmk
apart from Yochai's answer, you might consider setting pos.y to ground level in your collision case to avoid clipping errors - Tobias Kienzler
Without energy loss the point also stop bouncing after some period of time and it's very slow moving down. - Vert
In my, now deleted, answer, I suggested that your bleeding your energy off to fast. Upon reflection, and further working the math, I realized that if you simply change it to velocity.y -= velocity.y*energyloss/2 you'd be correct. - rcollyer
@rcollyer: is that inside or outside the collision block? i.e. do you suggest friction [instead of] or [additionally to] a lossy bounce? - Tobias Kienzler

2 Answers

6
votes

Because floating points dont round-up nicely, you'll never get at a velocity that's actually 0. You'd probably get something like -0.00000000000001 or something.

you need to to make it 0.0 when it's close enough. (define some delta.)

2
votes

To expand upon my comment above, and to answer Tobias, I'll add a complete answer here.

Upon initial inspection, I determined that you were bleeding off velocity to fast. Simply put, the relationship between kinetic energy and velocity is E = m v^2 /2, so after taking the derivative with respect to velocity you get

delta_E = m v delta_v

Then, depending on how energyloss is defined, you can establish the relationship between delta_E and energyloss. For instance, in most cases energyloss = delta_E/E_initial, then the above relationship can be simplified as

delta_v = energyloss*v_initial / 2

This is assuming that the time interval is small allowing you to replace v in the first equation with v_initial, so you should be able to get away with it for what your doing. To be clear, delta_v is subtracted from velocity.y inside your collision block instead of what you have.

As to the question of adding air-resistance or not, the answer is it depends. For small initial drop heights, it won't matter, but it can start to matter with smaller energy losses due to bounce and higher drop points. For a 1 gram, 1 inch (2.54 cm) diameter, smooth sphere, I plotted time difference between with and without air friction vs. drop height:

difference in time with and without air-drag vs. drop height

For low energy loss materials (80 - 90+ % energy retained), I'd consider adding it in for 10 meter, and higher, drop heights. But, if the drops are under 2 - 3 meters, I wouldn't bother.

If anyone wants the calculations, I'll share them.