Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks Josh. I was expecting such a detailed answer. –  Vicky Jun 20 '13 at 3:28

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");
    }
share|improve this answer
2  
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 –  Roy T. Jun 19 '13 at 15:51
    
I know that. I meant that inside a single frame the Delta Time will always be the same. –  mobo Jun 19 '13 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.