obOB STUDIO
← All works
app★ flagship

Cosmic Gravity Sandbox

Newtonian gravity simulator with glowing orbital trails.

Loading Cosmic Gravity Sandbox

Tip: click Fullscreen for the best experience.

Six kinds of body and nothing to win

A full-screen canvas with Newton's law running on it and nothing to win. You press somewhere, drag, and let go: a body appears where you pressed and flies off along the drag. That is the whole interaction. It boots into the Solar System preset and leaves you alone.

Read the build notes ↓

About this project

A full-screen N-body gravity simulator. Click to place planets, stars, and black holes, drag to launch them, then watch real orbital mechanics unfold with glowing trails. Six body types, three collision models, curated presets (figure-eight, galaxy collision) and a live energy/center-of-mass HUD.

Built with

JavaScriptCanvas 2DPhysics

How to use it

Click and drag on empty space
Places a body where you pressed, not where you released; the drag draws its velocity arrow and letting go launches it. The drag length is multiplied by 0.05, so a 200-pixel pull gives a speed of 10.
Click without dragging
Drops the body at that spot with zero velocity, so it just falls toward whatever is nearest.
Shift + drag, or middle-mouse drag
Pans the camera. Desktop only, because neither input has a touch equivalent.
Mouse wheel
Zooms by a factor of 1.1 per notch. Also desktop only; I never wrote a pinch handler.
Touch drag
The same place-and-launch gesture as the mouse. The canvas sets touch-action to none, so dragging launches a body instead of scrolling the page.
Body type buttons (planet, star, gas giant, neutron star, black hole, asteroid)
Load that type's mass, radius and colour into the sliders. Picking the black hole also ticks Fixed for you, and does not untick it when you switch back to something else.
Mass slider, Radius slider, colour swatch, Fixed checkbox
Set what the next body will be: mass 1 to 5000, radius 2 to 60, any colour. Fixed pins it in place, so it pulls on everything else but never moves itself.
Preset buttons
Load Solar System, Binary Stars, Figure-Eight or Galaxy Collision. Each one clears the canvas and resets the camera, G to 1.00 and speed to 1.0x, because the hardcoded orbital velocities assume G is exactly 1.
Pause, Play and Clear in the bottom bar
Freeze or resume the physics, or wipe every body and recentre the camera. There are no keyboard shortcuts anywhere in this app; these are buttons only.
Speed and G sliders, Collision and Trails dropdowns
Speed runs 0.1x to 5x and G runs 0.1 to 5, both applied live. Collision switches between Fusion, Elastic Bounce and Annihilation; Trails switches between Line, Dots and None.

Build notes.

Six kinds of body and nothing to win

A full-screen canvas with Newton's law running on it and nothing to win. You press somewhere, drag, and let go: a body appears where you pressed and flies off along the drag. That is the whole interaction. It boots into the Solar System preset and leaves you alone.

The six types in the Forge panel are each just a mass, a radius and a colour. Asteroid is mass 10 at radius 2, planet 500 at 8, gas giant 1500 at 16, star 3000 at 24, neutron star 5000 at 4, black hole 10000 at 12. The neutron star is the interesting one, the densest thing on the list, 5000 of mass in a four-pixel dot, and it will pull an orbit apart from much further out than its size suggests.

The black hole button has a flaw I found only while writing this page. It writes 10000 into a mass slider whose max attribute is 5000, so the browser clamps the value while the label beside it still prints 10000. The hole you place weighs half what the readout claims.

What happens every frame

Every frame the engine walks all pairs of bodies, with no quadtree and no Barnes-Hut, and computes F = G*m1*m2 / (r-squared + 25). The 25 is a softening constant, EPSILON = 5, squared. Without it two bodies passing close get a force heading for infinity, one integration step turns that into a velocity in the thousands, and the body is gone for good. Softening is a small lie about short distances that keeps the sim from exploding.

Integration is semi-implicit Euler: velocity takes the acceleration first, then position takes the new velocity. It costs nothing over plain Euler and it stops closed orbits from unwinding.

Two things I would do differently. dt is the speed slider, not real elapsed time, so the sim runs at your monitor's refresh rate: a 144 Hz screen plays it 2.4 times faster than a 60 Hz one. And when two bodies touch, the collision handler rebuilds the bodies array and the frame returns immediately, discarding every force it had already accumulated. That is a dropped frame, and Galaxy Collision, which starts at 82 bodies and merges down to around 30, drops plenty early on.

Two of the four presets are wrong

Binary Stars is two 5000-mass stars 300 apart, each launched at speed 5. I picked 5 because it looked about right. A pair like that needs roughly 2.9 for a circular orbit and escapes at 4.08, so they never orbit. They leave. Separation is 300 at the start, 2126 by frame 300, 3993 by frame 600.

Figure-Eight is the same mistake wearing a nicer hat. The real figure-eight three-body orbit needs exact initial conditions. Mine are three equal 3000-mass bodies at x = -200, 0 and 200 with velocities I guessed. The momentum does cancel, which is the part I got right, so the centre-of-mass readout sits at (0, 0) and never moves. Run it forward and two of them capture each other while the third gets slung out the far side. Ordinary three-body chaos, not the shape printed on the button.

I have left both alone. Watching an orbit fail teaches more than being handed one that works, and Binary Stars is fixable from the front end: push the G slider to about 2.5 and the same two stars settle into a steady pair roughly 320 apart.

Watch the two bars, not the planets

The K and U bars in the top-left corner are the most useful thing on screen. They show kinetic and potential energy as a share of the total. A bound system settles near one third K and two thirds U, the virial theorem showing up as a progress bar. Load Solar System and the K bar sits around a third, 31 to 36 per cent, indefinitely. Load Binary Stars and it climbs: 60 per cent at the start, 81 by frame 300, 91 by frame 900. That climb is the system escaping itself.

The centre-of-mass readout is worth watching too. In Solar System the Sun starts at rest while the three planets carry about 7930 of momentum between them, all pointing the same way, spread across 16200 of total mass, so the whole system slides down the screen at 0.49 pixels a frame. The CoM y-value ticks up in a dead straight line: 196 at frame 400, 587 at 1200, 979 at 2000. Nothing is broken; the momentum has nowhere to go.

To break it on purpose, set Collision to Annihilation and drop a fixed black hole into a running system. Or set Trails to Dots and sweep the speed slider: the sampler records one point every fourth frame regardless of speed, so at 5x the dots spread into a dashed line and at 0.1x they bunch into a short stub.