attractParticles function

int attractParticles(
  1. List<SimParticle> list,
  2. double x,
  3. double y,
  4. double G,
  5. double reach,
  6. double swirl,
  7. double r0,
  8. double dt, [
  9. double w(
    1. SimParticle q
    )?,
  10. double vmax = double.infinity,
])

Pull SimParticles toward (x, y) with softened gravity, swirl and drag.

Implementation

int attractParticles(
  List<SimParticle> list,
  double x,
  double y,
  double G,
  double reach,
  double swirl,
  double r0,
  double dt, [
  double Function(SimParticle q)? w,
  double vmax = double.infinity,
]) {
  int eaten = 0;
  for (int i = list.length - 1; i >= 0; i--) {
    final q = list[i];
    final wt = w != null ? w(q) : 1.0;
    if (wt == 0 || q.delay > 0) continue;
    final dx = x - q.x;
    final dy = y - q.y;
    final d2 = dx * dx + dy * dy;
    if (d2 > reach * reach) continue;
    final d = d2 > 0 ? math.sqrt(d2) : 1.0;
    if (d < r0) {
      list.removeAt(i);
      eaten++;
      continue;
    }
    final a =
        (G / (d2 + r0 * r0)) * math.min(1.0, (reach - d) / (reach * 0.25)) * wt * dt;
    final ux = dx / d;
    final uy = dy / d;
    q.vx += (ux - uy * swirl) * a;
    q.vy += (uy + ux * swirl) * a;
    final drag = math.min(0.2, (1 - d / reach) * 1.6 * dt);
    q.vx -= q.vx * drag;
    q.vy -= q.vy * drag;
    final v = hypot(q.vx, q.vy);
    if (v > vmax) {
      q.vx *= vmax / v;
      q.vy *= vmax / v;
    }
  }
  return eaten;
}