What is the best way to slow down time?

Forums Programming cocos2d support (graphics engine) What is the best way to slow down time?

This topic contains 12 replies, has 10 voices, and was last updated by  alexasha 2 years, 9 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
Author Posts
Author Posts
March 18, 2011 at 7:47 pm #229539


What is the best way to slow down/ speed up time?

What is the best way to reverse time?

I am using box2d so I thought the best way is to apply different impulses, but how do I reverse time?


March 18, 2011 at 8:09 pm #321188


In order to ‘reverse’ time, you would need to keep track of the history for each object in your game, and then run through those saved states in reverse order to achieve the appearance of reversing time.

A similar approach could be used to save ‘ghosts’ in a game.

To change the speed of your game, have each object move by a general ‘speedFactor’ parameter. Then you could change that universal speed setting to slow down or speed up movement of objects, essentially giving the appearance of time speeding up or slowing down.

March 18, 2011 at 8:59 pm #321189


Also this works if you want to change the passage of time across everything:

[[CCScheduler sharedScheduler] setTimeScale:1.0]; //This is normal speed
[[CCScheduler sharedScheduler] setTimeScale:0.5]; //This is half speed
[[CCScheduler sharedScheduler] setTimeScale:2.0]; //This is double time

March 18, 2011 at 10:59 pm #321190


@spadict Thanks!

I wish there was a simple way to “reverse” time.

March 19, 2011 at 1:02 am #321191


I wish there was a simple way to “reverse” time.

Don’t we all… :D

March 19, 2011 at 6:43 am #321192


Ha! Would have saved me thousands in a divorce, lol!

March 19, 2011 at 7:12 am #321193


I think you’d be hard pressed to come up with a physics solver which works both forward and backward. Some processes are reversible, others like friction are not. Would running your game in reverse cause objects sliding on a surface to slow down (as would appear normal, but incorrect), or speed up (would look odd in reverse, but be correct).

March 19, 2011 at 11:27 am #321194


I looked a bit into this while researching a rewind function.

The particle systems used in cocos can actually run backwards. If you know the reset time, you can progress it to “now”, and then run it backwards, simply by stepping with negative time. Looks wicked when you fire explosions backwards :)

As for doing the same with a physics engine, it is by no means this easy. While stepping negative actually will make things run backwards, it will not be a reverse replica of what just happened, but some weird physics where things collide with backsides, rather than front side of objects.

For a few objects, I would simply track position. For many objects, I would make snapshots, and then “interpolate” the reverse movement.

March 19, 2011 at 11:33 am #321195


it probably shouldn’t be too hard to create a CCSprite subclass that saves a reversed version of actions that run on it

subclassing CCSprite and overriding runAction:

the only problem i see with doing it this way is that until you reverse the action you’ll have a bunch of CCActions laying around taking memory, and i case you’re performing a ton of actions or you have a lot of sprites this can get heavy

March 19, 2011 at 4:27 pm #321196

Blue Ether

There was a really good talk by Jonathan Blow at GDC last year about how he implemented time reversal in Braid. If you have access to the GDC archives, I suggest you look it up and listen to the talk, or at least check out the slides- which, I think, you can do for free? Not sure about that offhand.

To summarize, his concern was developing a system that would always yield the same EXACT results over and over. If you try running relative calculations forwards and backwards over and over, rounding errors will eventually result in slight variations. It’s not a robust enough method to build a solid repeatable gameplay system on.

Braid actually stores the game state in every frame, rather than trying to re-enact what happend via actions. Since that’s a massive amount of data, he uses a system of storing complete game states only at occasional “key frames,” and by only storing the variations from those key frames for all of the frames in-between. As you go backward, it recreates the game state in each frame by applying those variations to the nearest key frame state.

March 19, 2011 at 4:44 pm #321197


@BlueEther Very interesting concept, makes a lot of sense

April 9, 2011 at 7:44 am #321198


Do NSTimers get affected by [[CCScheduler sharedScheduler] setTimeScale:2.0]; too?

July 25, 2011 at 11:06 am #321199


@kohjingyu I don’t think you are supposed to use NSTimers with cocos2d.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.