constrain method

  1. @override
void constrain(
  1. ActorNode node
)
override

Implementation

@override
void constrain(ActorNode node) {
  ActorNode? t = target as ActorNode?;
  if (t == null) {
    return;
  }

  ActorNode p = parent!;
  Vec2D targetTranslation = t.getWorldTranslation(Vec2D());
  Vec2D ourTranslation = p.getWorldTranslation(Vec2D());

  Vec2D toTarget = Vec2D.subtract(Vec2D(), ourTranslation, targetTranslation);
  double currentDistance = Vec2D.length(toTarget);
  switch (_mode) {
    case DistanceMode.closer:
      if (currentDistance < _distance) {
        return;
      }
      break;

    case DistanceMode.further:
      if (currentDistance > _distance) {
        return;
      }
      break;
  }

  if (currentDistance < 0.001) {
    return;
  }

  Vec2D.scale(toTarget, toTarget, 1.0 / currentDistance);
  Vec2D.scale(toTarget, toTarget, _distance);

  Mat2D world = p.worldTransform;
  Vec2D position = Vec2D.lerp(Vec2D(), ourTranslation,
      Vec2D.add(Vec2D(), targetTranslation, toTarget), strength);
  world[4] = position[0];
  world[5] = position[1];
}