Murat Nafiz
Murat Nafiz

Reputation: 1438

Implementing gravity to projectile - delta time issue

I'm trying to implement a simple projectile motion in Android (with openGL). And I want to add gravity to my world to simulate a ball's dropping realistically.

I simply update my renderer with a delta time which is calculated by:

    float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
    startTime = System.nanoTime();

    screen.update(deltaTime);

In my screen.update(deltaTime) method:

    if (isballMoving) {
        golfBall.updateLocationAndVelocity(deltaTime);
    }

And in golfBall.updateLocationAndVelocity(deltaTime) method:

public final static double G = -9.81;

double vz0 = getVZ0(); // Gets initial velocity(z)
double z0 = getZ0();  // Gets initial height
double time = getS();  // gets total time from act begin
double vz = vz0 + G * deltaTime;  // calculate new velocity(z)
double z = z0 - vz0 * deltaTime- 0.5 * G * deltaTime* deltaTime; // calculate new position

time = time + deltaTime; //  Update time
setS(time); //set new total time

Now here is the problem;

What am I doing wrong?

Upvotes: 1

Views: 949

Answers (1)

Luis Cruz
Luis Cruz

Reputation: 187

Increase the amount you change per frame? This should make your animation 5x faster:

double vz = vz0 + G * deltaTime * 5;  // calculate new velocity(z)
double z = z0 - vz0 * deltaTime- 0.5 * G * deltaTime* deltaTime * 5; // calculate new position

Upvotes: 1

Related Questions