updatePosition method

bool updatePosition({
  1. required double scaling,
  2. required double minVelocity,
  3. required double maxStaticFriction,
  4. required double damping,
})

scaling: displacement scaling factor 0-1 return: whether it is in motion Within a time step, calculate the displacement of the node based on mechanics, also need to consider whether the current node is stationary, then consider the static friction, consider the dynamic friction

Implementation

bool updatePosition({
  required double scaling,
  required double minVelocity,
  required double maxStaticFriction,
  required double damping,
}) {
  if (_isFixed) {
    _force = Vector2.zero();
    _velocity = Vector2.zero();
    return false;
  }
  if (_velocity.length < minVelocity) {
    // static state
    if (_force.length < maxStaticFriction) {
      // If the force is too small in the static state, no calculation is required
      _velocity = Vector2.zero();
      _force = Vector2.zero();
      return false;
    }
  }

  // dynamic state
  final friction = -_velocity.normalized() * maxStaticFriction;
  _force += friction;
  _velocity += _force / mass;
  _velocity *= damping;
  position += _velocity * scaling;
  _force = Vector2.zero();
  return true;
}