Sign in to follow this  

Java whats wrong with my timelapse code?

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get the time lapse aka delta time between loops, to move an object by that x value. Right now I'm doing:
while(1 == 1) {
    lastTime = time;
    time = System.nanoTime();

    //supposed to move 5 pixels a second
    x+=5.0f*(float)(time-LastTime);
}




Except I'm getting some huge number instead, what am I doing wrong? thx

Share this post


Link to post
Share on other sites
I assume System.nanoTime() returns the time in nanoseconds, your code will move 5 pixels per nanosecond. You need to divide by 10^-9 to convert to seconds.

In addition, check that 'time' has a reasonable value before the loop starts (probably the current time). It may contain invalid data depending on where and when it is declared.

Share this post


Link to post
Share on other sites

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this