resetMassData method

void resetMassData()

This resets the mass properties to the sum of the mass properties of the fixtures. This normally does not need to be called unless you called setMassData to override the mass and you later want to reset the mass.

Implementation

void resetMassData() {
  // Compute mass data from shapes. Each shape has its own density.
  _mass = 0.0;
  _inverseMass = 0.0;
  inertia = 0.0;
  inverseInertia = 0.0;
  sweep.localCenter.setZero();

  // Static and kinematic bodies have zero mass.
  if (_bodyType == BodyType.static || _bodyType == BodyType.kinematic) {
    sweep.c0.setFrom(transform.p);
    sweep.c.setFrom(transform.p);
    sweep.a0 = sweep.a;
    return;
  }

  assert(_bodyType == BodyType.dynamic);

  // Accumulate mass over all fixtures.
  final localCenter = Vector2.zero();
  final temp = Vector2.zero();
  final massData = _pmd;
  for (final f in fixtures) {
    if (f.density == 0.0) {
      continue;
    }
    f.getMassData(massData);
    _mass += massData.mass;
    (temp..setFrom(massData.center)).scale(massData.mass);
    localCenter.add(temp);
    inertia += massData.I;
  }

  // Compute center of mass.
  if (_mass > 0.0) {
    _inverseMass = 1.0 / _mass;
    localCenter.scale(_inverseMass);
  } else {
    // Force all dynamic bodies to have a positive mass.
    _mass = 1.0;
    _inverseMass = 1.0;
  }

  if (inertia > 0.0 && (flags & fixedRotationFlag) == 0.0) {
    // Center the inertia about the center of mass.
    inertia -= _mass * localCenter.dot(localCenter);
    assert(inertia > 0.0);
    inverseInertia = 1.0 / inertia;
  } else {
    inertia = 0.0;
    inverseInertia = 0.0;
  }

  // Move center of mass.
  final oldCenter = Vector2.copy(sweep.c);
  sweep.localCenter.setFrom(localCenter);
  sweep.c0.setFrom(Transform.mulVec2(transform, sweep.localCenter));
  sweep.c.setFrom(sweep.c0);

  // Update center of mass velocity.
  (temp..setFrom(sweep.c)).sub(oldCenter);

  final temp2 = oldCenter;
  temp.scaleOrthogonalInto(_angularVelocity, temp2);
  linearVelocity.add(temp2);
}