1
\$\begingroup\$

So I know how timestepping works.

As I understand it if I wanted to move a character at the same speed in real-world time I need to simply multiply the movement of the character by DT on each frame.

So code without timestepping

forever
{
  player.position.x += 1
}

and with timestepping

forever
{
  player.position.x += 1 * dt
}

I have noticed that it is frustrating in some cases to do it like this. Because lets say I had a specific distance in mind for the player to travel in the frame, without timestepping I only need to add that ammount, however with timestepping I need to find a different value that produces the same results when multiplying by a "normal" dt value.

I was wondering how I could approach the following.

I want to move the player forward one unit each frame as long as the fps is 60. So I am going to call a function with moveForward(1) every frame. However if the fps is 30 I want the player not to move one unit but instead travel 0.5 units each frame but still call moveForward(1). So basically if their is no lag it travels 1 unit each frame if their is lag it still travels at the same "real world" speed but in code travels less far each frame.

My question is. How would one calculate it? Perhaps you could fill in this skeleton function.

var targetDT = (1.0 / 60.0)
var currentDT = //**Value changes each frame to represent currenty delta time of frame**//
func moveForward(units: Int)
{
   var scalar = //**IDK what goes here something that considers the fps**//
   player.position.x += units * scalar;
}

This is probably very simple math I am just not figuring it out!

Also what is the relationship between dt and fps if we consider that the render loop and logic loop happen at the "same" time simultaniously?

\$\endgroup\$
4
\$\begingroup\$

I think you need to rethink what you're trying to do.

Look at the last lines in your example move forward code

var scalar = //**IDK what goes here something that considers the fps**//
player.position.x += units * scalar;

Now look at the normal movement scaled by time code

player.position.x += 1 * dt

Notice the similarity?

I understand that you have problems calculating exactly how fast a character should move. Though I usually find it easier to think of speed per second than speed per 1/60th of a second its easy to make a few helper methods to help you with that:

speedPerSecond = speedPerFrame * 60

With the added bonus that your game will run well even when the frame rate dips a bit.

As with all posts about time steps, please read this article: http://gafferongames.com/game-physics/fix-your-timestep/

\$\endgroup\$

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.