arrive static method

Vector3 arrive(
  1. Vector3 currentPos,
  2. Vector3 currentVel,
  3. Vector3 targetPos, {
  4. double slowingRadius = 3.0,
  5. double maxSpeed = 5.0,
  6. double maxForce = 10.0,
})

Steers towards targetPos, decelerating smoothly inside slowingRadius.

Implementation

static vm.Vector3 arrive(
  vm.Vector3 currentPos,
  vm.Vector3 currentVel,
  vm.Vector3 targetPos, {
  double slowingRadius = 3.0,
  double maxSpeed = 5.0,
  double maxForce = 10.0,
}) {
  final toTarget = targetPos - currentPos;
  final distance = toTarget.length;
  if (distance < 0.001) return -currentVel;

  final rampedSpeed = maxSpeed * (distance / slowingRadius);
  final targetSpeed = math.min(rampedSpeed, maxSpeed);
  final desiredVel = (toTarget / distance) * targetSpeed;
  final force = desiredVel - currentVel;
  return truncate(force, maxForce);
}