SkillAgentSearch skills...

ThreeBodyBot

Poorly written code that generates moderately exciting plots of a very specific physics phenomenon that enthralls dozens of us around the globe.

Install / Use

/learn @kirklong/ThreeBodyBot
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Random Three-Body Simulation Generator (with Twitter/Tumblr/YouTube/Mastodon bot)

<!-- <img align="left" src="figure8.gif" width="380" height="380"> -->

Figure 8 solution

A cool stable solution to the three-body problem created by the code, based off of Chenciner and Montgomery (2000). See all the animations on the bot's ~~Twitter~~, Tumblr, YouTube, or Mastodon!

What people are saying about @ThreeBodyBot:

"I'm Jessie Christiansen and I stan the Random Three Body Problem twitter account." – Dr. Jessie Christiansen

"Mesmerizing bot" – Emily Lakdawalla

"is this trisolaran propaganda" – adam from Twitter

"shit I should really improve this code now that people other than me might look at it" – @ThreeBodyBot's inept parent

<br/>

The three-body simulation generator

This is a fun little pet project I put together in Julia that renders a random gravitational three-body simulation in two dimensions. It's a simple program that uses an explicit implementation of fourth order Runge-Kutta to simulate the system over a specified timescale. In addition to stopping after a specified amount of time, the simulation will quit if there is a collision between bodies or one or more bodies is ejected from the system. It then makes a pretty animation (complete with randomly generated fake background stars!) and saves it locally. This thing is now pretty much finalized, or at least at v1.0. As of January 2021 the bot has also started tracking its stats, outputting the results of each night's simulation into a text file (3BodyStats.txt) that's read in and analyzed with the notebook 3BodyAnalysis.ipynb, which makes a pretty array of plots showcasing where the bot has been. The longest "solution" the bot has ever found was a system that lasted a little over 14,000 years, and you can find that immortalized here. The tweet thread in the bot's bio contains a record of all of the longest simulations ands is updated each time it beats its own record if you want to see previous record holders!

A lazy n-body generator...

There are several scripts in the nbody folder worth looking at if you are looking to create even more gravitational chaos. These will start occasionally popping up on the bot's accounts, and there are new instructions below that should get you started if you want to try to make one of these yourself! The camera implementation with these is still a little buggy though...so I've currently disabled them until I get around to fixing that. If you have great ideas on what should be done to improve it please do so and I will happily credit and merge your changes. I think I can apply the improved camera system from the three-body versions here, but I need to actually sit down and spend a day doing it...

The math / procedural details

The simulations use Newtonian gravity only to simulate the system, i.e. each body's motion is governed simply by:

$$\mathbf{a_i} = \ddot{\mathbf{r_i}} = \sum_{j\neq i} -GM_{j}\frac{\mathbf{r_i - r_j}}{r_{ij}^3}$$

Where the body at index $i$ is the body being moved and $j \neq i$ the bodies that $i$ is attracted towards, with $i$ running from 1 to the total number of bodies (i.e. in the three-body case $i$ runs from 1 to 3). $G$ is Newton's gravitational constant, $M$ is the mass, and $\mathbf{r}$ the position vector (with $r_{ij}$ corresponding to the distance between two bodies $i$ and $j$). This second-order differential equation is split into two coupled first-order differential equations ($\dot{\mathbf{r}} = \mathbf{v}$ and $\dot{\mathbf{v}} = \mathbf{a}$) and integrated with an adaptive time stepping 4th order Runge-Kutta procedure ensuring that the total error of each simulation is <0.001% (measured from change in total energy of the system). The equations are integrated in 2D cartesian space (2D looks cleaner in visualizations and makes collisions more frequent, which is fun), although simple 3D versions of the code are hosted here as well if you're interested. For a more in-depth walk-through / analysis see the materials in the NumericsTutorial folder, which includes a nice tutorial on how to build your own n-body simulator from scratch!

The /Tumblr/YouTube/Mastodon bot

For Twitter: This is my first experience with JavaScript and the Twitter API, so it's mostly cobbled together code from the Twit documentation, this auto-tweeting image bot code, and this video tweeting example from Loren Stewart.

Update: Twitter finally disabled my free access to the v1.1 API (4 months after they were supposed to), so the bot now uses the Python package tweepy, but I have kept the examples linked above as they were still immensely useful for my learning.

To tweet from a script you need a developer account with Twitter so that you can fill in API keys (see configSample.js). The script the bot originally used to upload to Twitter is at server.js, but unfortunately the Twit package only supports (at this writing) the v1.1 API which Twitter recently locked behind a paywall. The bot now tweets using the Python script tweepy_bot.py as tweepy supports the v2 API, but I've kept both versions here on GitHub for those who might be curious.

Second update: Sadly, the bot is really dead now...on Twitter anyways (thanks Elon...) but it lives on in other places! Read more below:

For Tumblr: I used the great pytumblr module (and accompanying documentation there) to upload the simulations to a tumblr blog. You need to register your application to get authentication keys to be able to post but the process is pretty easy with Tubmlr! The script that does the uploading is bot.py.

For YouTube: This was the trickiest one... YouTube has a great and easy API to use like Tumblr, but they don't let you publish the videos you upload via their API to the public unless you "verify" your app, which requires a lot of red tape. Part of that verification process is showing how your app works and that it will be used by > 100 people...and on these grounds they denied verifying my app, so I had to get a little crafty. Ended up using this wonderful Opplast repo and instructions therein to accomplish this, but the way this works is significantly less stable over time than if YouTube would just let me use their API to upload, so if you're reading this and you work at YouTube (or know someone who does) and want to help me verify my app please reach out! The script that does the uploading to YouTube is opplastUpload.py and you can find the bot's YouTube channel here.

Update: They figured out this work-around and it stopped working and I'm too lazy to find another one...so for now I manually upload only the extended cuts to the bot's YouTube page.

For Mastodon: Like Tumblr, Mastodon has a nice Python API wrapper (Mastodon.py) that I use to upload the videos to the bot's Mastodon account. Again you need to register your application to get authentication keys but like Tubmlr it's pretty painless! With Mastodon you also have to make sure the server allows bots, which is why I originally chose to host the bot on bots.inspace. This server has since shut down but the bot has found a new home on Mathstodon. The script that takes care of uploading the videos to Mastodon is MastodonUpload.py.

Want to generate your own animations?

Overwhelmed by lots of instructions and want me to just do it for you? DM the bot a donation receipt showing you supported an org fighting for civil rights (ie places like the Equal Justice Initiative, Human Rights Campaign, ACLU, etc.) and I will do the heavy lifting for you! I will happily make you a classic three-body version, a 3D three-body version, or you can request one of my new fancy new n-body simulations – you can even pick colors and name the stars! Here's an example of a fun custom thing I recently put together in honor of the new Dune movie:

Dune.gif

Now back to the details...

If you're on Windows, there's now a 15 minute tutorial video that walks you through the entire process, from installing Julia to making your first animation! If there's interest I will also make one demonstrating the process on Linux, but I'm assuming most people who are running Linux won't need/want a tutorial. Unfortunately I don't have access to a Mac, so can't make one for macOS. Hopefully between the Windows video and the instructions below you can sort it out, sorry!

Don't want to download anything / tinker with your own computer? One of my students Navan Chauhan recently deploy

View on GitHub
GitHub Stars300
CategoryProduct
Updated9d ago
Forks17

Languages

Jupyter Notebook

Security Score

100/100

Audited on Mar 21, 2026

No findings