Libgdx how to calculate elapsed time

I tried to calculate the elapsed time in libgdx by adding a delta value to my render method for my float value, which is measured in time since the playback state starts. The error or problem, as you want to call it, is that according to my calculations, the time displayed in this case is twice as much as real time. I tried dividing by 2, and I was very close to real time, which means that by adding delta to 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 real time fails above, and my question is why? EDIT: I use the screen, but it doesn't matter that I tried to use both the Gdx.graphics.getDeltaTime () and delta methods from the render method.

+4
source share
4 answers

LibGDX is still actually Java, just use standard methods to get the time. At the beginning of using 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 long.

As for why it doesn't work with the method you posted, I'm not sure.

EDIT: If your LibGDX project is not configured to operate at a fixed speed, changing the frame rate will result in a change in Gdx.graphics.getDeltaTime (), because the delta time is returned for the last frame updated,

+12
source

You can use wrapper TimeUtils.

Get current time:

long startTime = TimeUtils.millis();

Get the time elapsed since launch:

long elapsedTime = TimeUtils.timeSinceMillis(startTime);
+12

java-. , .

long startTime = System.currentTimeMillis();

, :

long elapsed time = System.currentTimeMillis() - startTime;

, , , , , libgdx , .

, , ... , 60 . , - 30 ...

, getRawDeltaTime() - .

: - 1000, , .

+3

TimeUtils . , . LibGdx .

:

public long time;
time = TimeUtils.nanoTime();

wiki:

TimeUtils " System.nanoTime() System.currentTimeMillis(). , !"

+2

Source: https://habr.com/ru/post/1542473/


All Articles