# Time Reversal in Real-Time 3D

This topic is 3267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Many of you would be familiar with the concept, having played or read about Prince of Persia: Sands of Time. I've been tring to find information on how to implement a time rewind/reverse function into a real-time 3D game similar to that found in this game. The closest articles i can find are ones on game input recording and playback, and a couple of paragraphs from the article "Mapping Time In Video Games". Such an undertaking in a game of today's standards would require the ability to rewind a physics engine, as well as the animations of the game characters and the game-state itself. So how would one go about doing this? If you have any advice, or can point me in the direction of any articles, that would be great.

##### Share on other sites
I think PoP succeeded at this because they used a simulation of rather limited complexity. It may even have been possible for them to just store away the full physics state of each frame and interpolate when playing it backwards, and warping objects to their new position, overriding the simulation stepping of the physics engine.

Or they had their own physics simulation that supported reverse stepping by ensuring each operation had a reverse. This is probably complex and there's no (as far as I know) general way to treat all operations by simply reversing operations and using a normal physics engine.

Imagine for example someone jumping from a platform out into empty space. You can reverse the forward velocity and gravity acceleration at the time you switch to rewinding, but the jump action just disappears in the reversal. Moving forward knocking a box into movement forward with simultaneous angular rotation would be quite complex too. If you rewind back to the point of collision (should be easy), how handle the "collision reaction" you get at the point of collision? It should in this case be that the player moves back and the box comes to rest. Even with the knowledge that the player moved forward before that, at the point of rewinding, the timing must be perfect and rounding errors must not happen, to ensure that the box doesn't collision react to the moving away of the player.

I'd say storing away the old state is your best bet, if you can afford the memory consumption. I'd say you can, if you only intend to rewind as little as they did in PoP - you have character position, velocity, active animation and animation key frame for each character that is within sight (all other characters are standing still, you could assume), making for a total of maybe 100-2000 bytes per frame. That's at most 1.2MB for 10 seconds of rewinding capability.

##### Share on other sites
Just save the last few game states and replay them. Or, if you want extra work, try to make undo functions for every action that is in the game, use them to reverse time and try to make that thing stable with floating point numbers...

EDIT: One frame's size from my 2D game (with physics) is ~20 kB. If we save the last 2 seconds with game update rate = 30 times per second, we get 60 * 20 = 120 kB. If the game would be 3D and a bit more advanced, I'm guessing that our game data would be ~50 kB and the total size would be 300 kB. That's not much at all.
Btw, don't look at the fact that save files from Unreal Engine games are big and saving there is slow because the engine is too universal and saves too much data.

[Edited by - snake5 on December 29, 2009 8:37:37 AM]

##### Share on other sites
Quote:
 Original post by SargerasMany of you would be familiar with the concept, having played or read about Prince of Persia: Sands of Time.I've been tring to find information on how to implement a time rewind/reverse function into a real-time 3D game similar to that found in this game.The closest articles i can find are ones on game input recording and playback, and a couple of paragraphs from the article "Mapping Time In Video Games".Such an undertaking in a game of today's standards would require the ability to rewind a physics engine, as well as the animations of the game characters and the game-state itself.So how would one go about doing this?If you have any advice, or can point me in the direction of any articles, that would be great.
You're going to need an undo stack of sorts, containing a set of events - everything from player input to object collisions to game-state - which lists both before-and-after states, and also update routines for each object that are bidirectional.

edit: in terms of floating-point errors, you can use these event listings to re-calibrate - so the falling box might not rewind to exactly where it was, but when it hits the event, that then re-positions it in its precise original location before whatever caused it to fall happened.

##### Share on other sites
snake5; just for the record, your math slipped. 2*30*20kB = 60*20kB = 1200kB, not 120kB.

##### Share on other sites
I have experience with replay (i.e. playback), but not rewinding simulation. I haven't thought much about it, but I'd make a way to set the game state based on replay buffer information, and when reversing time simply playing back the replay buffer in reverse, when exiting the replay buffer use the frame to set the proper game state information and resume the game.

This is easier to say than to do though. You'll need to be able to ensure you can track which animation sequences are playing and have the animation system be able to start a sequence from a specified frame. Also, the more complicated your game state, the harder this is to maintain, and dead enemies/allies are another thing to deal with (not to mention dealing with particles, and rigid bodies affected by physics).

##### Share on other sites
Right, thanks. :)
But that's still not too much. Games like that would require a PC with 1 GB memory anyway..

##### Share on other sites
The simplest way to provide a 'rewind' feature is to capture the 'state' of the game each frame and simply restore that state in reverse order.Depending on how complex your game world is,it wont take inordinate amounts of memory.You would typically have a fixed size circular buffer,with the size of the buffer constraining how much 'time' you can rewind.

Mod edit: Removed spam signature

[Edited by - Evil Steve on January 5, 2010 8:33:40 AM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633726
• Total Posts
3013569
×