moveToTarget method

void moveToTarget(
  1. Vector2 target,
  2. double ds
)

Smoothly moves this Vector2 in the direction target by a displacement given by a distance ds in that direction.

It does not goes beyond target, regardless of ds, so the final value is always target.

Note: ds is the displacement vector in units of the vector space. It is not a percentage (relative value).

Implementation

void moveToTarget(
  Vector2 target,
  double ds,
) {
  if (this != target) {
    final diff = _reusableVector
      ..setFrom(target)
      ..sub(this);

    if (diff.length < ds) {
      setFrom(target);
    } else {
      diff.scaleTo(ds);
      setFrom(this + diff);
    }
  }
}