8

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 ?

Vert
  • 81
  • 2
  • What happens if you just take the energy loss out of the equation? – redbmk Aug 10 '11 at 15:46
  • apart from Yochai's answer, you might consider setting `pos.y` to ground level in your collision case to avoid clipping errors – Tobias Kienzler Aug 10 '11 at 15:50
  • Without energy loss the point also stop bouncing after some period of time and it's very slow moving down. – Vert Aug 10 '11 at 16:24
  • 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 Aug 10 '11 at 16:25
  • @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 Aug 10 '11 at 16:37
  • @Tobias, I added an answer below. – rcollyer Aug 10 '11 at 20:51
  • You should perhaps take a look at JBullet. – KarlP Aug 11 '11 at 11:26

2 Answers2

6

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.)

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
2

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.

Community
  • 1
  • 1
rcollyer
  • 10,475
  • 4
  • 48
  • 75
  • +1 thanks for this answer. I think this is a good solution, too. In contrast to Vert's original collision implementation yours is for a "softer" collision where the body collides with the floor and gets to a rest there (like a pillow maybe) while Vert's version is a lossy bounce. Which one to choose depends of course on the physical behaviour desired. And you're right, including air friction would be overkill. – Tobias Kienzler Aug 11 '11 at 08:53
  • @Tobias, this is a lossy bounce also, just a little softer of a bounce. – rcollyer Aug 12 '11 at 14:45
  • @Vert, I never noticed when I posted it, but I dropped `energyloss` from my result for `delta_v`. It's fixed now. – rcollyer Sep 13 '11 at 03:30
  • So is v_initial the velocity of the object during collision with the ground? – Mike Glaz Feb 21 '17 at 15:16
  • @mikeglaz this made sense when I wrote it 5 years ago, but it isn't quite at the moment. I'm going to need to think about it a bit. – rcollyer Feb 21 '17 at 15:57