• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
adt7

[XNA] and Fix Your Timestep

7 posts in this topic

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;

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 ...
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

[code] 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);
}
}[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0