Which physics library can also step backwards in time?
#1 Members - Reputation: 122
Posted 18 December 2012 - 05:48 AM
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.
#2 Members - Reputation: 301
Posted 18 December 2012 - 06:28 AM
Check out my new blog: Morphexe
#3 Members - Reputation: 280
Posted 18 December 2012 - 06:29 AM
My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz
"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."
#4 Members - Reputation: 332
Posted 18 December 2012 - 06:36 AM
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.
#5 Members - Reputation: 1001
Posted 18 December 2012 - 06:37 AM
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)
#6 Members - Reputation: 122
Posted 18 December 2012 - 08:54 AM
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.b)Save snapshots at some interval, then either interpolate or simulate forward/backward from closest point (whichever works the best)
This should be better than interpolating between two frames myself, because a collision might happen in the meantime.
#7 Moderator* - Reputation: 5339
Posted 18 December 2012 - 10:38 AM
#8 Members - Reputation: 122
Posted 18 December 2012 - 05:45 PM
Thank you for your reply.
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.
b)Save snapshots at some interval, then either interpolate or simulate forward/backward from closest point (whichever works the best)
This should be better than interpolating between two frames myself, because a collision might happen in the meantime.
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
#9 Crossbones+ - Reputation: 219
Posted 18 December 2012 - 08:30 PM
#10 Members - Reputation: 122
Posted 19 December 2012 - 10:31 AM
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?
#11 Members - Reputation: 195
Posted 20 December 2012 - 06:40 AM
I created my own physics engine that ensures that all motion is constant-velocity and all changes to velocity happen in instantaneous "key frames" (impulses?) that are created at least N times per second but also for every collision
this allows me to scrub backwards and forwards in time by using simple linear interpolation between key frames
also it means that I swap some simulation accuracy (mainly drag effects) for "perfect" collisions so I don't have any interpenetration issues
(so far I haven't needed more than 1000 frames with up to 16 objects (pos + quaternion) covering around 12 seconds)






