Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gravity simulation

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 ?

like image 550
Vert Avatar asked Aug 10 '11 15:08

Vert


People also ask

Is there a gravity simulator?

Gravity Simulator lets you control one of the most basic forces in the universe. Launch stars and planets, and watch as gravitational patterns unfold. Conic sections, dancing spirals, spirographs, and plenty of utter chaos will appear before your eyes.

Is gravity a force?

Gravity is the force by which a planet or other body draws objects toward its center. The force of gravity keeps all of the planets in orbit around the sun.

Can you feel the gravity?

Since the force of gravity is not a contact force, it cannot be felt through contact. You can never feel the force of gravity pulling upon your body in the same way that you would feel a contact force.


1 Answers

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

like image 143
Yochai Timmer Avatar answered Oct 10 '22 23:10

Yochai Timmer