4
\$\begingroup\$

I've just started using MonoGame (which pretty much seems to be an open implementation of XNA) and I'm trying to figure out on how to work with framerate independant calculations. Usually what I found in all the other game engines / libraries I've used before its some kind of field called "delta time" which I only had to multiply to all the values to make them framerate independant.

I understood the logic between that and I'd like to continue working with delta time values. But as I couldn't find something like that in MonoGame I made some kind of workaround myself. Here's what I did:

protected override void Update (GameTime gameTime) {
    deltaTime = gameTime.ElapsedGameTime;
    float delta = deltaTime.Milliseconds;
    delta = delta / 1000; 

    //(...)

    //And now I can use delta to apply movement like this:
    position.X += SpeedX*delta;
}

Is that a good workaround? Does it have any overhead I might be overlooking at? Is there a pre-built value for what I'm looking for?

Thank you very much

\$\endgroup\$
  • \$\begingroup\$ I'm not sure what's the problem. Just use gameTime.ElapsedGameTime.TotalSeconds and multiply everything by that. \$\endgroup\$ – user9790 Dec 7 '13 at 16:53
  • \$\begingroup\$ You may want to check this. gamedev.stackexchange.com/questions/1589/… \$\endgroup\$ – Kogesho Dec 7 '13 at 17:06
6
\$\begingroup\$

There's no need to do anything complicated. The GameTime.ElapsedGameTime property is a TimeSpan representing the amount of elapsed game time since the last update.

Therefore, if you want to measure your game updates in seconds the easiest thing to do is this:

    protected override void Update(GameTime gameTime)
    {
        var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

        //And now I can use delta to apply movement like this:
        position.X += SpeedX * delta;

        base.Update(gameTime);
    }

What you're really doing here is declaring that you want to measure your game updates in seconds. So the value of SpeedX is going to represent pixels per second.

I'm not entirely sure why Microsoft decided to use TimeSpans instead of floats. I suppose in theory you could choose to represent your updates in milliseconds, but in practice I don't think that's very common. In any case, once you've decided the unit of measure it's probably best to stick with it and the easiest way to do that is to pass around a single value as a float.

\$\endgroup\$
-1
\$\begingroup\$

Try something like this:

private double lastTime;

protected override void Update (GameTime gameTime) {
    double delta = (gameTime.TotalSeconds - lastTime);
    lastTime = gameTime.TotalSeconds; 

    float deltaF = (float)delta;

    position.X += SpeedX*deltaF;
}
\$\endgroup\$
  • 3
    \$\begingroup\$ There is no need to do this yourself. You can just simply use the GameTime.ElapsedGameTime.TotalSeconds property. \$\endgroup\$ – user9790 Dec 7 '13 at 16:55

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.