Introduction: Empty Space and Unstable Suns

That gravity should be innate, inherent, and essential to matter, so that one body may act upon another at a distance, through a vacuum, without the mediation of anything else, by and through which their action and force may be conveyed from one to another, is to me so great an absurdity, that I believe no man who has in philosophical matters a competent faculty of thinking, can ever fall into it.

Isaac Newton (in a letter to Richard Bentley, 1693)

Although Isaac Newton first discovered the laws of gravity in 1687, he remained puzzled. How can the force of gravity exert itself between two distant bodies without the need for anything in between? It would take over 200 years until Albert Einstein’s theory of general relativity to resolve the paradox by explaining gravity not as a force but as a property of space-time.

Nevertheless, we find it very useful to think of gravity as a force; after all, we experience the outcome of Earth’s pull on ourselves every day. Furthermore, gravity is such a constant force that we possess precise formulas that predict how two heavenly bodies will move because of the effects of gravity.

Yet as soon as we introduce a third heavenly body into a gravitational system, no general solution exists for predicting the bodies’ movements, and chaos sometimes reigns. Such is the lament of the residents of Trisolaris, a fictional planet in Cixin Liu’s The Three-Body Problem that finds itself in the midst of three suns locked in an unpredictable dance.

Despite making the solution of the three-body problem the central aim of their civilization, the residents of Trisolaris never solve the three-body problem and must endure the repeated arrival of “chaotic eras” in which the suns break stable orbits, making their planet uninhabitable and forcing them into hibernation.

Fortunately, computer scientists do not wake up in the morning desperate for perfect precision or musing philosophically like celibate physicists. They often just want to build something cool that works. To this point, we will build on the notion that we introduced in Chapter 2 that when computing something exactly is difficult, simulating can sometimes be quite a bit easier. The productive laziness of programmer nerds leads us to the following central computational problem of this chapter.

Gravity Simulator Problem

Input: A configuration initialUniverse of a “universe” of bodies, an integer numGens, and a floating-point number time representing a time interval in seconds.

Output: All numGens + 1 configurations of the universe, starting with initialUniverse, where each configuration is approximated over an interval time.

We will soon solve this problem to build beautiful simulations of gravitational systems, whether they are the moons of Jupiter or a simulation of three chaotic stars, as shown in the animations below.

Four moons of Jupiter in simulated orbits.
A simulation of three heavenly bodies, whose movements are solely due to the force of gravity acting between them.

We have just one problem: what type should a heavenly body be? Until now, we’ve only worked with basic types like numbers, strings, lists, and dictionaries. Yet a star or planet isn’t any of these; rather, it’s a distinct entity with properties of its own. In this chapter, we will extend our knowledge of programming to work with objects that are more abstract than the types that we have encountered thus far.

Scroll to Top