Table of Contents

How to update a game with Variable or Fixed Timing

Demonstrates how to set up the runtime to call your Update method using variable or fixed timing.

Overview

Demonstrates how to set up the runtime to call your Update method using variable or fixed timing.

There are two techniques for setting how often your Update method is called.

  • Fixed timing (Default) means that Update is called each time a fixed interval of time has passed. Fixed timing guarantees that Update will be called, however, you may drop frames if the previous work needs to be interrupted to call Update.
  • Variable timing means to call Update as soon as other work finishes; this implies that it is up to a game developer to ensure that your render loop happens quickly enough so that Update will be called often enough to exceed your minimum frame rate.

To use a fixed time step

Note

Fixed timing is the default set to 60 FPS (or 0.0166667 seconds per frame).

  1. Create a class that derives from Game.

  2. Set IsFixedTimeStep to true.

    this.IsFixedTimeStep = true;
    

    This causes the Update method to be called each time the fixed time interval has passed.

  3. Set TargetElapsedTime to a fixed interval of time.

    This example sets the time between calls to 16 milliseconds.

    this.TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);    // Update() is called every 30 times each second / 30 FPS
    
    Note

    In older samples, you might see TargetElapsedTime registered as TimeSpan.FromTicks(333333), which is the same as 30 FPS.

To use a variable time step

A variable timestep has as much benefits as it has drawbacks and care must be taken as it will directly affect your rendering if you are not careful.

  1. Create a class that derives from Game.

  2. Set IsFixedTimeStep to false.

    This causes the Update method to be called as often as possible, instead of being called on a fixed interval.

    this.IsFixedTimeStep = false;
    
    Note

    The Ship Game sample implements a variable time step depending on the vsync setting of the device. It is a good place to start when evaluating variable timings for your game.

    The Racing Game sample on the XNA Game Studio Archive (still based on XNA 4) is written to ONLY work in a Fixed Time loop due to its physics implementation. Beware it is an awesome project.

See Also

Concepts

Reference

© 2012 Microsoft Corporation. All rights reserved.