228

Gravitational Swarm

Dense orbital particle cloud interacting with moving binary gravitational mass centers under softened Newtonian inverse-square laws.

Playground
60 FPS Canvas 2D
Click + Drag to interact with field
</>

Full Executable Algorithm Code

// 009 - Gravitational Swarm (particles)
// 1:1 Original algorithm engine source
function createGravitationalSwarm() {
  const MAX_PARTICLES = 2e3;
  const px = new Float32Array(MAX_PARTICLES);
  const py = new Float32Array(MAX_PARTICLES);
  const vx = new Float32Array(MAX_PARTICLES);
  const vy = new Float32Array(MAX_PARTICLES);
  return {
    setup(context) {
      const cx = context.width * 0.5;
      const cy = context.height * 0.5;
      for (let i = 0; i < MAX_PARTICLES; i++) {
        const angle = Math.random() * Math.PI * 2;
        const r = 50 + Math.random() * (context.width * 0.35);
        px[i] = cx + Math.cos(angle) * r;
        py[i] = cy + Math.sin(angle) * r;
        const vMag = Math.sqrt(800 / r);
        vx[i] = -Math.sin(angle) * vMag;
        vy[i] = Math.cos(angle) * vMag;
      }
    },
    render(context, timeState, params) {
      const { ctx, width, height } = context;
      const count = Math.min(MAX_PARTICLES, Number(params.particleCount || 1500));
      const G = Number(params.gravity || 900);
      const damping = 0.999;
      const t = timeState.time;
      ctx.fillStyle = "rgba(8, 9, 13, 0.15)";
      ctx.fillRect(0, 0, width, height);
      const cx1 = width * 0.5 + Math.cos(t * 0.7) * (width * 0.15);
      const cy1 = height * 0.5 + Math.sin(t * 0.7) * (height * 0.15);
      const cx2 = width * 0.5 - Math.cos(t * 0.7) * (width * 0.15);
      const cy2 = height * 0.5 - Math.sin(t * 0.7) * (height * 0.15);
      for (let i = 0; i < count; i++) {
        const dx1 = cx1 - px[i];
        const dy1 = cy1 - py[i];
        const d1Sq = dx1 * dx1 + dy1 * dy1 + 400;
        const f1 = G / (d1Sq * Math.sqrt(d1Sq));
        vx[i] += dx1 * f1;
        vy[i] += dy1 * f1;
        const dx2 = cx2 - px[i];
        const dy2 = cy2 - py[i];
        const d2Sq = dx2 * dx2 + dy2 * dy2 + 400;
        const f2 = G / (d2Sq * Math.sqrt(d2Sq));
        vx[i] += dx2 * f2;
        vy[i] += dy2 * f2;
        vx[i] *= damping;
        vy[i] *= damping;
        const oldX = px[i];
        const oldY = py[i];
        px[i] += vx[i];
        py[i] += vy[i];
        const speed = Math.sqrt(vx[i] * vx[i] + vy[i] * vy[i]);
        const hue = (200 + speed * 25 + t * 15) % 360;
        ctx.strokeStyle = hsla(hue, 90, 65, Math.min(0.9, speed * 0.2 + 0.3));
        ctx.lineWidth = Math.min(2.5, speed * 0.4 + 0.8);
        ctx.beginPath();
        ctx.moveTo(oldX, oldY);
        ctx.lineTo(px[i], py[i]);
        ctx.stroke();
      }
    }
  };
}

// Default parameters from content metadata
const defaultParams = [
  {
    "key": "particleCount",
    "label": "Orbital Stars",
    "type": "range",
    "min": 500,
    "max": 2000,
    "step": 100,
    "defaultValue": 1500,
    "description": "Number of particles"
  },
  {
    "key": "gravity",
    "label": "Gravitational Constant (G)",
    "type": "range",
    "min": 300,
    "max": 2000,
    "step": 50,
    "defaultValue": 900,
    "description": "Mass attraction intensity"
  }
];

if (!window.__art_instances) window.__art_instances = {};
if (!window.__art_instances['gravitational-swarm']) {
  const inst = typeof createGravitationalSwarm === 'function' ? createGravitationalSwarm() : null;
  if (inst && inst.setup) {
    inst.setup({ ctx, width, height, dpr: 1, aspectRatio: width / height }, defaultParams);
  }
  window.__art_instances['gravitational-swarm'] = inst;
}

const instance = window.__art_instances['gravitational-swarm'];
if (instance && instance.render) {
  instance.render(
    { ctx, width, height, dpr: 1, aspectRatio: width / height },
    { time, deltaTime: dt, frameCount: Math.floor(time * 60), fps: 60 },
    defaultParams
  );
}
Edit in Interactive Playground Zero Dependencies • Standalone Canvas 2D
ƒ

Mathematical Formulation

high
Analytical Equation
Fi=j=12GMj(rjpi)(rjpi2+ϵ2)3/2\mathbf{F}_i = \sum_{j=1}^{2} \frac{G M_j (\mathbf{r}_j - \mathbf{p}_i)}{(\|\mathbf{r}_j - \mathbf{p}_i\|^2 + \epsilon^2)^{3/2}}
Click to expand
Compact Formula
a_x = G * dx / (dx² + dy² + ε²)^(3/2), a_y = G * dy / (dx² + dy² + ε²)^(3/2)

Mathematical Tags

#gravity #n-body #physics #particles #orbit #celestial
Author: Math Art Core Target: 60 FPS

Export & Embed: Gravitational Swarm

4K PNG Snapshot

High-resolution single frame render

WebM Video (5s Loop)

60 FPS browser-captured stream

Standalone JS Script

Complete executable Canvas 2D algorithm

HTML Iframe Embed

<iframe src="https://art.fazleyrabbi.xyz/embed/gravitational-swarm" width="500" height="500" frameborder="0" loading="lazy"></iframe>
ESC
↑↓ Navigate Select
110 Mathematical Artworks
Made by Fazley Rabbi

Support the Project

Keep mathematical creative coding alive & open source

Or via Direct Payoneer ($0 Fee)
Payoneer Customer ID: $0 Fee Direct
24076084

💡 Payoneer app: Go to Pay → Pay to recipient → Enter ID 24076084.

Thank you for supporting generative mathematical animations! ✨