Three body simulation in Processing

Ever since I took Mechanics I’ve been fascinated by solving for the equations of motion of different physical systems. Only recently have I developed the programming confidence to be able to actually solve these equations so I can see the time evolution of some of my favorite systems. One rather fascinating system is the ‘three body’ system. This system has three particles of mass m1, m2, and m3 that experience the gravitational force resulting from every other body’s gravitational field. In this blog post I go through the process of actually solving for the equations of motion of the two body system. It is rather easy to extend this derivation to the three body system. The three body system is an example of a ‘chaotic’ system — small changes in input parameters result in large changes in the time evolution of the system. One can mitigate the instability of the problem by assuming that the mass of one of the particles is much larger than that of the others. While my code will solve this problem regardless of masses, I’ve set it up such that the first mass is about 100 times larger than that of the second mass, which is in turn about 100 times larger than the third mass. This makes it easier to adjust parameters. I’ve added some sliders that will adjust the position and velocity of each of the masses in my simulation. Below is a GIF showing the functionality of my code. Sorry it’s so darn ugly.


Notice that the central mass moves in the positive y direction while the simulation runs. I don’t think that this is due to anything physical. In fact, I think that it has to do with the stability of my ODE solver. For those who are interested, I’m using an explicit method called Runge-Kutta. Runge-Kutta does a good job of dealing with unstable ODEs (like the one I’m solving here) but it can’t do as well as an implicit solver. Implicit solvers require solving a system of nonlinear equations at each time step, so I haven’t bothered to implement it yet.

Leave a Reply