Wednesday, December 2, 2015

simulations - Variable physics step, bad idea?


Currently, when I update the entities I calculate the time passed since the last update, and then pass that to their update function. They will in turn pass that duration to all their components.


    currentTime = clock.getTime()
timePassed = currentTime - lastUpdate

This means that the physics components also gets a variable time step, but it appears this is bad and can lead to inconsistent simulations. Here's some details on this:


> When should I use a fixed or variable time step?


The answers suggest that the physics simulation time step should be constant and higher than the rendering time step. But since I can't ensure how well the game will perform, this can be rather difficult to achieve.


One answer suggests this:




Use Gaffer's "fix your time step" approach. Update the game in fixed steps as in option 1, but do so multiple times per frame rendered - based on how much time has elapsed - so that the game logic keeps up with real time, while remaining in discrete steps.



This seems like a good approach to me, but I don't really understand how I should be doing it. Instead of doing:


physics.step(timePassed)

I should be doing the following?


while timePassed > 0:
physics.step(1) # millisecond
timePassed -= 1


Answer



The problem with the physic simulations lays on the integration step to solve equation of motion.


What you should do is simply to set an error in integration you can accept (ε) then look to your integration algorithm to see for what step size it gives you that error (ε(Δt)); let say that this error is Tmin.


Now if your elapsed time is less than Tmin a simple step may suffice; if is greater you have to split it in n substeps where each substep is less than Tmin (n = floor(elapsed / tmin); substep = elapsed / n).


Now everything is related to your ε: greather ε means more integration error but less iterations; little ε assures you both a better precision and more iterations.


Please keep in mind that integration errors sums up so your motion trajectory will diverge from the real one (more if the system is badly conditioned).


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...