1
\$\begingroup\$

Generally, we associate a "state time" with our sprites. Libgdx provides a method to do so:

float deltaTime = Gdx.graphics.getDeltaTime();

However, in the main game loop, we have a delta time as a parameter in render() methods. Suppose my player has states like Idle, WalkLeft, WalkRight, Jump, Hit, and Die. I use the delta time to know how much total time has elapsed within a state:

public void setState (int state){
    float deltaTime = Gdx.graphics.getDeltaTime();
    this.state = state;
    this.stateTime += deltaTime;
}

Is it proper to write this, or I should take the parameter for delta time from the method that calls it?

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

I think you're conflating render and logic updates. The delta returned by the Gdx.graphics object is an elapsed time value that probably should only be used for render updates, but the state an object is in is more of a game logic issue, and you generally want these things handled on different timers.

This is because if you pause your game logic (by stopping the game logic timer, so all delta times for logic are zero), you may still want some aspects of your rendering (such as, perhaps, HUD animations) to continue.

So first I would recommend you divorce your graphics and logic timers. Then, I would recommend pushing the delta time through to every object that needs it as a parameter. You can still your delta time by calling Gdx.graphics.getDeltaTime, but do it only once at the top-most update function, and push the resulting delta down through every game or render object's own update methods. This helps decouple them from the global instance, allowing you to do better global control of timing-related effects (like slowing everything down, or pausing things as described above).

I'd also recommend moving the update of the elapsed time into an actual update() method, and not doing it in setState. It makes very little sense that setting the state would also advance the object's simulation time, and it means you can't call setState more than once in an update interval without artificially pushing the clock for that object forward.

\$\endgroup\$
0
-3
\$\begingroup\$

Gdx.graphics.getDeltaTime() returns the same delta for each frame.

Running this code will never return "Delta different" even though it does get called at different times.

    float delta = Gdx.graphics.getDeltaTime();

    for(int i = 0; i <= 1000000 ; i++)
    {
        if(delta != Gdx.graphics.getDeltaTime()) Gdx.app.log("Test", "Delta different");
    }
\$\endgroup\$
2
  • 2
    \$\begingroup\$ That is because it gives the delta time for the last frame. Next frame it will give you a different delta (if you're not running in fixed time-step). As with any timestep related question see : gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$
    – Roy T.
    Jun 19, 2013 at 15:51
  • \$\begingroup\$ I know that. I meant that inside a single frame the Delta Time will always be the same. \$\endgroup\$
    – mobo
    Jun 19, 2013 at 16:33

You must log in to answer this question.

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