Inspiration

I got hooked on the Kardashev and Barrow scales after watching Kurzgesagt – In a Nutshell. I wanted the physics behind the videos, so I read Brian Greene's The Elegant Universe and The Hidden Reality. Sci-fi had already shown me what these civilizations might look like, from the planet-cracking powers of Red Rising to the post-singularity civilizations of Charles Stross's Eschaton series.

When I saw the SpaceX track at MHacks, I wanted to build the thing I'd always pictured while reading: a simulator where you can climb these civilizations yourself. You'd harness a planet, then a star, then a galaxy, or go the other way down into genes, atoms and particles. Real space data and real physics would decide what's actually possible.

What it does

COSMOS is a voice-controlled, multiplayer universe simulator with six civilizations on two ladders.

Ladder Level You control Example
Kardashev (outward, energy) K1 A planet (~$10^{16}$ W) Cover part of the Moon in solar collectors
K2 A star (~$10^{26}$ W) Build a Dyson swarm around the Sun
K3 A galaxy (~$10^{36}$ W) Expand across $49,192$ real Gaia stars
Barrow (inward, control) B2 Genes Edit one DNA base and watch a town's oxygen supply change
B4 Atoms Place ions in a window lattice and change a city's energy use
B6 Particles Run a matter–antimatter reactor

You can talk to it ("Take me to the Moon and harness its power"). Grok picks the right tools, the physics engine computes the result inside SpacetimeDB, and Grok narrates the real numbers back to you.

Each civilization knows its limits. Ask a gene-editing B2 civilization for a Dyson sphere around Vega and Grok refuses: it explains why and tells you that K3 could do it. Faster-than-light travel is refused at every level. Grok Imagine renders the exact state you just computed. Friends can join the same universe, see each other's avatars, follow each other and get notified when someone builds something.

How I built it

Real data in. I pulled data from 23 public scientific sources into about 115,000 rows across 33 SpacetimeDB tables:

  • ESA Gaia DR3 (49,192 stars)
  • JPL Horizons and NASA SPICE (planets, moons, radii, orbits)
  • NASA Earthdata
  • NASA Exoplanet Archive
  • SDSS DR18
  • SIMBAD
  • Our World in Data (human energy use)
  • NIST (atomic data)
  • PDG (particle masses)
  • PubChem, RCSB PDB and ClinVar (biology)

Every value is tagged observed, derived, assumed or simulated, so you always know what's real.

Physics out. A deterministic TypeScript engine turns that data into results.

  • Kardashev level from power (Sagan's interpolation): $$K = \frac{\log_{10} P - 6}{10}$$
  • Power captured by a body or collector at distance $d$ from a star: $$P = \frac{L_\star}{4\pi d^2}\,\pi R^2\,f\,\eta$$
    • $d$ comes from Horizons and $R$ from SPICE.
    • $L_\star$ is Gaia's measured luminosity. For Vega that's about 49 $L_\odot$, which gives a Dyson sphere of about $9\times10^{26}$ W.
  • Radiator temperature comes from the Stefan–Boltzmann law, $P = \varepsilon\sigma A T^4$. Big structures have to dump their waste heat somewhere.
  • K1 grid: regional power balance with about 3.5% HVDC loss per 1,000 km.
  • K3 expansion: a k-d tree plus Dijkstra shortest paths over real star positions. The expansion front is never faster than light.
  • B2: genetic code → protein → oxygen-carrying capacity → how many people a town can support.
  • B4:
    • ion density from $n = x\rho N_A / M$;
    • light transmission from Beer–Lambert, $I = I_0 e^{-\alpha c \ell}$;
    • then lighting and cooling load → city energy.
  • B6: annihilation energy $E = 2(mc^2 + T)$ with PDG masses. Neutrinos carry away about half, and it honestly comes out net-negative, because you have to make the antimatter first.

SpacetimeDB is the entire backend. There's no other server.

  • Every action (click, chat or voice) is a single submit_command reducer. It validates identity, role, world revision and duplicate command IDs, then runs the physics engine inside the transaction, writes the results to tables and stores a receipt.
  • Row-level security scopes each world to its members. Clients subscribe and changes stream to every player instantly.
  • A scheduled reducer drives shared galactic time for K3. It auto-pauses idle worlds.
  • Grok Imagine jobs run through a lease queue in SpacetimeDB. A render is rejected if the world changed while it was being generated, so images always match the physics.
  • Multiplayer presence (poses, names, follow) is just more tables.

Grok.

  • Grok Voice uses the realtime API with a tool catalogue generated from the engine.
  • Grok chat handles the same tools for typed commands.
  • Grok Imagine renders cinematic views from prompts built from the computed state.

Frontend and deployment. The frontend is React and React Three Fiber, with a cinematic camera that zooms between scales. It's deployed on Vercel, whose serverless functions keep the xAI key off the client, with SpacetimeDB Maincloud as the database. It's fully in the cloud.

Challenges I ran into

  • Running real physics inside a database transaction.
    • Problem: the engine had to be deterministic and fast enough to run in a SpacetimeDB reducer.
    • Fix: one shared engine package used by both the browser and the module, memoized, with input hashing so stale results are detectable.
  • Row-level security and subscriptions.
    • Problem: joined RLS filters need indexed, public join tables.
    • Fix: I redesigned access around a dedicated membership ACL table.
  • Teaching the AI to say no.
    • Problem: early on, asking for a Dyson sphere while in B2 made the agent "helpfully" jump to K2.
    • Fix: a civilization gate on every tool. The agent now refuses, explains why and suggests the right civilization, but never switches for you.
  • Stale AI renders.
    • Problem: images could arrive after the world had changed.
    • Fix: job leases with input-hash checks.
  • Cloud costs.
    • Problem: idle galactic clocks kept ticking and burned database energy.
    • Fix: idle auto-pause.

Accomplishments I'm proud of

  • Every number on screen traces back to a real measurement and a real equation.
  • A 115k-row scientific baseline and a live multiplayer world, all in one SpacetimeDB database with no extra backend.
  • An AI agent that understands physical limits instead of just doing whatever you ask.
  • Tested end to end:
    • 24 engine tests;
    • 9 SpacetimeDB integration tests;
    • 13 multiplayer tests with real separate clients;
    • 21 voice-agent scenarios.

What I learned

  • How the Kardashev and Barrow scales map onto real physics: inverse-square flux, Stefan–Boltzmann heat limits, Beer–Lambert absorption and relativistic annihilation energy.
  • That a database built for game state can be an authoritative simulation server, with transactions, scheduling, RLS and live sync built in.
  • That an AI agent gets more trustworthy when it's grounded in tools and constraints instead of prompts alone.

What's next

  • More civilizations, including the remaining Barrow levels.
  • Exoplanet colonization using the NASA Exoplanet Archive.
  • Time-lapse replays of a civilization's history, straight from SpacetimeDB's command receipts.

Built With

Share this project:

Updates

Submission history