implementing gravity to projectile - delta time issue
Posted
by
Murat Nafiz
on Stack Overflow
See other posts from Stack Overflow
or by Murat Nafiz
Published on 2012-11-05T22:53:57Z
Indexed on
2012/11/05
23:00 UTC
Read the original article
Hit count: 257
android
|projectile
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
as0.07
statically, 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
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?
© Stack Overflow or respective owner