Jump to content

  • Log In with Google      Sign In   
  • Create Account

Which physics library can also step backwards in time?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Vaclav   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 December 2012 - 05:48 AM

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.

Sponsor:

#2 Morphex   Members   -  Reputation: 298

Like
4Likes
Like

Posted 18 December 2012 - 06:28 AM

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 


#3 Dawoodoz   Members   -  Reputation: 299

Like
3Likes
Like

Posted 18 December 2012 - 06:29 AM

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".

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 max343   Members   -  Reputation: 340

Like
4Likes
Like

Posted 18 December 2012 - 06:36 AM

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.

#5 Waterlimon   Crossbones+   -  Reputation: 2561

Like
3Likes
Like

Posted 18 December 2012 - 06:37 AM

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


#6 Vaclav   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 December 2012 - 08:54 AM

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.

#7 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 18 December 2012 - 10:38 AM

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.
[ 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 ]

#8 clanston   Members   -  Reputation: 122

Like
2Likes
Like

Posted 18 December 2012 - 05:45 PM

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.


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 ray_intellect   Crossbones+   -  Reputation: 258

Like
0Likes
Like

Posted 18 December 2012 - 08:30 PM

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.

#10 Vaclav   Members   -  Reputation: 136

Like
0Likes
Like

Posted 19 December 2012 - 10:31 AM

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?

#11 skytiger   Members   -  Reputation: 258

Like
0Likes
Like

Posted 20 December 2012 - 06:40 AM

FWIW

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)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS