Sunday, November 1, 2015

Three body problem in Processing

I've been messing around with doing some very basic physics simulations in Processing. Last week I did the two body problem, which describes the motion of two particles under mutual gravitation. This week I extended the code a little to do the three body problem -- three particles under mutual gravitation. Coding this up was pretty easy, but finding initial conditions that resulted in something viewable was quite hard! I want to build in some interactivity such that a user can dynamically adjust initial conditions, but I've been too lazy to get around to doing that. Maybe next time I'll have done that! For now, however, you're stuck with a .gif I made:



From this little animation, you can see how chaotic this system is! You can set up a situation in which you get stable orbits. In the simulation below I set the mass of the central particle to be about 200 times that of the outer one. The one in the middle orbit is about 100 times smaller than that of the outer one.



Here you can see that the central mass is moving a little. This is because it is orbiting around the center of mass of the outer/central mass sub system. For those who are interested, I changed from float to double arrays in this code and my previous two body code. The stability of the solution increased in the long term as a result. As always, using an implicit ODE method would probably be the best way to ensure long term stability of my simulation.

UPDATE: As promised, I added some sliders to my code so that users can adjust the initial parameters of the system before letting it loose. Check out the gif below.














3 comments: