solvePositionConstraints method

  1. @override
bool solvePositionConstraints(
  1. SolverData data
)
override

This returns true if the position errors are within tolerance. Internal.

Implementation

@override
bool solvePositionConstraints(SolverData data) {
  final qA = Rot();
  final qB = Rot();
  final rA = Vector2.zero();
  final rB = Vector2.zero();
  final uA = Vector2.zero();
  final uB = Vector2.zero();
  final temp = Vector2.zero();
  final pA = Vector2.zero();
  final pB = Vector2.zero();

  final cA = data.positions[_indexA].c;
  var aA = data.positions[_indexA].a;
  final cB = data.positions[_indexB].c;
  var aB = data.positions[_indexB].a;

  qA.setAngle(aA);
  qB.setAngle(aB);
  temp
    ..setFrom(localAnchorA)
    ..sub(_localCenterA);
  rA.setFrom(Rot.mulVec2(qA, temp));
  temp
    ..setFrom(localAnchorB)
    ..sub(_localCenterB);
  rB.setFrom(Rot.mulVec2(qB, temp));

  uA
    ..setFrom(cA)
    ..add(rA)
    ..sub(_groundAnchorA);
  uB
    ..setFrom(cB)
    ..add(rB)
    ..sub(_groundAnchorB);

  final lengthA = uA.length;
  final lengthB = uB.length;

  if (lengthA > 10.0 * settings.linearSlop) {
    uA.scale(1.0 / lengthA);
  } else {
    uA.setZero();
  }

  if (lengthB > 10.0 * settings.linearSlop) {
    uB.scale(1.0 / lengthB);
  } else {
    uB.setZero();
  }

  // Compute effective mass.
  final ruA = rA.cross(uA);
  final ruB = rB.cross(uB);

  final mA = _invMassA + _invIA * ruA * ruA;
  final mB = _invMassB + _invIB * ruB * ruB;

  var mass = mA + _ratio * _ratio * mB;

  if (mass > 0.0) {
    mass = 1.0 / mass;
  }

  final c = _constant - lengthA - _ratio * lengthB;
  final linearError = c.abs();

  final impulse = -mass * c;

  pA
    ..setFrom(uA)
    ..scale(-impulse);
  pB
    ..setFrom(uB)
    ..scale(-_ratio * impulse);

  cA.x += _invMassA * pA.x;
  cA.y += _invMassA * pA.y;
  aA += _invIA * rA.cross(pA);
  cB.x += _invMassB * pB.x;
  cB.y += _invMassB * pB.y;
  aB += _invIB * rB.cross(pB);

  data.positions[_indexA].a = aA;
  data.positions[_indexB].a = aB;

  return linearError < settings.linearSlop;
}