Which physics library can also step backwards in time?

Started by
9 comments, last by skytiger 11 years, 4 months ago
Hello
I'm looking for a free, cross-platform (windows and linux is enough for me), C/C++ physics library which can also step physics back in time, not only forward in time.
For example, what I want to do looks like this:
[source lang="cpp"]world.update(0.05);
world.update(0.05);

...

world.update(-0.025);
world.update(-0.05);
world.update(-0.025);[/source]
And I want to get the same state in the end as it was at the beginning.
I have already tried Box2D and chipmunk physics. Box2D only stops time for negative values (does nothing), chipmunk does the thing (ok not very accurately) as long as there are no collisions. If there was a collision meanwhile, I get completely different result when rolling things back in time. Without any collisions it works.

Have you guys ever did this, or know which library can do it well?
I could even try ODE, if it can do this. It's 3D, but I can use it for 2D too I guess.
Advertisement
I think you are better creating a snap shot of the time frame, like save the positions of all the moving objects in a frame, and then when moving back just set all the objects to their previous snapshot state.

Check out my new blog: Morphexe

Since there are multiple previous states that can lead to the same state, you have to remember how you got there. Something like this was done in "Prince of persia the sands of time".
Generally you won't be able to reverse the simulation.
Simplest example is, drop a box on the floor. Now it's in rest. Reverse the time. Will the box lift up? No.

In general terms, if a non conservative force is involved in the simulation (in non elastic collisions, friction is involved) you won't be able to reverse the simulation.
If you want to do something like fwd(0.5) bwd(-0.25), you probably need to either:
a)Simulate backwards at same rate as forwards, then interpolate to the accurate position
b)Save snapshots at some interval, then either interpolate or simulate forward/backward from closest point (whichever works the best)

o3o

Thank you for your reply.

b)Save snapshots at some interval, then either interpolate or simulate forward/backward from closest point (whichever works the best)

So I'm going to do this. Set the position, rotation, velocity and angular velocity at the earlier of the two frames next to the desired time. Then advance physics by the difference between the desired time and the frame time.
This should be better than interpolating between two frames myself, because a collision might happen in the meantime.
You may find this interesting. All of the links Hodgman posted are worth checking out. If you're interested in forking out $4, you can get Jonathan Blow's presentation on implementing rewinding here.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Thank you for your reply.
[quote name='Waterlimon' timestamp='1355834227' post='5012016']
b)Save snapshots at some interval, then either interpolate or simulate forward/backward from closest point (whichever works the best)

So I'm going to do this. Set the position, rotation, velocity and angular velocity at the earlier of the two frames next to the desired time. Then advance physics by the difference between the desired time and the frame time.
This should be better than interpolating between two frames myself, because a collision might happen in the meantime.
[/quote]

Hi Vaclav,

I'm not sure what your specific use case is here, but be aware that physics systems don't like 'teleporting' collision geometry around. It is a slow process. Consider it (some what) equivalent to setting up the initial scene... more specially, when you teleport an object its potential collision lists will need to be re-updated entirely, rather than the presumably more optimized process used when moving an object with velocity. (Obviously every system/engine is different and this may or may not be applicable).

With the snapshot approach, while 're-winding' you don't need to move all the collision geometry around (only the visual geometry)... only once you go to re-start time do you need to move the collision geometry to the new location (and probably best to do this with a completely re-load of the physics world). This will only result in a performance spike when restarting the simulation which you can hide behind a transition effect.

Cheers,
Brad
The snapshot approach should be easy, however if you are trying to step to a specific point in time to view an event in a simulation, then there could be problems in stepping forwards. For example, imagine you have 2 objects that are headed towards each other and a snapshot before they collide, and a snapshot of a point in time after the collision, say for example there is a 1 second gap between the snapshots (for example). Now if you reset the objects locations and try to step forwards in 1/10 of a second increments to the point of collision then you are running the simulation with different settings. Sometimes changing the time step can alter the accuracy of the simulation by a large factor, and so you might even find a different result - i.e. a different ending position.
Well what I want to do is like a multiplayer racing game over network, so something with physics over network.
I have read some articles about it, here is what I think how it would work:
Say I have a client and a server. I measure the latency between them, one direction it's ping/2. Let's say ping is 200 ms, so one way latency is 100 ms.
The physics simulations runs on the server, but the client also tries to approximate it. If the client gets an input for example to move, sends it to the server that it wants to move. Then the server has to roll back time of the physics with 100 ms, because the action of the client happened at that time. Then apply the action, and go forward with time 100 ms again.
The server sends the actual positions and velocities let's say 10 times a second. When the client receives positions, the ones it got are the positions reflecting the physics state 100 ms earlier, so the client sets those positions and velocities and advances time by 100 ms.

For this, restarting the simulation every time when teleporting objects, would be too often to do. I'm going to use Box2D. I don't know if you have problems with collisions when setting the new positions and velocities.
So is it correct what I think of? Or do I make it overcomplicated?

This topic is closed to new replies.

Advertisement