1
\$\begingroup\$

I have been developing my game on two computers. I recently got a new computer and have noticed an issue that seems to be happening in my game.

When computer 1 runs the game at 60 FPS everything seems to run fine, all the timings are correct and everything runs smoothly.

When computer 2 runs the game at 60 FPS everything seems to slow down, everything happens properly (all the timings are correct) but it's as if the update is not being called at 60 FPS. If I "unlock" the frame rate and let it go to >3000 fps then it runs fine, but not when it's at the expected 60 FPS. A frame counter in code seems to tell me the FPS is 59.

I'm not sure if this is relevant but I usually use something like this to perform updates between moving or animations, etc:

 public bool ShouldUpdate(GameTime gameTime)
 {
     if (this.Disable)
         return false;

     float dpf = this.DelayPerFrame; // example value: 60f
     float tpf = 1.0f / dpf;

     this.totalElapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

     if (this.totalElapsedTime >= tpf)
     {
         this.totalElapsedTime = 0f;

         return true;
     }

     return false;
 }

What kind of things can be causing this? I'm more than happy to add any additional information.

I guess my question really comes down to, how do I ensure that all updates and timings happen the same across computers?

\$\endgroup\$
  • \$\begingroup\$ It's usually a bad idea to use a timer to synchronize to framerate. Instead, you should use a sync API or event that corresponds exactly to the vertical refresh event. Not sure what that looks like in xna/monogame, but I assume there's some way to do it. \$\endgroup\$ – MooseBoys Apr 10 '15 at 20:46
4
\$\begingroup\$

You shouldn't be trying to ensure that all your updates happen after X milliseconds - the existing game loop does that already. Instead, what you should be doing is applying delta time correction to your updates to ensure your updates, no matter how frequent or infrequent, regular or irregular, all happen at the same rate.
An example is as follows, and is rather simple to implement.

float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
position += velocity * delta;

What this does is effectively scale your update logic to move according how much time has elapsed since the last update. If you move at 1 meter/second, and you update 10 times a second, each update will move you forward 10cm. Update 20 times/second, move forward 5cm each time. This has the advantage of keeping everything moving at the same rate on different machines despite the number of updates occuring.

\$\endgroup\$
  • \$\begingroup\$ How does this concept apply to things like changing the animation frame of a sprite? Normally I do that based on time, how should I actually be doing it? \$\endgroup\$ – test Apr 14 '15 at 19:20
  • \$\begingroup\$ Sorry for not responding sooner - been pulling solid 9 hour days lately, and mentally knackered after debugging code for at least 5. Anyway, Is your way of doing sprite animations similar to the MSDN example (msdn.microsoft.com/en-us/library/bb203866.aspx), or is it fully custom? If you're using the AnimatedTexture class, it does delta correction internally, so my advice is effectively null. If you're using a custom implementation you've written, then the link above gives a look inside the AnimatedTexture.UpdateFrame() method, and might be useful on how to apply delta correction. \$\endgroup\$ – Seta Apr 15 '15 at 5:39
  • \$\begingroup\$ @test I agree, your tpf and looping technique is weird. do as alex says. and then fix your timestep. (google for "fix your timestep"). it looks like what you are doing, using a remainder accmulator to keep the floating part of what is lost by the determination of loop count for the inner physics loop. (physic loop in the render loop, with loop count depending on delta time. and the result is a fixed dt for the physics.) \$\endgroup\$ – v.oddou May 13 '15 at 9:08

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.