XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Problem with GameTime - inaccuracy?

Last post 04-25-2008 12:38 PM by Jason Maskell. 5 replies.
  • 04-24-2008 7:27 PM

    Problem with GameTime - inaccuracy?

    I'm probably doing something wrong here, but when doing some time based effects on our current game I noticed that 5000ms doesn't appear to be equal to 5 seconds in the real world. Here's a bit of code (code snippet thing STILL doesn't work?)

            private int hackyCount = 0;
            public override void Update(GameTime gameTime)
            {
                base.Update(gameTime);

                hackyCount += gameTime.ElapsedGameTime.Milliseconds;

                if (hackyCount>5000)
                {
                    hackyCount = hackyCount - 5000;
                    TraceThingy.TraceInformation("Timer!");
                }
            }

    The log is showing that going off every 2-3 seconds, which is straight up wrong. Even allowing for log lag, it should be slower, not faster. The visual effects that should be going off every X seconds are too fast as well.

    Other things in the engine that are depending on msec appear to be going too fast as well. I've never had this issue with XNA before, but the last time I used it was GS 1.0 Refresh.

    Any help here? This is the top level update loop, and it should be getting an accurate gametime if anything does.


  • 04-24-2008 8:01 PM In reply to

    Re: Problem with GameTime - inaccuracy?

    Two problems:

    - You want TimeSpan.TotalMilliseconds, not TimeSpan.Milliseconds.

    - You should store this as a float, not an integer1. At 60 fps, each frame is 16.6667 milliseconds. Rounding that to integer will introduce significant error.
    XNA Framework Developer - blog - homepage
  • 04-24-2008 9:58 PM In reply to

    Re: Problem with GameTime - inaccuracy?

    Shawn Hargreaves:
    Two problems:

    - You want TimeSpan.TotalMilliseconds, not TimeSpan.Milliseconds.

    - You should store this as a float, not an integer1. At 60 fps, each frame is 16.6667 milliseconds. Rounding that to integer will introduce significant error.


    Thanks Shawn, that's helpful. Seems a bit counter-intuitive though, the TotalMilliseconds part - does that indicate total msec since the timer started? Milliseconds appears to be 16 each update call.

    I'll try that when I'm at work tomorrow, I'm sure that'll sort it. Cheers.
  • 04-24-2008 11:16 PM In reply to

    Re: Problem with GameTime - inaccuracy?

    The difference is that Milliseconds is an integer counter whereas the TotalMilliseconds is a double (or float; I forget). So with TotalMilliseconds you get the fractional part of the time as well which avoids the rounding issues.
  • 04-25-2008 1:03 AM In reply to

    Re: Problem with GameTime - inaccuracy?

    Answer

    That *is* a difference but the main one is that milliseconds is just the millisecond part of the time and totalmilliseconds is the time converted to milliseconds. I have lost track of how many times this one has burnt me - I always forget.

    so

    MilliSeconds(10.274) =  274

    TotalMilliSeconds(10.274) = 10274

    MilliSeconds(13.274) = 274

    TotalMilliSeconds(13.274) = 13274



    The ZBuffer News and information for XNA

    Please read the forum FAQs - Bug reporting
  • 04-25-2008 12:38 PM In reply to

    Re: Problem with GameTime - inaccuracy?

    Well, that's got it. It was a stupid error on my part, the main update was being called explicitly as well as by XNA. Durr.
Page 1 of 1 (6 items) Previous Next