# Replay Physics

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

## Recommended Posts

Hi everyone,

I would like to get some advice on how to make a visually convincing replay for my racing game.

I am using PhysX for the physics simulation and i am pretty sure that everything is non-deterministic.

I am not sure how to verify if my time step is fixed or not ?

My first attempt at recording replay was very hilarious, and a big fail to say the least...

I tried to record user input (only input change) so every time i want to start recording i have a variable called recordedTime.

This variable is incremented with the time step like this.

if (isRecording) {

recordedTime += Time.timeStep;

}


Then i would record the user input like this

void recordAction( float time, string actionName ) {
actions.Add( time.ToString() + "_" + actionName );
}


so for example recordAction( recordedTime, "accel_on" );

etc etc.

But of course failed to reproduce the action properly.

So i decided to add some keyframes at key moments in order to repositionate the car in the good position direction velocity and everything.

So i have a function that record pretty much everything.

Car position, orientation, rigidbody velocity, rigidbody angularvelocity, same for wheels, etc...

But even after adding the keyframe system, it just does not worked as expected.

The thing, is i don't know really the tweaking i need to do in order to reproduce the same action at the same time.

Should i use Update ? FixedUpdate ? LateUpdate ? to record action ?

I just struggle to understand the most fundamental concept.

If i record acceleration input at 1.20 second, how can i play it again later with a different camera angle that is going to affect my framerate and then play the action faster or slower than 1.20 ?

So, instead of trying to reproduce everything in a physical way, i was wondering if it would be a decent solution to record the car position and orientation keyframes only, and then play a kind of linear transition between each keyframes in order to reproduce visually the movement of my car.

Does some professional games use it ?

Any feedback or advices would be very appreciated.

Edited by jujunosuke

##### Share on other sites

Hello jujunosuke,

Recording only the position and orientation of each rigidbody along with the keyframe should be fine. After the recording, you will have an array of frames that look like this :

frame n :
- time = tn
rigidbody 0 :
- position = p1n
- orientation = q1n
rigidbody 1:
- position = p2n
- orientation = q2n
...

During the replay, you just have to manually set the position/orientation of the rigidbodies by interpolatating between the different frames, like you said. It's like playing a skeleton animation. Also, you may want to disable the bodies during the replay to avoid unnecessary collisions.

I'm not very familiar with the physX callbacks (Update, LateUpdate...), but depending on the quality you want for the replay, you can call the record function either in FixedUpdate (more simple, because you don't have to store the time since the frames are recorded at fixed intervals), or LateUpdate (will give a smoother replay because more frames are created).

##### Share on other sites

Instead of trying to replicate and reproduce the physics in my replay, i think i will go with the keyframe system as you mentioned.

It just looks to be so less pain..

I have more questions though, i hope you can help me to clear my mind.

If during the game, i record something like this :

0.0 ==> carPosition.x = 0;

1.0 ==> carPosition.x = 10;

2.0 ==> carPosition.x = 20;

Because my game has no fixed time step, the framerate is every-time different. So if during the replay my camera angle is different, with more objects displayed on screen, i will never be able to reproduce the action at the exact same timing i recorded them..

Instead of being 0.0, 1.0, and 2.0 seconds it will probably play 0.03, 1.023, 2.054 etc...

Does that mean that i need to calculate the position of my car related to these value ?

For example if at recorded time 1.0 my car position is 10, when replayed at 1.023 it should be :

positionX = 20(next position) - 10(actualPosition) = 10 * 1.023(currentTime) = 10.23

Is that pseudo code correct ?

What i want to say is that even if the replay time is totally different from the actual recorded time, i should be able to "guess" the position of the car with mathematic.

Am i correct ?

And the last question, how often would i have to record keyframes ?

The more i record them the more accurate the replay will be right ?

Anyway, thank you very much, and i hope you can understand my English because i am not native speaker..

Best regards.

##### Share on other sites

i think i got it.

I kind of replied my own answer.

So, here is the formula to interpolate two values or two Vector3

//==================================================================
//  INTERPOLATE
//==================================================================
public float interpolate( float x1, float x2, float x3, float y1, float y3 ) {
return ( (x2 - x1) * (y3 - y1) / (x3 - x1) ) + y1;
}


x1 is the timing of the keyframe1

x2 is the current timing

x3 is the timing of the keyframe2

y1 is the recorded position of the keyframe1

y3 is the recorded position of the keyframe2

It will return y2 which is the interpolation of keyframe 1 and 2.

The position will change with the current timing

So basically, the interpolation can be done properly even if the framerate is not a fixed framerate

because the position can be calculated at any moment with the interpolation function.

Thats it, i hope it help.

##### Share on other sites

If you're in Unity (sounds like it), there's actually something that does this on the asset store that works pretty well

https://www.assetstore.unity3d.com/#/content/4341

At \$10, it's a steal, works well, covers physics and even sound...

##### Share on other sites

Sorry dude but i didn't came on gamedev physics forum to get someone who give me a link to a product.

I am here because i want to solve a problem and think with my head.

Otherwise i would have post on Unity forum don't you think so ?

Make sens ?

Sorry i don't want to sound rude.

##### Share on other sites

So basically, the interpolation can be done properly even if the framerate is not a fixed framerate

because the position can be calculated at any moment with the interpolation function.

Yes exactly !

Your equation is the right one for a linear interpolation, which, I think, is sufficient enough (you could do a polynomial interpolation with 3 or more keyframes, but it becomes complicated).

As you noted, the more keyframes you store, the more accurate the replay will be. When I implemented a replay system for a ball game, 30 frames were created per second, and it worked well. the replay duration was at most 5 second long, so the data buffer was very small.

##### Share on other sites

Now that i know what to do, i just need to implement it on my project.

Thanks again.

• 21
• 13
• 9
• 17
• 13