I am working on a car race game and just implemented a ghost sprite for replaying past races. I use a physics engine and after much reading I came to the conclusion that the best way to store the ghost data for replay would be to record the car's position and rotation at given timepoints, as for example described here: https://gamedev.stackexchange.com/a/8380/26261.
But what would be a good way to find those timepoints during replay? An example would be a record with this data:
time: +3.19932 (seconds since race start) position: 180,40 (position at that time) rotation: 30.4 (rotation at that time)
But I have several problems with that:
When I replay, it's unlikely that I reach the exact timepoint at 3.19932 again - more likely, I will have a timepoint around 3.1 and have to find the closest matching record. When interpolating, even the closest matching above and below. This sounds very inefficient and time consuming?
In which list structure could I store these records for a later replay? An array? Doesn't that mean that search time for records matching a certain time will increase the longer the race is?
Which frequency should I use for timepoints? Each frame would be -I guess- overkill, rather I should save i.e. every nth frame and interpolate in between, which makes the storage questions in 2. even more difficult.
So is this idea even the right approach? If yes, how could I efficiently store and retrieve the data? Please note that I generally would like to go with using the data structure above, not deterministic gamestates and recording user input etc.
Thanks for any help!
I realise I should describe the environment I use: Cocos2D for iPhone.
There is a method
update:(ccTime)delta. Ideally, this method would be called every 1/60 seconds, but there is no guarantee -
delta is the actual time passed since the last gametick and could be a lot more or less than 1/60.
It is in this method where I would like to store the current gamestate.