Planets
Simulating celestial bodies
I have loved simulating bodies affected by gravity ever since I started programming; rudimentary approximations of physics lead to instant visual gratification.
I found a bunch of old experiments, some in Flash with a file date ~2011, one in JS ~2016.
When going the rudimentary route, mathematically one has to avoid divisions by numbers close to zero to prevent ridiculous slingshots, although, that’s how it would be in the real universe - if objects were points as with this experiment.
Many factors have to be fudged to make celestial simulators aesthetically pleasing - with the scale of the universe almost everything would look like distant stars - only planets and their moon(s) could be seen within the same viewport.
Source code:
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var circleRads = Math.PI * 2;
var wid = canvas.width;
var hei = canvas.height;
var hw = wid / 2;
var hh = hei / 2;
var maxR = hw; // Math.hypot(hw, hh);
var i, j, k, p;
var damping = 1; //0.999; // cute but it's space hey.
var particles = 0;
var arrParticles = [];
var bodies = 0;
var arrBodies = [];
var rand = (min, max) => min + Math.random() * (max - min);
var randInt = (min, max) => Math.round(rand(min, max));
function celestialBody(parent, d, m, v) {
return (arrBodies[bodies++] = {
depth: parent ? parent.depth + 1 : 0,
parent,
d: d * maxR,
v: v * 0.001, // * Math.random(),
colour: parent ? `hsl(${rand(100, 300)}, ${rand(20, 80)}%, ${rand(20, 80)}%)` : "#fd4", // sun
m, // mass isn't really used
a: rand(0, circleRads),
r: m * 20,
});
}
function drawPlanets(t) {
j = -1;
while (++j < bodies) {
var planet = arrBodies[j];
var parent = planet.parent;
if (parent) {
planet.x = parent.x + Math.sin(planet.a + t * planet.v) * planet.d;
planet.y = parent.y + Math.cos(planet.a + t * planet.v) * planet.d;
} else {
// sun
planet.x = hw;
planet.y = hh;
}
ctx.beginPath();
ctx.fillStyle = planet.colour;
ctx.arc(planet.x, planet.y, planet.r, 0, circleRads, false);
ctx.fill();
}
}
function clusterBomb() {
var baseAngle = rand(0, circleRads);
k = -1;
while (++k < 1200) {
shoot(rand(baseAngle, baseAngle + 0.01), 0);
}
}
function swarm() {
k = -1;
while (++k < 1200) {
shoot(rand(0, circleRads), 0);
}
}
function missile() {
shoot(rand(0, circleRads), 20);
}
function kill(z) {
particles--;
arrParticles[z] = arrParticles[particles];
arrParticles.length = particles;
}
function shoot(angle, deviate) {
var x = hw + Math.sin(angle) * maxR;
var y = hh + Math.cos(angle) * maxR;
var dx = hw + Math.sin(angle) * deviate - x;
var dy = hh + Math.cos(angle) * deviate - y;
var velocity = rand(0.0001, 0.005);
var vx = dx * velocity;
var vy = dy * velocity;
arrParticles[particles++] = {
x,
y,
vx,
vy,
colour: `hsl(${rand(0, 360)}, 30%, 80%)`,
};
}
function drawIt(t) {
requestAnimationFrame(drawIt);
ctx.fillStyle = "rgba(0,0,0,0.5)";
ctx.fillRect(0, 0, wid, hei);
drawPlanets(t);
i = particles;
particleLoop: while (i--) {
p = arrParticles[i];
var fx = 0,
fy = 0;
j = bodies;
while (j--) {
var planet = arrBodies[j];
var dx = planet.x - p.x;
var dy = planet.y - p.y;
var distance = Math.hypot(dx, dy) + 0.01;
if (distance < planet.r) {
// hit
kill(i);
continue particleLoop;
}
var gravity = 10 / distance;
if (gravity > 0.05) {
ctx.strokeStyle = planet.colour;
ctx.lineWidth = 20 / distance;
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(planet.x, planet.y);
ctx.stroke();
}
// this is not correct physics, artistic license:
fx += (dx / distance) * gravity;
fy += (dy / distance) * gravity;
}
p.vx += fx;
p.vy += fy;
p.vx *= damping;
p.vy *= damping;
p.x += p.vx;
p.y += p.vy;
ctx.beginPath();
ctx.fillStyle = p.colour;
ctx.arc(p.x, p.y, 2, 0, circleRads, false);
ctx.fill();
}
ctx.font = "18px sans-serif";
ctx.fillStyle = "#fff8";
var counts = [];
arrBodies.forEach((b) => {
counts[b.depth] = (counts[b.depth] || 0) + 1;
});
var [_sc, pc, mc] = counts;
ctx.fillText(`${particles} asteroids / ${pc} planets / ${mc || 0} moons`, 15, 30);
}
var sun = celestialBody(null, 0, 1, 0);
var earth = celestialBody(sun, 0.3, 0.5, 0.1);
var earthMoon = celestialBody(earth, 0.05, 0.2, 0.8);
missile();
missile();
drawIt(0);
function newSystem() {
bodies = 0;
arrBodies = [];
var sun = celestialBody(null, 0, 1, 0);
var tp = randInt(2, 6);
var j = -1;
var pd = 0;
while (++j < tp) {
pd += rand(0.1, 0.3);
var mass = rand(0.2, 0.5);
var planet = celestialBody(sun, pd, mass, rand(0.05, 0.2));
var tm = randInt(0, 2);
var k = -1;
while (++k < tm) {
celestialBody(planet, rand(0.02, 0.1), rand(0.05, 0.2), rand(0.5, 1));
}
}
}
enableExpButtons("canvas-planets", [
["Asteroid", missile],
["Asteroid field", clusterBomb],
["Swarm", swarm],
["New System", newSystem],
]);
In honour of its creation time, function and var have been retained.
In this version the larger bodies are moving on fixed orbits in that they are unaffected by gravity amongst themselves, to keep the system stable. Not doing so would result in something akin to the three-body problem. There is so much room for improvement: asteroid belts, planet detail, planetary rings, collisions generating more asteroids, sun casting light/shadows, a more accurate gravity pull, the list goes on…
I love the havoc if you don’t clear the screen and let these experiments run for a long time. Here’s a screen capture from an earlier mentioned .SWF file after it was running for a few hours.

This is the chaos of the universe in which we exist.
This code was modified in 2026 to make it presentable.