When should I multiply a value changing over time per gameTime.ElapsedGameTime.TotalSeconds

StackOverflow https://stackoverflow.com/questions/21735485

  •  10-10-2022
  •  | 
  •  

Question

I saw in a lot of tutorials (and also books) that when you have to change the position of an object, you should multiply the moving amount by elapsed game time.

First question

I understood that it is due to CPU speed, that is different for every PC, so if you don't do that every person that will play with your game will see different speed of the objects.

Is this true?

Second question

Should I do it also in this situation? In which situations should I do it?

public void Update(GameTime gameTime)
{
    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    position_ += change_position_amount_ * elapsed;
    scale_ += change_scale_amount_; // * elapsed; ?
    color_ *= fade_amount_; // * elapsed; ?
}
Was it helpful?

Solution

The answer to your first question is Yes, most likely they will perform at different speeds. Your game is probably going to have times when it needs to process a bunch of information, and it might slow down. If you don't take into factor the amount of time elapsed between frames, faster CPUs and GPUs will make the game perform faster. For example, if you program a game on a CPU or GPU that struggles, and then someone else plays it on a very fast CPU and GPU, the game could be too fast (and unbeatable) for them. Also, if a really weak CPU or GPU plays you game, and only gets 20-30fps, the game will run at half speed, unless the amount of time elapsed is taken into consideration.

To answer your second question, I would multiple everything that is dependent on time by the elapsed time. (As @SteveH pointed out, you can use either gameTime.ElapsedGameTime.TotalMilliseconds or gameTime.ElapsedGameTime.Totalseconds, whichever fits your needs better) So:

public void Update(GameTime gameTime)
{
    float elapsed = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

    position_ += change_position_amount_ * elapsed;
    scale_ += change_scale_amount_ * elapsed; 
    color_ *= fade_amount_ * elapsed; 
}

So in summary, you should take the elapsed time into consideration if you want a game with a consistent speed.

As a side note, gameTime.ElaspedTime.TotalMilliseconds, usually doesn't return the exact time. If you wanted to be very exact use gameTime.ElapsedRealTime.TotalMilliseconds (XNA 3.1 or earlier) or use a timespan or stopwatch. HTH

OTHER TIPS

For your first question: Yes this is true. If you do not use the change in time as part of your update different CPUs and different operating conditions (having a ton of programs running in the background) will cause your Update() to be called less frequently. We use deltaTime in there because it allows for compensation for when the Update() method is called less. For your second question: It's really up to you. Do you think that people using your game will get frustrated if the scale, or color fading seem to skip around? I normally would use deltaTime for anything that involves movement.

A side note: How you are currently getting the elapsed (or deltaTime) does not matter that it's a float. Below is normally how I've seen deltaTime work with XNA and MonoGame

float deltaTime = gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;

Essentially the above line gets the amount of milliseconds as an integer, and then creates a float that is 1000th the size of the milliseconds. This results in a float containing the number of seconds with an accurate decimal after it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top