alignment static method

Vector3 alignment(
  1. Vector3 currentVel,
  2. List<Vector3> neighborVelocities, {
  3. double maxSpeed = 5.0,
  4. double maxForce = 10.0,
})

Computes an alignment force steering towards the average velocity of neighborVelocities.

Implementation

static vm.Vector3 alignment(
  vm.Vector3 currentVel,
  List<vm.Vector3> neighborVelocities, {
  double maxSpeed = 5.0,
  double maxForce = 10.0,
}) {
  if (neighborVelocities.isEmpty) return vm.Vector3.zero();
  var avgVel = vm.Vector3.zero();
  for (final v in neighborVelocities) {
    avgVel += v;
  }
  avgVel /= neighborVelocities.length.toDouble();
  if (avgVel.length2 == 0) return vm.Vector3.zero();

  final desired = avgVel.normalized() * maxSpeed;
  final force = desired - currentVel;
  return truncate(force, maxForce);
}