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;
  _invMass = 0.0;
  _I = 0.0;
  _invI = 0.0;
  _sweep.localCenter.setZero();

  // Static and kinematic bodies have zero mass.
  if (_bodyType == BodyType.STATIC || _bodyType == BodyType.KINEMATIC) {
    // _sweep.c0 = _sweep.c = _xf.position;
    _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 Vector2 localCenter = world.getPool().popVec2();
  localCenter.setZero();
  final Vector2 temp = world.getPool().popVec2();
  final MassData massData = _pmd;
  for (Fixture f = _fixtureList; f != null; f = f._next) {
    if (f._density == 0.0) {
      continue;
    }
    f.getMassData(massData);
    _mass += massData.mass;
    // center += massData.mass * massData.center;
    (temp..setFrom(massData.center)).scale(massData.mass);
    localCenter.add(temp);
    _I += massData.I;
  }

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

  if (_I > 0.0 && (_flags & FIXED_ROTATION_FLAG) == 0.0) {
    // Center the inertia about the center of mass.
    _I -= _mass * localCenter.dot(localCenter);
    assert(_I > 0.0);
    _invI = 1.0 / _I;
  } else {
    _I = 0.0;
    _invI = 0.0;
  }

  Vector2 oldCenter = world.getPool().popVec2();
  // Move center of mass.
  oldCenter.setFrom(_sweep.c);
  _sweep.localCenter.setFrom(localCenter);
  // _sweep.c0 = _sweep.c = Mul(_xf, _sweep.localCenter);
  Transform.mulToOutUnsafeVec2(_transform, _sweep.localCenter, _sweep.c0);
  _sweep.c.setFrom(_sweep.c0);

  // Update center of mass velocity.
  // _linearVelocity += Cross(_angularVelocity, _sweep.c - oldCenter);
  (temp..setFrom(_sweep.c)).sub(oldCenter);

  final Vector2 temp2 = oldCenter;
  temp.scaleOrthogonalInto(_angularVelocity, temp2);
  _linearVelocity.add(temp2);

  world.getPool().pushVec2(3);
}