Jump to content

  • Log In with Google      Sign In   
  • Create Account


[XNA] and Fix Your Timestep


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 04 January 2011 - 08:23 AM

I'm sure all of you are familiar with the "Fix Your Timestep" article, and if you're not, then you should be.

I'm trying to achieve the same effect in an XNA game, without writing my own Game class, since the Game class does an awful lot for me that I can't be bothered to do myself.

I've come up with a fairly naive implementation, that seems to work, but I want someone with more XNA experience to take a look and see if I'm doing something that's going to cause problems.

Yes, I know I've got an allocation in my loop, but Microsoft in their wisdom decided to make the fields on the GameTime class readonly, so I'm not sure how I can get around that.


long t = 0;
const long dt = TimeSpan.TicksPerSecond / 60;

long currentTime = 0;
long accumulator = 0;

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

long newTime = gameTime.TotalGameTime.Ticks;
long deltaTime = newTime - currentTime;

currentTime = newTime;
accumulator += deltaTime;

while (accumulator >= dt)
{
base.Update(new GameTime(gameTime.TotalGameTime, TimeSpan.FromTicks(dt)));

t += dt;
accumulator -= dt;
}

// TODO: Add your update logic here
}




Edit: In case it's not clear, I'm using the following lines to turn off VSync and allow the render loop to run as fast as possible.

graphics.SynchronizeWithVerticalRetrace = false;
IsFixedTimeStep = false;



Sponsor:

#2 MJP   Moderators   -  Reputation: 10110

Like
0Likes
Like

Posted 04 January 2011 - 10:41 AM

What you have there is pretty much exactly what the base Game class will do if you enable fixed time steps. So you won't have gained anything.

If you want to actually take it a step further, what you need to do is add partial updates. These occur when your accumulator is > 0, but less than your time step amount. In this case you divide the accumulator by your time step to get an "alpha" value, and in your update you interpolate between the previous state and updated state using that alpha.

#3 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 04 January 2011 - 09:36 PM

The Game class will still only call Draw 60 times a second with fixed timestep enabled, at least that's what my profiling is telling me.

I probably shouldn't care, but I'm finding that using the Game class loop is giving me little stalls every few seconds or so.

#4 EJH   Members   -  Reputation: 314

Like
0Likes
Like

Posted 05 January 2011 - 04:28 AM

Quote:
Original post by adt7I probably shouldn't care, but I'm finding that using the Game class loop is giving me little stalls every few seconds or so.


The dropped frames in the default XNA FixedTimeStep makes it un-usable. It has been brought up many times on the forum:

http://forums.create.msdn.com/forums/t/9934.aspx

... but I've never seen someone post a way to use XNA FixedTime step and not have jerky motion updates. The only way to get smooth motion is:

- FixedTimeStep = false
- do all motion updates based on time elapsed since previous frame

Basically what you are doing now. You can leave vSync on imho to prevent screen tearing, though for some games that might not be a problem.

As an experiment try enabling FixedTimeStep with your fully delta-time enabled motion and camera updates. You'll notice that motion and especially camera rotation are still jerky/stuttering. Disable it and they go back to smooth as glass. You'd think that would not be the case ...

#5 Aardvajk   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 05 January 2011 - 04:33 AM

Quote:
Original post by EJH
- do all motion updates based on time elapsed since previous frame


Problem with that is that simulations are not stable. In the case of complex physics, this can lead to assemblages bursting apart. In the more usual sense of a 2D jumping platformer, characters will jump different heights and distances depending on framerate.

MJP has the correct answer - you need to fix your timestep then use the alpha value to interpolate positions on screen. You get smooth graphics and stable, repeatable simulation.

#6 MJP   Moderators   -  Reputation: 10110

Like
0Likes
Like

Posted 05 January 2011 - 09:44 AM

Yeah like Aardvajk mentioned, even simple physics simulations will become non-deterministic or even completely unstable without a fixed time step. It will be fine if you VSYNC at 60Hz and never go below that (since then your time step is essentially fixed at 60Hz), but if you do dip below 60 the player might suddenly fall through the floor.

Another option is lock your time delta at 60Hz (or 30Hz), even if your rendering/update loop drops below that rate. With that your physics won't explode if you drop below your target rate, but it will look like time is slowing down. This is basically the old-school method used by a lot of 8/16-bit games. Consequently you're making a 2D platformer this can actually make for a kind of cool, "retro" effect.

#7 dpoon   Members   -  Reputation: 174

Like
0Likes
Like

Posted 05 January 2011 - 03:18 PM

Have you seen this article?
http://www.gamedev.net/reference/programming/features/iosExcerpt4/

It's specific to iOS but I'm sure the algorithm can be adapted for XNA.

This is the original article that the iOS one was based:
http://sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml.



#8 JWBaker   Members   -  Reputation: 235

Like
0Likes
Like

Posted 17 August 2011 - 01:21 PM

Iv been fiddling around with setting up my XNA game with my own fixed timestep. I think i got it, but i wanted a second opinion if these makes sense. Mostly i want to be sure i'm using GameTime correctly.

public class Game1 : Microsoft.Xna.Framework.Game {

    	GraphicsDeviceManager graphics;
    	SpriteBatch spriteBatch;
    	EntityManager entityManger;

    	private double t = 0.0d;
    	private double dt = 1.0d / 60.0d;
    	private double accumulator = 0.0d;
    	private double alpha = 0.0d;

    	public Game1() {
        	graphics = new GraphicsDeviceManager(this);
        	entityManger = new EntityManager();
        	Content.RootDirectory = "Content";
    	}

    	protected override void Initialize() {

        	base.Initialize();
    	}

    	protected override void LoadContent() {
        	spriteBatch = new SpriteBatch(GraphicsDevice);

    	}

    	protected override void UnloadContent() {

    	}

    	protected override void Update(GameTime gameTime) {

        	accumulator += gameTime.ElapsedGameTime.TotalSeconds;

        	while (accumulator >= dt) {
            	base.Update(new GameTime(TimeSpan.FromSeconds(t), TimeSpan.FromSeconds(dt)));
            	t += dt;
            	accumulator -= dt;
        	}

        	alpha = accumulator / dt;

    	}

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

        	base.Draw(gameTime);
    	}
	}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS