Question

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;

  • If I set deltaTime as 0.07 statically, then the animation runs normally. But since the update() method runs as faster as it can, the length and therefore the speed of the ball varies from device to device.

  • If I don't touch deltaTime and run the program (deltaTime's are between 0.01 - 0.02 with my test devices) animation length and the speed of ball are same at different devices. But the animation is so SLOW!

What am I doing wrong?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top