• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Replay Physics

7 posts in this topic

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.

What would you think about this option ?

Does some professional games use it ?


Any feedback or advices would be very appreciated.

Thank you in advance.

Edited by jujunosuke

Share this post

Link to post
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 this post

Link to post
Share on other sites

dacingmad, thank you very much for your feedback.

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 this post

Link to post
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

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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
Share on other sites

dancingmad, thank you for your feedback.

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


Thanks again.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0