AZ TOOLS

Three Body Chaos Lab

The three-body problem and chaotic orbits, simulated in Maya: three masses, Newton's law of gravity and nothing drawn by hand.

The figure-eight solution: three equal masses chasing each other along one curve. One period with Velocity Verlet at dt = 0.005; the total energy at the end differs from the start by 7×10⁻⁸ %. All the orbits on this page are computed with the tool's own solver.

Why I built it

After watching the Netflix series 3 Body Problem, reading about the problem was not enough: I wanted my own interactive simulation, to see how such systems behave right inside Maya.

That is how Three Body Chaos Lab started: a Python tool for Maya that computes the motion of three bodies attracting each other and turns the result into ordinary Maya animation and trajectory geometry.

The one rule of the project: no trajectory is drawn in advance. Where every object is at every step comes straight out of the numerical solution of the equations of motion.

The problem

For each body we know three things: its mass \(m\), its starting position \(\mathbf r\) and its starting velocity \(\mathbf v\). We want the position of all three bodies over time,

$$\mathbf r_1(t),\quad \mathbf r_2(t),\quad \mathbf r_3(t)$$

given that every body is pulled by the other two.

Two bodies have an analytic solution: conic orbits, ellipses, parabolas and hyperbolas. The general three-body problem has no such universal, simple solution, so the motion has to be computed numerically.

The physical model

Classical Newtonian gravity. The force between two bodies is

$$F = G\,\frac{m_1 m_2}{r^2}$$

For a simulation it is more convenient to compute the acceleration of body \(i\) directly:

$$\mathbf a_i = G \sum_{j \neq i} m_j\, \frac{\mathbf r_j - \mathbf r_i}{\left|\mathbf r_j - \mathbf r_i\right|^3}$$

So at every step each of the three bodies feels the other two.

The positions change all the time, so after every step all the accelerations are computed again from scratch.

Numerical integration

The state is advanced with Velocity Verlet, a symplectic integrator that suits mechanical and orbital problems well. The position is updated as

$$\mathbf r_{t+\Delta t} = \mathbf r_t + \mathbf v_t\,\Delta t + \tfrac12\,\mathbf a_t\,\Delta t^2$$

then the gravitational acceleration \(\mathbf a_{t+\Delta t}\) is computed again at the new positions, and the velocity is updated with the average of the two:

$$\mathbf v_{t+\Delta t} = \mathbf v_t + \frac{\mathbf a_t + \mathbf a_{t+\Delta t}}{2}\,\Delta t$$

One step, in order:

Position(t)→Gravity(t)→Acceleration(t)→ Position(t + dt)→Gravity(t + dt)→Acceleration(t + dt)→ Velocity(t + dt)

And then it all repeats, thousands of times.

Why not Euler

The simplest possible step would be explicit Euler:

$$\mathbf v_{new} = \mathbf v + \mathbf a\,\Delta t \qquad \mathbf r_{new} = \mathbf r + \mathbf v\,\Delta t$$

Plain Euler accumulates energy error quickly. In an orbital system that makes a stable orbit grow or fall apart for purely numerical reasons. Velocity Verlet preserves the structure of a mechanical system much better over long stretches of time.

The same forces, the same dt = 0.005, three periods of the figure eight. Euler keeps gaining energy and the eight unravels; Velocity Verlet stays on it. The Euler side is an illustration: it uses the tool's force function with an Euler step, which the tool itself does not offer.

The time step

One of the most important parameters is dt. With dt = 0.01 the state of the system is recomputed every 0.01 units of simulation time. The smaller dt, the more accurate the result:

0.01→0.005→0.001→0.0005

and the more work there is, in proportion. The long periodic orbits use small steps over tens of thousands of iterations; the longest preset, Dragonfly, runs at dt = 0.00001 for 8.4 million steps.

Sampling for Maya, independent of the physics

A small dt brings a different problem. Sixty thousand physics steps could mean tens of thousands of curve CVs and animation keys in Maya. So the physics and the output are separated. Record Every N Physics Steps lets the physics run on every step, 1, 2, 3, 4, 5 …, while Maya only receives every Nth one, 10, 20, 30, 40 …

