setMassData method

void setMassData (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.

@param massData the mass properties.

Implementation

void setMassData(MassData massData) {
  // TODO_ERIN adjust linear velocity and torque to account for movement of center.
  assert(world.isLocked() == false);
  if (world.isLocked() == true) {
    return;
  }

  if (_bodyType != BodyType.DYNAMIC) {
    return;
  }

  _invMass = 0.0;
  _I = 0.0;
  _invI = 0.0;

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

  _invMass = 1.0 / _mass;

  if (massData.I > 0.0 && (_flags & FIXED_ROTATION_FLAG) == 0.0) {
    _I = massData.I - _mass * massData.center.dot(massData.center);
    assert(_I > 0.0);
    _invI = 1.0 / _I;
  }

  final Vector2 oldCenter = world.getPool().popVec2();
  // Move center of mass.
  oldCenter.setFrom(_sweep.c);
  _sweep.localCenter.setFrom(massData.center);
  // _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);
  final Vector2 temp = world.getPool().popVec2();
  (temp..setFrom(_sweep.c)).sub(oldCenter);
  temp.scaleOrthogonalInto(_angularVelocity, temp);
  _linearVelocity.add(temp);

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