casing function

CasingState casing(
  1. double x,
  2. double y,
  3. double vx,
  4. double vy,
  5. double va,
  6. double age,
  7. double g,
  8. double floor, [
  9. double bounce = 0.42,
  10. double a0 = 0,
])

Evaluates closed-form bouncing casing kinematics at age.

Implementation

CasingState casing(
  double x,
  double y,
  double vx,
  double vy,
  double va,
  double age,
  double g,
  double floor, [
  double bounce = 0.42,
  double a0 = 0,
]) {
  var t0 = 0.0;
  var a = a0;
  var hit = -1.0;
  for (var n = 0; n < 6; n++) {
    final d = floor - y;
    final tBounce = (-vy + math.sqrt(math.max(0.0, vy * vy + 2 * g * d))) / g;
    if (age - t0 < tBounce) {
      final u = age - t0;
      return CasingState(
        x: x + vx * u,
        y: y + vy * u + 0.5 * g * u * u,
        a: a + va * u,
        rest: false,
        hit: hit,
      );
    }
    x += vx * tBounce;
    a += va * tBounce;
    t0 += tBounce;
    hit = age - t0;
    final vin = vy + g * tBounce;
    y = floor;
    if (vin * bounce < 45) {
      final u = age - t0;
      const k = 7.0;
      final s = (1 - math.exp(-k * u)) / k;
      return CasingState(
        x: x + vx * 0.6 * s,
        y: floor,
        a: (a / math.pi + 0.5).floor() * math.pi,
        rest: true,
        hit: hit,
      );
    }
    vy = -vin * bounce;
    vx *= 0.55;
    va *= 0.6;
  }
  return CasingState(
    x: x,
    y: floor,
    a: (a / math.pi + 0.5).floor() * math.pi,
    rest: true,
    hit: hit,
  );
}