2
\$\begingroup\$

I search for methods to create a stopwatch on libgdx and came across with this:

public void trigger(float delta){

    playTime += delta;
    playTimeRounded = ((double)Math.round(playTime * 100) / 100);
    Gdx.app.log("deltaTime", delta + "");
}

But its not accurate and its much faster. How can i create an accurate stopwatch?

\$\endgroup\$
6
  • \$\begingroup\$ Add more information about how you call trigger function. \$\endgroup\$ Dec 19, 2014 at 13:45
  • \$\begingroup\$ it is triggered at the render method. \$\endgroup\$ Dec 19, 2014 at 13:50
  • \$\begingroup\$ As I understand, you call trigger from render method something like this trigger( Gdx.graphics.getDeltaTime() ). In accordance with documentation getDeltaTime returns smoothed delta time over n frames. So, you should change Gdx.graphics.getDeltaTime() to Gdx.graphics.getRawDeltaTime(). \$\endgroup\$ Dec 19, 2014 at 14:15
  • \$\begingroup\$ Nope. Still come up with the same result. The getDeltaTime() and getRawDeltaTime() returns the same result. \$\endgroup\$ Dec 19, 2014 at 14:22
  • \$\begingroup\$ How are you seeing the result? Do you have a text displayed on screen? \$\endgroup\$
    – felipe
    Dec 19, 2014 at 14:46

3 Answers 3

1
\$\begingroup\$

When you accumulate deltas you are also accumulating rounding errors.

Instead, record the system time the game started and compare it to the system time now.

Do not use time-of-day as this can get adjusted by internet-based time-of-day clock synchronization and other things.

Different OSes have different names for those clocks.

On windows you can use GetTickCount64 for this ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms724411%28v=vs.85%29.aspx )

On linux you can use clock_gettime with CLOCK_MONOTIONIC (http://man7.org/linux/man-pages/man3/clock_gettime.3.html )

On OSX you can use clock_get_system_microtime ( https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/services/services.html )

For Java use System.nanoTime()

\$\endgroup\$
0
\$\begingroup\$

Maybe your delta parameter has a wrong value when called. I prefer to use Gdx.graphics.getDeltaTime() instead.

float playTime = 0;
float playTimeRounded;

void AnyUpdateMethod()
{
    playTime += Gdx.graphics.getDeltaTime();
    playTimeRounded = Math.round(playTime * 100) / 100.0f;
    Gdx.app.log("playTimeRounded", playTimeRounded + "");
}
\$\endgroup\$
1
  • \$\begingroup\$ I already used Gdx.graphics.getDeltaTime(); and still come up with the same result. \$\endgroup\$ Dec 19, 2014 at 14:21
0
\$\begingroup\$

The deltaTime is the amount of time which passes between drawing two consecutive frames. It is used for updating things on your screen that change in time, for example when moving your objects around, you would want to apply the deltaTime to the object's speed, in order to have a consistent movement across devices. When calculating playtime though, you should only use the system time, increase it, decrease it normally, without applying any delta to it. You would have to store your playTime value as a long, since you are going to add to it the value of TimeUtils.millis(). Just forget about the deltaTime in this case.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .