ConstantVolumeJoint constructor

ConstantVolumeJoint(
  1. World argWorld,
  2. ConstantVolumeJointDef<Body> def
)

Implementation

ConstantVolumeJoint(World argWorld, ConstantVolumeJointDef def)
    : _bodies = def.bodies.toList(growable: false),
      super(def) {
  _world = argWorld;
  if (def.bodies.length <= 2) {
    throw "Can't create a constant volume joint with less than three bodies.";
  }

  _targetLengths = Float64List(_bodies.length);
  for (var i = 0; i < _targetLengths.length; ++i) {
    final next = (i == _targetLengths.length - 1) ? 0 : i + 1;
    final dist = (_bodies[i].worldCenter - _bodies[next].worldCenter).length;
    _targetLengths[i] = dist;
  }
  _targetVolume = getBodyArea();

  if (def.joints.isNotEmpty && def.joints.length != def.bodies.length) {
    throw 'Incorrect joint definition. '
        'Joints have to correspond to the _bodies';
  }
  if (def.joints.isEmpty) {
    final distanceJointDef = DistanceJointDef();

    _distanceJoints.addAll(
      List<DistanceJoint>.generate(
        _bodies.length,
        (i) {
          final next = (i == _bodies.length - 1) ? 0 : i + 1;
          distanceJointDef.frequencyHz = def.frequencyHz; // 20.0;
          distanceJointDef.dampingRatio = def.dampingRatio; // 50.0;
          distanceJointDef.collideConnected = def.collideConnected;
          distanceJointDef.initialize(
            _bodies[i],
            _bodies[next],
            _bodies[i].worldCenter,
            _bodies[next].worldCenter,
          );

          final distanceJoint = DistanceJoint(distanceJointDef);
          _world.createJoint(distanceJoint);

          return distanceJoint;
        },
      ),
    );
  } else {
    _distanceJoints.clear();
    _distanceJoints.addAll(def.joints);
  }

  _normals = List<Vector2>.generate(_bodies.length, (_) => Vector2.zero());
}