5.9 KiB · text History 0297141
// The moving parts of the Bubbles and Absorbing spheres patterns. Both
// keep their discs in units of the canvas's shorter side (as the
// shaders do), step them with the field's clock, and hand them over as
// vec4 uniforms ball0 to ball47 (x, y, radius, and a fourth value of
// their own; unused ones zero): separate uniforms rather than an array,
// because Qt's ShaderEffect, for neiam-device, can't pass arrays.

export const MAX_BALLS = 48

const rand = (lo, hi) => lo + Math.random() * (hi - lo)
const TAU = 2 * Math.PI

// Off the walls: back inside, heading away.
function walls(b, [w, h]) {
  if (b.x - b.r < 0) (b.x = b.r), (b.vx = Math.abs(b.vx))
  if (b.x + b.r > w) (b.x = w - b.r), (b.vx = -Math.abs(b.vx))
  if (b.y - b.r < 0) (b.y = b.r), (b.vy = Math.abs(b.vy))
  if (b.y + b.r > h) (b.y = h - b.r), (b.vy = -Math.abs(b.vy))
}

function launch(extent, r, speed) {
  const a = Math.random() * TAU
  return {
    x: rand(r, Math.max(r, extent[0] - r)),
    y: rand(r, Math.max(r, extent[1] - r)),
    r,
    vx: speed * Math.cos(a),
    vy: speed * Math.sin(a),
  }
}

function pack(list, extra) {
  const out = {}
  for (let i = 0; i < MAX_BALLS; i++) {
    const b = list[i]
    out[`ball${i}`] = b ? [b.x, b.y, b.r, extra(b)] : [0, 0, 0, 0]
  }
  return out
}

// Soap bubbles drifting about, bouncing off the walls and each other
// (elastic, with mass by area). The fourth value is the phase their
// film's colours swirl through.
export class Bubbles {
  constructor() {
    this.list = []
  }

  step(dt, extent, n) {
    n = Math.min(MAX_BALLS, Math.max(1, Math.round(n)))
    while (this.list.length > n) this.list.pop()
    while (this.list.length < n) {
      const b = launch(extent, rand(0.035, 0.1), rand(0.009, 0.024))
      b.phase = Math.random()
      b.swirl = rand(0.02, 0.06) * (Math.random() < 0.5 ? -1 : 1)
      this.list.push(b)
    }

    for (const b of this.list) {
      b.x += b.vx * dt
      b.y += b.vy * dt
      b.phase = (b.phase + b.swirl * dt + 1) % 1
      walls(b, extent)
    }
    for (let i = 0; i < this.list.length; i++) {
      for (let j = i + 1; j < this.list.length; j++) {
        const a = this.list[i]
        const b = this.list[j]
        const dx = b.x - a.x
        const dy = b.y - a.y
        const dist = Math.hypot(dx, dy) || 1e-6
        const overlap = a.r + b.r - dist
        if (overlap <= 0) continue
        const nx = dx / dist
        const ny = dy / dist
        const ma = a.r * a.r
        const mb = b.r * b.r
        // Apart, the lighter one moving further.
        a.x -= (nx * overlap * mb) / (ma + mb)
        a.y -= (ny * overlap * mb) / (ma + mb)
        b.x += (nx * overlap * ma) / (ma + mb)
        b.y += (ny * overlap * ma) / (ma + mb)
        // And bounce, if they're closing.
        const closing = (a.vx - b.vx) * nx + (a.vy - b.vy) * ny
        if (closing > 0) {
          const ja = (2 * mb * closing) / (ma + mb)
          const jb = (2 * ma * closing) / (ma + mb)
          a.vx -= ja * nx
          a.vy -= ja * ny
          b.vx += jb * nx
          b.vy += jb * ny
        }
      }
    }
  }

  pack() {
    return { ...pack(this.list, b => b.phase), count: this.list.length }
  }
}

// Spheres launched in every direction at their own speeds. They bounce
// off the walls, but where two meet the larger eats into the smaller:
// the area the smaller loses the larger gains, with the momentum that
// came with it, the two just touching, until the smaller is gone. With
// three left, they hold a moment, fade, and a new set is launched.
// The fourth value picks each sphere's colour.
export class Absorbers {
  constructor() {
    this.list = []
    this.state = "in"
    this.fade = 0
    this.timer = 0
    this.n = 0
  }

  launchAll(extent, n) {
    this.n = n
    this.list = Array.from({ length: n }, () => {
      const b = launch(extent, rand(0.012, 0.045), rand(0.009, 0.03))
      b.shade = Math.random()
      return b
    })
  }

  step(dt, extent, n) {
    n = Math.min(MAX_BALLS, Math.max(4, Math.round(n)))
    if (!this.list.length || (n !== this.n && this.state !== "out")) {
      this.launchAll(extent, n)
      this.state = "in"
    }

    for (const b of this.list) {
      b.x += b.vx * dt
      b.y += b.vy * dt
      walls(b, extent)
    }
    this.absorb()

    if (this.state === "in") {
      this.fade = Math.min(1, this.fade + dt / 1.5)
      if (this.fade >= 1) this.state = "live"
    } else if (this.state === "live" && this.list.length <= 3) {
      this.state = "hold"
      this.timer = 4
    } else if (this.state === "hold" && (this.timer -= dt) <= 0) {
      this.state = "out"
    } else if (this.state === "out") {
      this.fade = Math.max(0, this.fade - dt / 2)
      if (this.fade <= 0) {
        this.launchAll(extent, n)
        this.state = "in"
      }
    }
  }

  absorb() {
    const list = this.list
    for (let i = 0; i < list.length; i++) {
      for (let j = i + 1; j < list.length; j++) {
        const a = list[i]
        const b = list[j]
        if (a.gone || b.gone) continue
        const dist = Math.hypot(b.x - a.x, b.y - a.y)
        if (dist >= a.r + b.r) continue
        const [big, small] = a.r >= b.r ? [a, b] : [b, a]
        const area = big.r * big.r + small.r * small.r
        // The radii that keep the area and just touch: big + small = dist.
        let grown = (dist + Math.sqrt(Math.max(0, 2 * area - dist * dist))) / 2
        let left = dist - grown
        if (left < 0.003 || grown >= dist) {
          grown = Math.sqrt(area)
          left = 0
          small.gone = true
        }
        const before = big.r * big.r
        const after = grown * grown
        const taken = after - before
        big.vx = (before * big.vx + taken * small.vx) / after
        big.vy = (before * big.vy + taken * small.vy) / after
        big.r = grown
        small.r = left
      }
    }
    this.list = list.filter(b => !b.gone)
  }

  pack() {
    return { ...pack(this.list, b => b.shade), count: this.list.length, fade: this.fade }
  }
}