How to: Make a Game Use a Variable Time Step

Demonstrates how to make a game use a variable time step.

To make a game use a variable time step

  1. Create a class that derives from Game.
  2. Set IsFixedTimeStep to false. This causes Update to be called as often as possible instead of being called on a fixed interval.

    IsFixedTimeStep = false;
    
  3. Since the amount of time between calls to Update will vary, specify any rates used in the game as units per millisecond (ms).

    // Speed in world units per ms.
    private double speed = 0.02f;
    
  4. In Update get the value of gameTime.ElapsedGameTime.TotalMilliseconds. This gives the amount of time that has passed since the last call to Update.

    // Time elapsed since the last call to update.
    double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;
    
  5. Determine the change that occurred since the last update by multiplying any rates being used by the elapsed time.

    // Multiply speed by elapsed time to get the distance moved.
    double distance = (speed * elapsedTime);
    
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;

    // Speed in world units per ms.
    private double speed = 0.02f;

    // Direction the object is facing.
    private Vector3 direction = new Vector3( 0, 0, 1 );

    // Current position of the object.
    private Vector3 position = new Vector3( 0, 0, 0 );

    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );

        IsFixedTimeStep = false;

    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here.

        base.Initialize();
    }


    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {
            // TODO: Load any ResourceManagementMode.Automatic content.
        }

        // TODO: Load any ResourceManagementMode.Manual content.
    }


    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }


    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here.

        base.Update( gameTime );

        // Time elapsed since the last call to update.
        double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;

        // Multiply speed by elapsed time to get the distance moved.
        double distance = (speed * elapsedTime);

        // Apply the change in position in the direction the object is moving.
        position += direction * (float)(distance);


        // Add application-specific drawing code.

    }


    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        // TODO: Add your drawing code here.

        base.Draw( gameTime );
    }
}
Show: