top of page

Three‑Body Gravitational Dance: A Visual Exploration in Python

  • Writer: Chaitanya Singh
    Chaitanya Singh
  • Jul 25
  • 2 min read

In this demonstration you’ll see three masses interact under Newtonian gravity in real time. By tweaking initial positions, velocities and masses you can explore everything from elegant figure‑eight orbits to chaotic trajectories that never repeat.

  1. Simulation Setup

    • We define three bodies, each with its own mass, position and velocity.

    • Gravity strength G is adjustable (for example between 0.5 and 5.0) to explore tight or loose binds.

    • A small softening term prevents infinite forces when two bodies get extremely close.

    • A time step dt of 0.005 ensures numerical stability even during close encounters.

  2. Leapfrog Integration for Stability

    • First we compute the total gravitational force on each body by summing pairwise attractions to the other two.

    • Bodies’ velocities are updated by half a time step under those accelerations.

    • Positions are then updated by a full time step using the new velocities.

    • Finally velocities receive another half‑step update.

    • This symplectic scheme preserves energy far better than basic Euler methods.

  3. Generating Initial Configurations

    • Predefined setups include the famous figure‑eight orbit and the Lagrange equilateral triangle solution.

    • A “binary plus third” system places two heavy masses in circular orbit with a lighter body tracing around them.

    • A fully random configuration picks three positions at random within a radius, assigns random masses, then computes approximate circular velocities around their common center of mass plus a small random perturbation.

    • Total momentum is then shifted to zero so the system stays centered.

  4. Recording Trajectories

    • We store each body’s position at every time step (up to 4 000 frames) so we can draw trailing paths behind them.

    • Trail length can be set (for example 3 000 points) so you see the recent history without cluttering the view.

  5. Real‑Time Animation

    • A black background makes the colored paths stand out clearly.

    • Each body is drawn as a filled circle whose size scales with its mass.

    • Trails show the path taken over the last few thousand steps.

    • A legend labels each body by name and mass.

    • A live energy readout combines kinetic and potential energy at each frame, letting you watch how well the integrator conserves total energy.

  6. What You’ll Observe

    • In stable configurations like the Lagrange triangle, bodies trace perfect triangles around their center of mass.

    • The figure‑eight solution reveals a single elegant curve that all three bodies follow in lockstep.

    • Random setups often produce chaotic paths where slight changes in initial conditions lead to wildly different futures.

    • Energy remains nearly constant throughout, confirming the accuracy of the leapfrog method.

  7. Playing with Parameters

    • Increase G to see faster collisions and tighter orbits.

    • Vary mass ratios to watch lighter bodies sling‑shot around heavier ones.

    • Change dt to test the trade‑off between speed and accuracy.

    • Extend the simulation length or adjust trail_length to capture longer‑term dynamics.

  8. Next Steps

    • Add more bodies to explore N‑body chaos.

    • Implement adaptive time stepping so dt shrinks during close encounters.

    • Introduce relativistic corrections for extreme gravitational fields.

    • Port the force calculations to GPU for real‑time performance with hundreds of bodies.

This project offers a hands‑on window into the rich, often surprising world of gravitational dynamics. Edit initial conditions, tweak parameters, and see how three masses can choreograph dances that range from perfect geometric beauty to wild unpredictability. Enjoy the exploration!

Comments


LinkedIn

  • LinkedIn

© 2025 by Chaitanya Singh

bottom of page