me as very unexperienced game developer, I wondered how I could achieve something like re-winding the time in my game or jump back to a special moment (killcam) and replay it for the user.
-
3\$\begingroup\$ Possible duplicate of gamedev.stackexchange.com/questions/20289/… If that doesn't answer your question: What have you tried? Where do you run into problems? \$\endgroup\$– EricCommented Jun 19, 2012 at 9:05
-
\$\begingroup\$ @Eric true, I didn't find that particular answer by googling/searching. Thank you. I already started a close vote on this question with a link to your link. \$\endgroup\$– daemonfire300Commented Jun 19, 2012 at 9:32
-
\$\begingroup\$ -1 This is a very broad question. Braid's timeshift is very different to a killcam: you're asking about slow motion, (possible) time-reversal, and replays and not being clear at all about which one you actually want. \$\endgroup\$– doppelgreenerCommented Jun 20, 2012 at 0:52
-
1\$\begingroup\$ @JonathanHobbs you downvote something I already voted to close and said that is a duplicate? I am very sorry but this seems a bit overreacted to me. \$\endgroup\$– daemonfire300Commented Jun 20, 2012 at 13:16
1 Answer
The only way you can achieve this is if your game is completely deterministic. In mathematical terms, that means that if you have the function:
y = x^2
Then you can always get a value for x
if you have a value for y
:
x = square_root(y)
For every time t
, you must be able to generate a world state. If you have something like a bullet being fired from a gun, then you want to be able to rewind its position from impact back to when it left the barrel. There are two ways to achieve this:
Record the position of the bullet every x frames. When replaying, interpolate between those frame positions.
Have a function for bullet position that takes a time
t
. Have this function be completely deterministic and rewind it fromt1
(impact) tot0
(departure). This is called integration in mathematics.
Now, this second method is obviously the best, but it's also the hardest. Because in a game, not everything can be broken down to functions:
- Players mash on keys and move with mouses.
- Networking delays can mean physics run at a different framerate.
- Some functions can not be integrated (see: N-body problem).
Most likely you'll end up with a combination of the two: integration for bullet position and interpolated recordings for player positions.
-
\$\begingroup\$ I would like to add that especially floating point operations are problematic since the outcome can be slightly different on another platform and these errors accumulate. \$\endgroup\$– Roy T.Commented Jun 19, 2012 at 9:50
-
\$\begingroup\$ @RoyT. What do you mean by another platform in context of braid or killcam? \$\endgroup\$ Commented Jun 19, 2012 at 9:55
-
\$\begingroup\$ @Kikaimaru I think he means different hard/software platforms. For example, if you were to use CUDA for your own physics simulation, you would be restricted in both accuracy and rounding modes available. This could result in slight errors, or big ones, if your results propagate into the next simulation step and accumulate over time. \$\endgroup\$– user13213Commented Jun 19, 2012 at 10:46
-
\$\begingroup\$ @melak47 even worse floating point operations differ per compiler version and setting, see gafferongames.com/networking-for-game-programmers/… \$\endgroup\$– Roy T.Commented Jun 19, 2012 at 12:15
-
\$\begingroup\$ @RoyT. that's just brilliant :/ \$\endgroup\$– user13213Commented Jun 19, 2012 at 12:43