update method

void update(
  1. double deltaTime
)

Advance the simulation by deltaTime seconds.

When fixedTimestep is set this drains an accumulator in fixed fixedDeltaTime increments, so a frame may run zero, one or several sub-steps. Event buffers are cleared here rather than per sub-step so that events raised by an early sub-step survive until the caller polls them — the poll API is once-per-frame, not once-per-step.

Implementation

void update(double deltaTime) {
  _stepStopwatch
    ..reset()
    ..start();

  _lastResolvedCollisionCount = 0;
  _lastAwakeBodyCount = 0;

  // Cleared once per frame, not once per sub-step: _detectCollisions()
  // appends to these, and clearing inside it would discard everything the
  // first sub-step found the moment a second one ran.
  _sensorBeginBuffer.clear();
  _sensorEndBuffer.clear();
  _contactBeginBuffer.clear();
  _contactEndBuffer.clear();

  if (fixedTimestep) {
    _accumulator += deltaTime;
    final maxAccumulated = fixedDeltaTime * maxSubSteps;
    if (_accumulator > maxAccumulated) _accumulator = maxAccumulated;
    while (_accumulator >= fixedDeltaTime) {
      _stepFixed(fixedDeltaTime);
      _accumulator -= fixedDeltaTime;
    }
  } else {
    _accumulator = 0.0;
    _stepFixed(deltaTime);
  }

  // Body move events (Dart fallback: track awake transitions).
  _moveEventBuffer.clear();
  for (final body in _bodies) {
    if (body.isStatic) continue; // static bodies don't move
    final wasAwake = _prevAwake[body] ?? true;
    final fellAsleep = wasAwake && !body.isAwake;
    if (body.isAwake || fellAsleep) {
      _moveEventBuffer.add((body: body, fellAsleep: fellAsleep));
    }
    _prevAwake[body] = body.isAwake;
  }

  _stepStopwatch.stop();
  _lastStepMs = _stepStopwatch.elapsedMicroseconds / 1000.0;
}