I am wondering about how time manipulation mechanisms in games are typically designed. I am particularly interested in time reversing (sort of like in the latest SSX or Prince of Persia).
The game is a 2D top down shooter.
The mechanism I am trying to design/implement has the following requirements:
1) Actions of entities apart from the player character are completely deterministic.
- The action an entity takes is based on the frames progressed since level start and/or the position of the player on the screen
- Entities are spawned at set time during the level.
2) Time reverse works by reversing back in realtime.
- Player actions are also reversed, it replays in reverse what the player performed. Player has no control during reverse time.
- There is no limit on the time spent reversing, we can reverse all the way to the beginning of the level if wanted.
As an example:
Frames 0-50: Player moves foward 20 units over this time Enemy 1 spawns at frame 20 Enemy 1 moves left 10 units during frame 30-40 Player shoots bullet at frame 45 Bullet travels 5 foward (45-50) and kills Enemy 1 at frame 50
Reversing this would play back in realtime: Player moves backwards 20 units during this time Enemy 1 revives at frame 50 Bullet reappears at frame 50 Bullet moves backwards 5 and disappears (50-45) Enemy moves left 10 (40-30) Enemy removed at frame 20.
Just looking at movement I had some ideas about how to achieve this, I thought of having a interface that changed behavior for when time was advancing or reversing. Instead of doing something like this:
void update()
{
movement += new Vector(0,5);
}
I would do something like this:
public interface movement()
{
public void move(Vector v, Entity e);
}
public class advance() implements movement
{
public void move(Vector v, Entity e)
{
e.location += v;
}
}
public class reverse() implements movement
{
public void move(Vector v, Entity e)
{
e.location -= v;
}
}
public void update()
{
moveLogic.move(new vector(5,0));
}
However I realised this would not be optimal performance wise and would quickly become complicated for more advance actions (such as smooth movement along curved paths e.t.c.).