When to use gameTime


I’m making a platformer I’m trying to make it frame rate independent. I have a vague understanding of gameTime and how to use it, but I’m still confused of when to use it, and all attempts so far to implement it haven’t worked. My two questions are thus:

  • When should I use gameTime, do I use it for friction as well as change in velocity? And can I just do all of my calculations on the change in coordinates then multiply the final vector by gameTIme.ElapsedGameTime.Seconds or do I have to multiply each calculation?

  • How do I properly implement? A code snippet of a basic physics system with it implemented would be really helpful too.


Couple things to understand about gameTime:
The .Seconds property only represents the seconds “remainder value”. I.e. if your ElapsedGameTime was 10.5 seconds, the Seconds property would have a value of 0.5s. The TotalSeconds would have a value of 10.5, which based on your comment seems to be what you want here. Same goes for the other properties like Milliseconds and TotalMilliseconds, which is probably what you actually want to be using, but up to you.

An example of it in use:

player.position.X += MOVE_SPEED_PER_MS * gameTime.TotalMilliseconds;

However, you won’t see any change in your game’s execution with this code by default unless its framerate falls under 60fps. This is because MonoGame limits your game loop cycles to 60Hz by default (60 FPS). You can release this limit by setting IsFixedTimeStep = false in your game’s constructor (or later if you want to keep a fixed timestep for your loading cycle or some such). Be aware that doing so can have your game run at hundreds of frames per second during points where it isn’t doing much. Meaning, you need to consider the possibilities when using gameTime to animate things where the TotalMilliseconds value is very very small.

1 Like

Thank you for a prompt reply,

Your example is helpful, but perhaps a little too simplistic as I am using multiple vectors to achieve acceleration and friction. Should I apply your example too all operations on all vectors?

How you calculate that is up to you. I don’t have much of an opinion on it without seeing your code. Whether or not it is appropriate to apply the gameTime modifier to your end-result vector or to each portion of the vector’s calculation depends on how exactly you’re implementing those changes (percentages, +10 directly, based on duration of a button press, etc).

Here’s a post that I responded to from someone asking about this, but with jumping specifically:

To tweak this a bit for strafe acceleration:

//class vars
float intendedStrafeDirection;
float acceleration;
float maxStrafeVelocity;

// Input
intendedStrafeDirection = CurrGamePadState.ThumbSticks.Left.X;
//or dpad
if (CurrGamePadState.IsButtonDown(Buttons.DPadLeft)) { intendedStrafeDirection = -1; }
elseif (CurrGamePadState.IsButtonDown(Buttons.DPadRight) { intendedStrafeDirection = 1; }
else { intendedStrafeDirection = 0; }

// Update movement
float deltaTime = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;
float prevVelocityX = velocity.X;
velocity.X += intendedStrafeDirection * acceleration * deltaTime;
if (Math.Abs(velocity.X) > maxStrafeVelocity) { velocity.X = Math.Sign(velocity.X) * maxStrafeVelocity; }
//verlet integration
position.X += (prevVelocityX + velocity.X) * 0.5f * deltaTime;

In essence, use gameTime code (deltaTime) when things change over time in your world:

Velocity adding onto position → use deltaTime
Acceleration affecting velocity → use deltaTime
Friction affecting velocity → use deltaTime
Setting velocity to a constant → DON’T use deltaTime (I explain next)

If you look at how I implement jump in the link, I set the velocity as such:

velocity.Y = jump.verticalRate; //no deltaTime
position.Y += (prevVelocityY + velocity.Y) * 0.5f * deltaTime;

When I set the value to a constant rate, I don’t need deltaTime. However when I add the velocity onto position, I still need to use deltaTime.

Another thing to note is some things don’t need deltaTime, such as mouse movement over time. Say for example, you create a camera that pans or rotates when you move the mouse. In this case, you don’t use deltaTime:

Vector2 mouseDelta = new Vector2(mousePos.X - (Graphics.GraphicsDevice.Viewport.Width / 2),
    mousePos.Y - (Graphics.GraphicsDevice.Viewport.Height / 2));
mouseDelta.X /= Graphics.GraphicsDevice.Viewport.Width;
mouseDelta.Y /= Graphics.GraphicsDevice.Viewport.Height;

camera.Position += mouseDelta * panSpeed;

Notice how this doesn’t require deltaTime. I’ll link to the reason I was given back when I didn’t know about this:

1 Like

Thank you for a very detailed response, I shall give this a go!

Use gametime if anything that moves, needs a delay to activate, or anything thats time related.

Remember, each pc runs faster or slower. gametime is used to simulate the same time of movement no matter what computer they use.