cohesion static method

Vector3 cohesion(
  1. Vector3 currentPos,
  2. Vector3 currentVel,
  3. List<Vector3> neighborPositions, {
  4. double maxSpeed = 5.0,
  5. double maxForce = 10.0,
})

Computes a cohesion force steering towards the center of mass of neighborPositions.

Implementation

static vm.Vector3 cohesion(
  vm.Vector3 currentPos,
  vm.Vector3 currentVel,
  List<vm.Vector3> neighborPositions, {
  double maxSpeed = 5.0,
  double maxForce = 10.0,
}) {
  if (neighborPositions.isEmpty) return vm.Vector3.zero();
  var center = vm.Vector3.zero();
  for (final p in neighborPositions) {
    center += p;
  }
  center /= neighborPositions.length.toDouble();
  return seek(
    currentPos,
    currentVel,
    center,
    maxSpeed: maxSpeed,
    maxForce: maxForce,
  );
}