initVelocityConstraints method

  1. @override
void initVelocityConstraints(
  1. SolverData data
)
override

Implementation

@override
void initVelocityConstraints(SolverData data) {
  _indexA = bodyA.islandIndex;
  _indexB = bodyB.islandIndex;
  _localCenterA.setFrom(bodyA.sweep.localCenter);
  _localCenterB.setFrom(bodyB.sweep.localCenter);
  _invMassA = bodyA.inverseMass;
  _invMassB = bodyB.inverseMass;
  _invIA = bodyA.inverseInertia;
  _invIB = bodyB.inverseInertia;

  final cA = data.positions[_indexA].c;
  final aA = data.positions[_indexA].a;
  final vA = data.velocities[_indexA].v;
  var wA = data.velocities[_indexA].w;

  final cB = data.positions[_indexB].c;
  final aB = data.positions[_indexB].a;
  final vB = data.velocities[_indexB].v;
  var wB = data.velocities[_indexB].w;

  final qA = Rot();
  final qB = Rot();
  final temp = Vector2.zero();

  qA.setAngle(aA);
  qB.setAngle(aB);

  // Compute the effective masses.
  temp
    ..setFrom(localAnchorA)
    ..sub(_localCenterA);
  _rA.setFrom(Rot.mulVec2(qA, temp));
  temp
    ..setFrom(localAnchorB)
    ..sub(_localCenterB);
  _rB.setFrom(Rot.mulVec2(qB, temp));

  _u
    ..setFrom(cB)
    ..add(_rB)
    ..sub(cA)
    ..sub(_rA);

  _length = _u.length;

  final c = _length - maxLength;
  if (c > 0.0) {
    _state = LimitState.atUpper;
  } else {
    _state = LimitState.inactive;
  }

  if (_length > settings.linearSlop) {
    _u.scale(1.0 / _length);
  } else {
    _u.setZero();
    _mass = 0.0;
    _impulse = 0.0;
    return;
  }

  // Compute effective mass.
  final crA = _rA.cross(_u);
  final crB = _rB.cross(_u);
  final invMass =
      _invMassA + _invIA * crA * crA + _invMassB + _invIB * crB * crB;

  _mass = invMass != 0.0 ? 1.0 / invMass : 0.0;

  if (data.step.warmStarting) {
    // Scale the impulse to support a variable time step.
    _impulse *= data.step.dtRatio;

    final pX = _impulse * _u.x;
    final pY = _impulse * _u.y;
    vA.x -= _invMassA * pX;
    vA.y -= _invMassA * pY;
    wA -= _invIA * (_rA.x * pY - _rA.y * pX);

    vB.x += _invMassB * pX;
    vB.y += _invMassB * pY;
    wB += _invIB * (_rB.x * pY - _rB.y * pX);
  } else {
    _impulse = 0.0;
  }

  data.velocities[_indexA].w = wA;
  data.velocities[_indexB].w = wB;
}