Reverse physics ?

Hey you guys,

I have an idea for a personal test project, in which I would like to throw rigidbodies (projectiles) at a wall or other rigidbodies and collapse it / move them with Physics, and then implement some kind of time reversal in which I would be able to build the wall anew in which the physics would play in reverse.

I really have no idea as to how to go about this in Unity. I do feel that storing, at every frame, each object’s name, position and rotation in an array (or a list) would be crazy in terms of memory usage and processing power and would be absolutely inefficient. Plus, this would only be feasible for a limited amount of time, and not for the whole game (not that I absolutely want to do this for the WHOLE game, but it would be cool it if I could…)

I was wondering if the PhysX engine had some sort of reverse button, or if I would be able to implement that reversal through code (by, for example, using a negative force, if that is even possible – even though the collision of two rigidbodies is not governed by a scripted force…)

I am at a loss here, so I would greatly appreciate any help from you learned folks here :slight_smile:

Thanks a bunch!

Ari ;o)

I suggest getting down at least some prototype, at least with couple objects, before optimizing it. Storing position and euler angles in a struct list seems like a good starting point to me.

edit: Note that i wouldnt mark the struct as serializable or serialize the list itself, to reduce possible bottleneck from unity editor inspector drawer.

You don’t have to store EVERY frame also. Depending on the complexity of the motion and how accurate you want the simulation, you could store the position and rotation every couple of frames and lerp in between them. That way you can store 3 or 4 times less data.

I’ve never used it, but for $40 this asset might seem a good investment for you:

hey there, from what I see you can just record every frame as a pic the just play them in reverse. It will gives players a feeling that the whole scene is reversing. The only con is you can not just rewind one object. The performance should be all right as it is just like playing video. hope it is clear with my poor English;)

So, after reading all of you guys’ suggestions here, I decided to try my hand at implementing something. Right now I’m storing name, position and rotation @ every frame (I’ll work on skipping frames and lerping later…), all into a giant “deported” list placed on an object.

Uh oh! While I’m typing this, I’m thinking this is awfully inefficient and I should save the data onto each object, not in a central list containing everything (which then has me looking for the gameObjects by name, which I read is a big no no…)

I’m going to try and assign a script to each object I want to reverse rather than centralize everything… More later! Thanks again to all of you who contribute to making those projects a little less lonely! :slight_smile:

It’s impossible, except if you mark down every time objects collided…

This may be old but it’s a fairly high result for anybody looking into this kind of thing.

I stored a Position/Rotation in a struct alongside two float values; the execution time of the action (any position/rotation update) and the action length time (when the state changed from what it is, to something else).

This allowed me to consolidate any action which had not changed since the previous loop just by extending the time the action runs for. It also means that any object which stays still only requires that it’s current state has it’s time extended, rather than appending a new state.

You can use this when “reversing time” to calculate, using the execution and run times, the duration the action should run for in reverse.