Question

I'm trying to write a fixed timestep.

        Stopwatch timer = Stopwatch.StartNew();
        TimeSpan dt = TimeSpan.FromSeconds(1/50);
        TimeSpan elapsedTime = TimeSpan.Zero;

        while(window.IsOpen())
        {
            timer.Restart();
            elapsedTime = timer.Elapsed;

            while(elapsedTime > dt)
            {
                window.DispatchEvents();

                elapsedTime -= dt;

                gameObject.FixedUpdate(deltaTime goes here as double);
            }
         }

I want to pass dt as argument to FixedUpdate as a double, is there a way to convert it somehow?

I'm also not quite sure about this line TimeSpan dt = TimeSpan.FromSeconds(1/50); Basically I want dt to hold 1/50th of a second.

Était-ce utile?

La solution 2

Without knowing the internals of FixedUpdate, you probably want TotalMilliseconds.

Stopwatch timer = Stopwatch.StartNew();
TimeSpan dt = TimeSpan.FromSeconds(1.0/50.0);
TimeSpan elapsedTime = TimeSpan.Zero;
while(window.IsOpen())
{
    timer.Restart();
    elapsedTime = timer.Elapsed;
    while(elapsedTime > dt)
    {
        window.DispatchEvents();
        elapsedTime -= dt;
        gameObject.FixedUpdate(dt.TotalMilliseconds);
    }
 }

Autres conseils

Regarding this part TimeSpan.FromSeconds(1/50). TimeSpan.FromSeconds accepts double, 1/50 is int(int/int gives int w/o floating point part), and its value is equal to 0, when passed to method this values gets implicitly converted to double and eventually you get: TimeSpan.FromSeconds(1/50) -> 00:00:00

To make it right, you have to work with double from the beginning and use 1.0/50(1.0 is double):
TimeSpan.FromSeconds(1.0/50) -> 00:00:00.0200000

Regarding this one: gameObject.FixedUpdate(deltaTime goes here as double);
I assume you want to pass milliseconds as and argument value. For that you can write:

gameObject.FixedUpdate(dt.TotalMilliSeconds);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top