setMassData method

void setMassData(
  1. MassData massData
)

Set the mass properties to override the mass properties of the fixtures. Note that this changes the center of mass position. Note that creating or destroying fixtures can also alter the mass. This function has no effect if the body isn't dynamic.

Implementation

void setMassData(MassData massData) {
  // TODO(Erin): adjust linear velocity and torque to account for movement of
  // center.
  assert(!world.isLocked);
  if (_bodyType != BodyType.dynamic) {
    return;
  }

  _inverseMass = 0.0;
  inertia = 0.0;
  inverseInertia = 0.0;

  _mass = massData.mass;
  if (_mass <= 0.0) {
    _mass = 1.0;
  }

  _inverseMass = 1.0 / _mass;

  if (massData.I > 0.0 && (flags & fixedRotationFlag) == 0.0) {
    inertia = massData.I - _mass * massData.center.dot(massData.center);
    assert(inertia > 0.0);
    inverseInertia = 1.0 / inertia;
  }

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

  // Update center of mass velocity.
  final temp = Vector2.copy(sweep.c)..sub(oldCenter);
  temp.scaleOrthogonalInto(_angularVelocity, temp);
  linearVelocity.add(temp);
}