Search Unity

How to get reliable physics simulation?

Discussion in 'Physics' started by trolollinger, Jan 15, 2021.

  1. trolollinger

    trolollinger

    Joined:
    Oct 29, 2019
    Posts:
    5
    hi,

    I'm running a number of physics simulation in parallel as part of a genetic algorithm experiment.
    to keep the training time short, I want the simulations to run as fast as possible. whereas, when replaying the simulation, I want them to run in real-time.
    for that reason, and for making the simulation as deterministic as possible, I am disabling Physics.autoSimulation, and go through the simulation in fixed time steps:

    Code (CSharp):
    1. IEnumerator simulate() {
    2.     Physics.autoSimulation = false;
    3.     // Application.targetFrameRate = 50;
    4.  
    5.     float maxDuration = 10f;
    6.     float timeStep = Time.fixedDeltaTime;
    7.     float t = 0f;
    8.  
    9.     while (true) {
    10.         if (t > maxDuration) {
    11.             break;
    12.         }
    13.  
    14.         Physics.Simulate(timeStep);
    15.         t += timeStep;
    16.  
    17.         // a)
    18.         // expectation: runs as fast as possible
    19.         // reality: physics don't seem to get applied at all
    20.         continue;
    21.  
    22.         // b)
    23.         // expectation: runs as fast as possible
    24.         // reality: speed seems to be real-time, more or less
    25.         yield return new WaitForSecondsRealtime(0f);
    26.  
    27.         // c)
    28.         // expectation: proper physics (same as b)), but slowed down
    29.         // reality: slow indeed, but collisions don't work reliably, for instance
    30.         yield return new WaitForSecondsRealtime(0.5f);
    31.     }
    32.  
    33.     yield break;
    34. }
    the problem I am encountering though is that despite calling Physics.Simulate(timeStep) with constant values, the presence or absence of WaitForSecondsRealtime in the loop seems to have an influence on the physics simulation.

    I'm at a loss here. the whole point of controlling physics yourself is that it is independent of the "wall time" (the real time passed). — how do I properly do this?

    thanks a lot in advance!