step method

void step(
  1. double dt, [
  2. double? timeSinceLastCalled,
  3. int maxSubSteps = 10
])

Step the physics world forward in time.

There are two modes. The simple mode is fixed timestepping without interpolation. In this case you only use the first argument. The second case uses interpolation. In that you also provide the time since the function was last used, as well as the maximum fixed timesteps to take.

@param dt The fixed time step size to use. @param timeSinceLastCalled The time elapsed since the function was last called. @param maxSubSteps Maximum number of fixed steps to take per function call (default: 10). @see https://web.archive.org/web/20180426154531/http://bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World#What_do_the_parameters_to_btDynamicsWorld::stepSimulation_mean.3F @example // fixed timestepping without interpolation world.step(1 / 60)

Implementation

void step(double dt, [double? timeSinceLastCalled, int maxSubSteps = 10]) {
  if (timeSinceLastCalled == null) {
    // Fixed, simple stepping

    internalStep(dt);

    // Increment time
    time += dt;
  } else {
    accumulator += timeSinceLastCalled;

    final t0 = performance.now();
    int substeps = 0;
    while (accumulator >= dt && substeps < maxSubSteps) {
      // Do fixed steps to catch up
      internalStep(dt);
      accumulator -= dt;
      substeps++;
      if (performance.now() - t0 > dt * 1000) {
        // The framerate is not interactive anymore.
        // We are below the target framerate.
        // Better bail out.
        break;
      }
    }

    // Remove the excess accumulator, since we may not
    // have had enough substeps available to catch up
    accumulator = accumulator % dt;

    final t = accumulator / dt;
    for (int j = 0; j != bodies.length; j++) {
      final b = bodies[j];
      b.previousPosition.lerp(b.position, t, b.interpolatedPosition);
      b.previousQuaternion.slerp(b.quaternion, t, b.interpolatedQuaternion);
      b.previousQuaternion.normalize();
    }
    time += timeSinceLastCalled;
  }
}