

This is an online GPU N-body simulation: a gravitational simulation where tens or hundreds of thousands of bodies (stars, for example) attract each other, computed on your graphics card (GPU) with WebGPU, the new standard for running programs on the GPU from a web page.
In a system of n bodies, every body attracts every other body, so computing the forces exactly takes n·(n-1) calculations on every step: 900 million for 30,000 bodies. Instead of using an approximation like the Barnes-Hut algorithm or the Fast Multipole Method, this tool computes every single interaction, the exact direct sum, with thousands of threads of the GPU working at the same time.
A modern graphics card computes hundreds of billions of interactions per second: a galaxy of 100,000 stars takes about 16 milliseconds per step on a mid-range desktop GPU, fast enough for real time. That is about 4 times faster than the Fast Multipole Method on the CPU, and without its approximation errors. Click Compare with the CPU to measure it on your own computer.
Choose an initial configuration: a spiral galaxy orbiting a black hole, a collision of two galaxies, an unstable rotating disk, a cloud that collapses, merging star clusters, a ring around a black hole, or an expanding universe. Then watch gravity do the rest. The initial conditions are the same as in our Barnes-Hut and FMM simulations, so you can compare the three tools directly.
While watching the simulation, you can pan, zoom, change the animation speed and customize the visualization: colors by speed, trails, glow and more. Move the camera by clicking and dragging your mouse on the canvas, and zoom by scrolling the mouse wheel on the canvas.
You can also record and download videos of your simulations, in webm format, and download the current animation frame as a PNG image. Everything runs directly in your browser and on your graphics card: nothing is uploaded to a server. You are free to share the generated images and videos anywhere. Attribution is not required but appreciated.
Below you can find some examples of gravitational simulations computed on the GPU. Click on any example to apply the configuration and start the simulation.
| Field | Description |
|---|---|
| Resolution | The canvas width and height, in pixels. You can select an option from the list of common display resolutions, or use "custom" to choose any width and height. |
| Width | The width of the animation canvas, in pixels. |
| Height | The height of the animation canvas, in pixels. |
| Field | Description |
|---|---|
| Configuration | The initial arrangement of the bodies:
Choosing a configuration also loads its recommended radius, masses and velocity factor. |
| Number of bodies | The number of bodies (stars) in the simulation, including the central masses, up to 1,000,000. The work of each step grows with the square of the number of bodies: twice as many bodies take four times longer. A mid-range desktop graphics card runs about 100,000 bodies in real time. The integrated graphics of a laptop are usually several times slower, so start with the default 30,000 bodies, and lower the number if the animation is slow. A recorded video always keeps the configured framerate, however slow the animation is. |
| Radius | The radius of the galaxy, of the disk or of the cloud, in pixels. In the galaxy collision it is the radius of each galaxy, and in the star clusters it is the radius of the region where the clusters are placed. |
| Total mass of the bodies | The mass of all the bodies together, without the central masses. It is divided equally between the bodies, so changing the number of bodies does not change the overall motion: more bodies only make the image more detailed. |
| Central mass (black hole) | The mass of the body at the center of each galaxy, or at the center of the disk or the ring. Use 0 for no central mass. The central masses are drawn as white dots. |
| Number of clusters | The number of star clusters. Only used by the "star clusters" configuration. |
| Velocity factor | Multiplies the initial velocities. In the disk configurations each body starts with the speed of a circular orbit around the mass inside its radius, multiplied by this factor: 1 means circular orbits, a smaller value makes the disk contract, and a larger value makes it expand. In the star clusters, it multiplies the random velocities of the bodies inside each cluster. In the expanding universe, it is the initial expansion speed as a fraction of the escape velocity. In the cold collapse it is 0 by default, and a value greater than 0 makes the cloud rotate. |
| Random seed | The positions of the bodies are random. The same seed always generates the same initial positions, so you can reproduce a simulation. Click the dice button to try a new random seed. |
| Field | Description |
|---|---|
| Gravitational constant | The strength of gravity (the constant G of Newton's law of gravitation). Greater values make everything move faster. It multiplies all the masses, so doubling it has the same effect as doubling every mass. |
| Softening | A small distance, in pixels, added to the distance between the bodies when computing the force: the force is proportional to Without softening, two bodies passing very close to each other feel an almost infinite force and are thrown away at absurd speeds. Each body then behaves more like a small cloud of stars than like a point, which is what the bodies of a galaxy simulation really represent. |
| Time step | The amount of simulated time advanced on every physics step. Smaller values make the simulation more accurate, but more steps are needed to advance the same amount of time. If bodies are thrown away from the black holes, reduce the time step or increase the softening. |
| Physics steps per frame | The number of physics steps calculated on every animation frame. The time simulated on each frame is the time step multiplied by this value and by the animation speed. With many bodies, 1 step per frame keeps the animation smoother. |
| Button or value | Description |
|---|---|
| Compare with the CPU | Pauses the simulation and computes the forces of the current bodies several times on the GPU, to measure the time of one computation. The bodies are then copied to the CPU, where the same forces are computed with three methods on a single thread: the exact direct sum, the Fast Multipole Method (theta 0.7, order 4) and the Barnes-Hut algorithm (theta 0.7). The direct sum on the CPU is only computed for a sample of 400 bodies, and its time is multiplied to estimate the time of every body: with 100,000 bodies the full direct sum would take many seconds. |
| Time per step | The time to compute the forces of every body once, with each method. For the FMM and the Barnes-Hut algorithm, it includes building the tree. |
| Average error | The difference between the computed force and the exact force on each body of the sample, divided by the exact force. The GPU computes the exact direct sum too, but with 32-bit numbers, so its error is only the rounding error, about one part in a million. The FMM and the Barnes-Hut algorithm approximate the pull of distant groups of bodies, which gives larger errors. |
| Field | Description |
|---|---|
| Color | How the bodies are colored:
|
| Body size | The diameter of each body, in pixels. A size of 1 draws each body as a single point, spread over the nearest pixels so it moves smoothly. |
| Brightness | The brightness of each body. The light of the bodies adds up, like in a long exposure photo of the sky, so the dense regions become bright and the sparse regions stay faint. Increase it when there are few bodies, and decrease it when there are many. |
| Show black holes | If enabled, the central masses are drawn as larger white dots. |
| Trails | How much of the previous frame remains visible, from 0 (no trails) to 0.99 (very long trails). The trails are drawn on the canvas, so moving or zooming the camera also leaves a trail. |
| Glow | If enabled, a blurred copy of the bodies is added over the image, which gives the galaxies a soft glow. |
| Glow radius | The radius of the blur of the glow, in pixels. |
| Glow intensity | The strength of the glow, from 0 to 2. |
| Field | Description |
|---|---|
| Origin | The coordinates origin (0,0), which is where the center of the galaxy is placed. It can be "top left" or "center". |
| Offset x | The "x" coordinate offset, in pixels. It determines the horizontal position of the "camera". Adjusting this offset lets you shift the view or "move" the camera. |
| Offset y | The "y" coordinate offset, in pixels. It determines the vertical position of the "camera". |
| Field | Description |
|---|---|
| Zoom | Changing this parameter allows you to "zoom in" or "zoom out". You can also zoom by scrolling the mouse wheel on the canvas. The bodies keep their size in pixels, only the distances between them change. |
| Field | Description |
|---|---|
| Transparent background | If this option is checked, the animation has a transparent background. |
| Background color | The background color, in hexadecimal value. For example, use #000000 for a black background. |
| Field | Description |
|---|---|
| Animation speed | The speed of the animation. Values greater than 1 mean the animation plays in "fast motion", and values smaller than 1 mean it plays in "slow motion". It changes the number of physics steps per frame, so the accuracy of the simulation stays the same. |
| Button or value | Description |
|---|---|
| Start | Start the simulation. |
| Restart | Restart the simulation from the initial conditions. |
| Pause | Pause the simulation. |
| Resume | Resume the simulation. |
| Stop | Stop the simulation. |
| Graphics card | The graphics card that runs the simulation, as reported by the browser. Some browsers only give the vendor, or nothing, for privacy. |
| Interactions per step | The number of forces computed on every step: each of the n bodies feels the pull of the other n-1 bodies. |
| GPU time per step | The time the graphics card took to move the bodies and compute their forces, on each step. It is measured on the GPU itself, and only shown by the browsers that support it. |
| Interactions per second | How many forces between two bodies the GPU computes per second. It is calculated from the GPU time per step when it is available, otherwise from the frames per second, which can be limited by the refresh rate of your screen. |
| Frames per second | The number of animation frames drawn per second. |
| Download current animation frame | Download the current animation frame (in PNG format). |
| Reset all the options | Restore every option to its default value and restart the simulation. |
Instead of only watching the simulations online, you can also record and download videos of your simulations. The simulation videos are generated using the webm extension.
| Field | Description |
|---|---|
| Framerate | The amount of frames per second that you want the video to have. |
| Automatically stop after... | If enabled, the video recording stops automatically after the configured time (in seconds) or after the configured amount of frames. |
Newton's law of gravitation says that every body attracts every other body with a force proportional to the product of their masses and inversely proportional to the square of the distance between them. To move n bodies one step forward, a simulation needs the total force on each body, and the direct way to get it adds up the pull of every other body: n·(n-1) calculations. Doubling the number of bodies makes each step four times slower.
A CPU has a few cores, and computes these forces mostly one after the other. A GPU has thousands of small cores, designed to draw millions of pixels at the same time, and with WebGPU a web page can use them for any computation. On every step, the simulation:
This is the same direct sum that a CPU would compute, the simplest method there is. Algorithms like the Barnes-Hut algorithm and the Fast Multipole Method do much less work, by approximating the pull of distant groups of bodies, but they organize the bodies in a tree, and walking a tree is irregular work that a GPU does poorly: each thread goes its own way, while a GPU is fastest when thousands of threads do exactly the same thing. On a desktop graphics card, up to a few hundred thousand bodies, the brute force of the GPU wins, and with no approximation errors.
The GPU computes with 32-bit floating point numbers, about 7 significant digits, because WebGPU has no 64-bit numbers. That is plenty for a galaxy: the forces are exact to about one part in a million, much better than any approximation algorithm, and the integration makes much larger errors anyway.
The bodies move on a plane: real galaxies are three dimensional, but their disks are thin, so a 2D simulation already reproduces many of their features: spiral arms, bars, tidal tails and mergers.
WebGPU is a standard that lets web pages use the graphics card, both to draw images and to run general computations, called compute shaders. It replaces WebGL, which was designed only for drawing, and it is supported by recent versions of Chrome, Edge, Safari and Firefox.
What if my browser does not support WebGPU?The page shows a message instead of the simulation. Updating your browser usually solves it. Some systems, older graphics cards and some Linux configurations still have WebGPU disabled. In that case, try our Fast Multipole Method simulation, which simulates the same galaxies on the CPU, in any browser.
Is the GPU faster than the Fast Multipole Method?On a desktop graphics card, yes, up to a few hundred thousand bodies, even though it does much more work. On a mid-range desktop graphics card (an RTX 5060 Ti in our tests), the GPU computes the forces of 30,000 bodies in about 1.6 ms, and of 100,000 bodies in about 16 ms, while the FMM takes about 20 ms and 75 ms on one core of a fast CPU: 12 and 4 times longer. With 250,000 bodies the GPU is still twice as fast. The FMM costs about n and the direct sum n², so with enough bodies the FMM always wins in the end, and on a slower graphics card it wins sooner. Click "Compare with the CPU" to measure it on your own computer.
It is possible, and the largest astrophysics simulations do it, but it is much harder. The FMM builds a new tree on every step, and walks it in an irregular way, which does not suit the thousands of threads of a GPU, and WebGPU has no 64-bit numbers and no way to add floating point numbers from several threads at the same time. The direct sum is perfectly regular, which is exactly what a GPU does best.
How many bodies can I simulate?Up to 1,000,000, but each step costs the square of the number of bodies. A mid-range desktop GPU runs about 100,000 bodies in real time, computes 250,000 bodies at about 10 steps per second, and 1,000,000 bodies at less than one step per second. The integrated graphics of a laptop are usually several times slower. If the animation becomes slow, record a video instead, because the video always keeps the configured framerate.
What is the N-body problem?The N-body problem is the problem of predicting the motion of a group of bodies that interact through gravity. With two bodies the orbits are simple ellipses, but with three or more bodies there is no general formula, and the motion must be computed numerically, step by step. See also the Three-Body Problem Simulation.
Which units are used?The distances are in pixels and the time is in arbitrary simulation units. The masses and the gravitational constant have no units either: only their product matters for the motion.
Are my simulations uploaded to a server?No. Everything runs in your browser and on your graphics card. Nothing is uploaded, and the images and videos you download are generated on your own computer.
Can I use the generated images and videos?Yes. You are free to use and share the generated images and videos on YouTube, TikTok, or any other social media or website. Attribution is not required but appreciated.



