0

For example in cocos2D:

- (void)update:(ccTime)delta

can someone explain what these time deltas or timestamps are used for? How are they relevant for how the game world is updated? Is it because we do not know the fps reliably and should not just rely on incremental property updates based on -update calls?

genpfault
  • 51,148
  • 11
  • 85
  • 139
openfrog
  • 40,201
  • 65
  • 225
  • 373
  • 1
    read @LearnCocos2d answer here on FPS ... http://stackoverflow.com/questions/13830185/why-is-only-60-fps-really-smooth-in-cocos2d. Basically, you will get (in cocos2d) all your update methods called after the screen refresh cycle. Depending on what you do in anyone of these update methods (ie time taken), your code may affect drastically the FPS, but mostly the ms per call. Thus, next time you are called, it may be important to know the delta since last-call in case you are trying to simulate a realistic time base in a perceptible manner (skip frames, move more as function of soldier speed). – YvesLeBorg Dec 01 '13 at 23:13
  • OK so the purpose of the delta is that your animations don't depend on FPS but they depend on time? – openfrog Dec 05 '13 at 11:41
  • correct. Even when you set the fps , it is only a vague promise, not a commitment. So you could endup (at 60fps) with one frame at .0167 secs, and the next at .033. Time matters if you want to avoid erratic movement. – YvesLeBorg Dec 05 '13 at 18:01

1 Answers1

5

It is important for making frame independent movement. Typically any character movement you take into consideration the time since the last update call.

This is to ensure that your game behaves the same across devices of various performance. If you move a character by 1 pixel every frame then on a device that runs at 60fps the character will move twice as fast as on a device that gets 30fps.

By affecting all movement code for example by the delta time you ensure that all devices will behave the same.

It is simple to make movement frame rate independent. Something like multiplying a movement vector by deltaTime will achieve this.

user819640
  • 250
  • 5
  • 14
  • 2
    it's not quite that simple if the visuals (view) isn't decoupled from game logic (model). You could end up with game logic issues like tunneling through collisions at low framerates: http://www.learn-cocos2d.com/2013/10/game-engine-multiply-delta-time-or-not/ – CodeSmile Dec 02 '13 at 10:19
  • Oh, ok so deltaTime is a factor. For example when device has 30 fps it is 2, and when device has 60 fps it is 1? And you multiply it with everything? Good point about decoupled game logic. – openfrog Dec 05 '13 at 11:43