1
\$\begingroup\$

Say I have a Game class with an update loop that receives a time increment dt by parameter. I was wondering if it's better or customary to apply this dt before the loop starts or after the loop finishes.

class Game
{
    void Update(float dt); 
    float m_time; 
}

dt is the delta time from last update loop.

m_time is the total accumulated time since the game started running.

Option 1:

void Game::Update(float dt)
{
    m_time += dt;
    /*
    do stuff
    */
}
  • Pros: I think it's more clear.
  • Cons: No update loop is processed at m_time = 0. The very first update loop already has m_time = dt.

Option 2:

void Game::Update(float dt)
{
    /*
    do stuff
    */
    m_time += dt;
}
  • Pros: The update loop starts at m_time = 0.
  • Cons: I find it a bit weird that the time is updated after all the processing has finished.

Any advise on which option is better or preferred over the other?

\$\endgroup\$
3
  • \$\begingroup\$ What is dt and what is m_time? \$\endgroup\$
    – JPtheK9
    May 22 '15 at 8:48
  • \$\begingroup\$ Edit that into your question so I can take back the down vote. It's a good question and I feel bad for giving it -1. \$\endgroup\$
    – JPtheK9
    May 22 '15 at 9:15
  • \$\begingroup\$ I've improved variables descriptions in my original post. \$\endgroup\$
    – FerranMG
    May 22 '15 at 12:27
1
\$\begingroup\$

dt, which I'm assuming is the time between frames, should be added to m_time at the beginning of the frame so that m_time will be the time since the start of the game when you use it later on in the frame, assuming that's what m_time is.

\$\endgroup\$
2
  • \$\begingroup\$ Yeah, dt is the time delta between frames, and m_time, in this simple example I set, is obviously the accumulated time since the game started. From your answer, I imply that the game will never have an update loop with m_time = 0, which is what I find a bit weird. Can't this cause strange effects? For example, animations that don't start playing from the beginning. \$\endgroup\$
    – FerranMG
    May 22 '15 at 8:54
  • \$\begingroup\$ It's just better to be explicit about these things even though intuiting them is easy (but maybe not to someone who hasn't seen dt before). When adding dt before the frame processes, m_time will be 0 for the first frame processes. Naturally, dt should be 0 when the game starts and 0 + 0 = 0. Actually, dt is undefined for the first frame but in application, 0 works fine. \$\endgroup\$
    – JPtheK9
    May 22 '15 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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