Fixed timestep

Fixed timestep

I’m a long time cocos2d-iphone programmer and am struggling with my transition into C++/cocos2d-x.
What is the best way to implement a fixed timestep in C++ or using a cocos2d api?
This should not be implemented through something third party such as box2d, however.
Any help is appreciated!

-PS: Sorry if this has been asked before, I googled for a while and couldn’t find anything!

1 Like

What did you mean fixed timestep?
Could you paste some pseudocode?

I’m using a method similar to the one described here

There is a fantastic article to read here: if you have the time, it is really eye-opening. In case you can’t read it now I’ve written my own short explanation in this post, but it doesn’t come near the level of helpfulness. It also doesn’t explain why this is important so read the article!

An example of a fixed timestep is an update method that runs 60 times per second, regardless of framerate. This matters due to the nature of different processors and clock speeds of different devices (especially on android, but other platforms too!).

The method for scheduling an update method in cocos2d-x is as follows:
bool ClassName::init(){ this->scheduleUpdate(); } void ClassName::update(float dt){ CCLOG("Update! dt: %f", dt); }

This gets called once per frame. This works well for some games, but for anything using physics its unusable. The output, even while trying to run at 60fps is messy:
cocos2d: Update! dt: 0.017664 cocos2d: Update! dt: 0.015915 cocos2d: Update! dt: 0.016462 cocos2d: Update! dt: 0.017207 cocos2d: Update! dt: 0.016125
In a fixed timestep, dt should always be equal to 1 divided by the framerate. In my case 1/60 or 0.01666667.
The output should always look like this:
cocos2d: Update! dt: 0.016667 cocos2d: Update! dt: 0.016667 cocos2d: Update! dt: 0.016667 cocos2d: Update! dt: 0.016667 cocos2d: Update! dt: 0.016667

I’ve attempted to schedule my own update method through the this->schedule method to run at 60 times per second however it provides the exact same output.

A fixed timestep is absolutely essential for building games with physics and I’m looking forward to what the community thinks about this!

@dotsquid I’ll check that out! Thanks!

In order to do it, i think you should use a timer yourself.
How you use in cocos2d-iphone?

Is there are any solutions to implement fixed timestep?

Something like this?

float fixedSpeed = 1.0/30.0; //30 fps
float elapsedTime = 0.0;

void GameScene::update(float dt) {
    elapsedTime+= dt;
    if( elapsedTime >= fixedSpeed ) {
        elapsedTime = 0;


I was thinking of something general.