Physics dt   = 0.0005
Record Every = 10        # same physical accuracy, about ten times less Maya geometry

Softening

When two point masses come very close, \(r \to 0\), the gravitational acceleration goes to infinity. In a discrete simulation that can turn into a huge velocity jump in a single step. A small gravitational softening keeps it finite:

$$r^2 \;\to\; r^2 + \epsilon^2 \qquad\Rightarrow\qquad \left(r^2 + \epsilon^2\right)^{3/2} \text{ in the denominator}$$

Softening is not part of the ideal three-body problem: it is numerical stabilisation. For the periodic orbits it is set very small, 10⁻⁶.

Keeping an eye on energy

To know how far the numerical result is from the ideal mechanical system, the tool also computes the total energy. Kinetic and gravitational potential energy:

$$E_k = \sum_i \tfrac12\, m_i v_i^2 \qquad E_p = -G \sum_{i

After a run, Energy Start and Energy End are compared:

$$\text{Energy drift} = \frac{E_{end} - E_{start}}{\left|E_{start}\right|} \times 100\%$$

That is what separates genuinely complex behaviour of the system from too much numerical error. For the presets, run with the tool's solver:

OrbitdtPhysics stepsEnergy drift
Balanced triangle0.0024,1354.4×10⁻¹³ %
Figure eight0.0051,2656.7×10⁻⁸ %
Butterfly I0.000162,3562.1×10⁻⁹ %
Moth I0.000529,7882.2×10⁻⁸ %
Bumblebee0.000023,176,7251.0×10⁻¹¹ %
Dragonfly II.17.B0.000018,368,3345.9×10⁻⁹ %

Chaos and sensitivity to initial conditions

A separate part of the project runs several almost identical systems side by side:

SYS00   Body C  Position X =  0
SYS01   Body C  Position X =  0.000001
SYS02   Body C  Position X = -0.000001   # then ±2ε, ±3ε …

All of them are computed with one algorithm, the same dt, the same number of steps and the same physical parameters. The only difference is a microscopic change in the initial state. At first their motion is practically identical. After a while the differences start to grow, and that is how you get to see sensitivity to initial conditions.

The Balanced + epsilon preset: seven copies of the equal-mass triangle, body C moved along X by ±ε, ±2ε, ±3ε with ε = 10⁻⁶, Simulation Steps raised to 20,000. For three turns the copies are indistinguishable; then the equilibrium breaks, and every copy breaks it differently. The inset is each copy's RMS divergence from SYS00.

Measuring the divergence

To put a number on it the tool uses RMS divergence. For each body, the distance to the same body of the reference system:

$$D_A = \left|\mathbf r_{A,i} - \mathbf r_{A,0}\right| \qquad D_B = \left|\mathbf r_{B,i} - \mathbf r_{B,0}\right| \qquad D_C = \left|\mathbf r_{C,i} - \mathbf r_{C,0}\right|$$

and then

$$D_{RMS} = \sqrt{\frac{D_A^2 + D_B^2 + D_C^2}{3}}$$

One number that describes how far the whole system is from the reference. It is diagnostics only and never feeds back into the simulation.

Periodic solutions

Besides arbitrary chaotic starts, the tool carries several well-known periodic configurations.

Lagrange: the balanced triangle

Three equal bodies sit at the corners of an equilateral triangle. With exactly the right tangential velocity the whole system turns around the common centre of mass and keeps its shape. For a triangle of side \(a\):

$$\omega^2 = \frac{3Gm}{a^3} \qquad v = \omega R$$

where \(R\) is a body's distance from the centre of mass. A dynamic equilibrium, and a fragile one: with equal masses it is unstable, which is exactly what the chaos experiment above relies on.

One full turn of the balanced triangle. The triangle never changes shape; energy drift 4.4×10⁻¹³ %.

Figure eight

Three equal bodies move along one shared figure-eight path. Their starting positions and velocities are chosen so that after one full period the system returns to where it started. It is one of the best-known modern periodic solutions of the three-body problem (Moore; Chenciner and Montgomery), and the animation at the top of this page.

More complex orbits

Later the tool got more intricate periodic solutions: Butterfly I, Moth I, Bumblebee and Dragonfly. The principle is no different from the figure eight, and the simulation algorithm stays exactly the same. Only the initial positions and velocities change. They all start from one symmetric convention:

$$\mathbf r_1 = (-1, 0, 0),\;\; \mathbf r_2 = (1, 0, 0),\;\; \mathbf r_3 = (0, 0, 0) \qquad \mathbf v_1 = \mathbf v_2 = (v_x, v_y, 0),\;\; \mathbf v_3 = -2\,\mathbf v_1$$

So Butterfly is not a hand-drawn path. The script receives a set of initial conditions, runs an ordinary Newtonian simulation, and the shape of the orbit appears on its own. That is probably the most interesting part of the experiment.

Butterfly I · v = (0.30689, 0.12551) · T = 6.24 · 62,356 steps.
Moth I · v = (0.46444, 0.39606) · T = 14.89 · 29,788 steps.
Bumblebee · v = (0.18428, 0.58719) · T = 63.53 · 3.2 million steps.
Dragonfly II.17.B · v = (0.05037, 0.57034) · T = 83.68 · 8.4 million steps.

Butterfly I, Moth I and Bumblebee are from the periodic orbits published by Šuvakov and Dmitrašinović (2013); Dragonfly uses re-converged centre-of-mass initial conditions.

The Maya tool

In short, the pipeline looks like this:

The current version is written in plain Maya Python, and the solver itself needs no external Python libraries. Its main parts:

Body
Mass, position, velocity and the position history.
Physics
calculate_accelerations(), simulate(), total_energy().
Chaos
Clone the system, perturb the initial state, measure the RMS divergence.
Maya output
Trajectory curves, animated spheres, materials, keyframe baking.
The AZ Three Body Chaos Lab window: the Bodies tab with presets and the three bodies, the Simulation tab with physics and the chaos experiment, the Output tab, and the diagnostics after a run.
The window: presets and the three bodies, physics and the chaos experiment, output and animation, and the diagnostics after a run, with the divergence and the energy drift.
Butterfly I in the Maya viewport: one trail curve per body, and spheres with baked keys travelling along them.
The figure eight with reference ghost markers along the trail.
Balanced + epsilon in Maya: the reference in full colour, the six perturbed copies in muted shades of the same hues.

Visualisation

To keep crossing trajectories readable, each body owns a colour:

The colour is shared by the body and its trail. The reference system gets the brightest version; systems with perturbed initial conditions get muted versions of the same hues. Even with many crossings you can tell which body drew which piece of a trajectory.

What Maya does, and what it does not

Maya is not the physics solver here. It provides the UI, the visualisation, storage of the result, the curves, the animation baking, the camera and the render. The physics itself is computed by Python code.

Python physics→Position history→Maya→Curves · Keys · Render

Limits of the model

This is the classical, simplified three-body problem. The bodies are point masses. Not included:

In other words, classical Newtonian N-body mechanics. For showing chaos and periodic orbits that is more than enough.

What came out of it

A fairly small Python solver turned into a little experimental lab inside Maya. You can change masses, starting positions and velocities; run several systems at once with microscopic perturbations and watch them separate; check energy conservation; explore the known periodic orbits; and keep everything as ordinary Maya animation.

The most interesting thing is that complicated-looking motion does not need complicated rules. All there is:

3 bodies+3 masses+3 positions+3 velocities+Newton's law

Yet with the right initial conditions that gives stable triangles, figure eights, Butterfly, Moth, Dragonfly, and chaotic trajectories that look as if somebody designed them on purpose. In fact they are just the result of one calculation repeated over and over:

Gravity→Acceleration→Velocity→Position→Gravity→ …

And that was the real goal of the experiment: to see how complex a motion you can get from the simplest possible set of physical rules.

Try it

AZ Three Body Chaos Lab is free for every AZ Tools account. It lives in the Lab tab of the AZ Tools Manager (0.3.25 or newer) and in the Lab section of the AZ Tools menu in Maya. Pick a preset on the Bodies tab, press RUN VISUAL CHAOS, then Frame Results.

Get AZ Tools Manager