Gravitational Swarm
Dense orbital particle cloud interacting with moving binary gravitational mass centers under softened Newtonian inverse-square laws.
60 FPS • Canvas 2D
Click + Drag to interact with field
</>
Full Executable Algorithm Code
107 lines
3656 chars
// 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=1∑2(∥rj−pi∥2+ϵ2)3/2GMj(rj−pi)
Click to expand
∑
Gravitational Swarm
Full Mathematical System • particles
100%
Complete System of Equations
[Governing Law][Discrete Progression][Domain & Space][Parameter State]Fi=j=1∑2(∥rj−pi∥2+ϵ2)3/2GMj(rj−pi)ax=G⋅dx/(dx2+dy2+ε2)(3/2),ay=G⋅dy/(dx2+dy2+ε2)(3/2)x∈R2,t∈R+,ω∈[0,2π]λparticleCount=1500(Orbital Stars),λgravity=900(Gravitational Constant (G))
Fi=j=1∑2(∥rj−pi∥2+ϵ2)3/2GMj(rj−pi)
Computational Implementation (JavaScript Engine Equivalent)
a_x = G * dx / (dx² + dy² + ε²)^(3/2), a_y = G * dy / (dx² + dy² + ε²)^(3/2) 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
Press ESC or F to exit