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
deltaTimeas0.07statically, then the animation runs normally. But since theupdate()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
deltaTimeand 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?
Increase the amount you change per frame? This should make your animation 5x faster: