Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to roll back data. To turn back time

Tags:

c++

arrays

So, I have a 3d platformer. And I want to have a button that if you hold it it makes you "go back in time". Thankfully the game is rather simple and only has one entity so the only thing that would have to be saved for each frame is.

struct Coord {
float x;
float y;
float z;
}

structure Bool6 {
bool front;
bool back;
bool left;
bool right;
bool top;
bool bottom;
}

struct Player {
Coord Pos;
Coord Vel;
Bool6 Col;
}

But I fear that is a lot of data especially since my game theoretically runs somewhere around 60fps and it would be good to have 5 seconds or so (300 frames) of data saved that can be accessed when roll-backed. I have considered each frame doing something like this

Player Data[300];

for (int i = 299; i > 0; i--)
{
   Data[i] = Data[(i-1)];
}
Data[0] = "THIS FRAMES DATA";

However that sounds like it means an outrageous amount of processing power is going just in storing each frame.

Is their a more efficient way to store this data keeping all of the data in order?

Also is their a way I can tell an array slot that it has nothing? So that their arent problems if the player tries to rollback before all of the array slots are filled or after rolling back? I believe in C# i would have set it equal to NULL... but that doesn't work in c++ probably because im using structures.

Thanks much!

like image 919
Andrew Avatar asked Nov 10 '22 12:11

Andrew


2 Answers

However that sounds like it means an outrageous amount of processing power

Before making such a statement it can be useful to do the math. It seems the data you are concerned with is about 40 bytes, so 40*300 = 12 kB. This can easily fit in memory and is far from an "outrageous amount of processing power" on modern computers.

Is their a more efficient way to store this data keeping all of the data in order?

Yes. If your game is deterministic, all you have to store is the player's input and one game state 5 seconds ago. When rolling back, reset the game state and replay user inputs to recompute each frame data.

See this question for interesing discussion on how to design your own replay system on gamedev stackexchange.

like image 101
fouronnes Avatar answered Nov 15 '22 06:11

fouronnes


I don't think an array of 300 relatively small elements will slow you down at all, have you tried profiling it yet?

That said you could store it in a vector and keep an iterator to the "current" and update that.

like image 22
Pieter Avatar answered Nov 15 '22 06:11

Pieter