Libgdx how to calculate elapsed time

java, libgdx, timer

Solution

LibGDX is still Java at heart, just use the standard methods for getting the time. At the start of the application use

startTime = System.currentTimeMillis();

then in the render you can do

System.out.println("Time elapsed in seconds = " + ((System.currentTimeMillis() - startTime) / 1000));

Make sure startTime is declared as a long.

As to why it's not working with the method you posted, I'm not sure.

EDIT: If your LibGDX project isn't set to run at a fixed rate, then the frame-rate change will cause a change in Gdx.graphics.getDeltaTime(), because the delta time is returned for the last frame updated.

Problem

I was trying to calculate elapsed time in libgdx by adding in the render method the value of delta to my float value which is the time i measure since play state starts. The bug or problem, how you want to call it, is that by my calculations, the time displayed doing this is two times the real time. I tried to divide by 2 and I was pretty close to the real time, which means that by adding delta every render call I don't get real time in seconds. Why is this happening? ``` private float time=0; public void render () { time +=Gdx.graphics.getDeltaTime(); } ``` the above doesn't get the real time and my question is why? EDIT: i am using a screen but it doesn't matter i tried both Gdx.graphics.getDeltaTime() and delta argument from render method.

Original source

Related problems