Processing Gravity


Turbo C++

We have all studied the classical equations of motion in high school. Towards the end of high school (after studying calculus and C programming), I wondered how one could represent differential vector formulae inside a computer. I was new to computer programming then and I didn't know a lot of things, but a year later, one of my professors (Prof. Atul Mody) and friend (Sundeep Tuteja - he was the one who taught me TC++ graphics!) sat along side me as I typed away the program. The whole thing two just under 2 hours - turns out that, simulating the motion of planets isn't Rocket Science haha!. I wrote the program in C++. I was using Turbo C++ 3.0 DOS Compiler.In retrospect, all that seems quite naïve, but it was one of the first 'useful' programs that I wrote. Now days you can do this and much much more with MATLAB & Octave (and even Perl/Python). Recently, I remembered the program yet again and wanted to run it. Of course this compiler didn't work on Windows 7.0 64-bit edition that I have on my PC. So I installed VirtualBox on my PC and then created a Virtual Machine and installed MSDOS 6.22 on it (HowTo1 | HowTo2). I copied the compiler's folder into MSDOS 6.22's virtual harddisk - to do this mounted it (the virtual harddisk - the .vdi file) in another virtual machine that I had created for Windows XP SP3 32-bit and then used folders shared over network between the Host OS (Windows 7) and the Guest OS (Windows XP running on a Virtual Machine). With that done, I booted up the MSDOS 6.22 and "C:\TC\BIN\TC.EXE" later, I had the IDE running. Opened "SATELL~1.CPP" and hit Ctrl+F9 and voila! (Of course it didn't work on the first go, the linker complained about not being able to find TCLASSL.LIB but that was easily fixed by unchecking "Container Class" and "Turbo Vision" in Options>Linker>Libraries).
SATELLITE.CPP is linked to here in case anyone wants to try it. What this program does is display a Planet (the big blue circle) at the center of the screen and its Satellite (the smaller red circle) orbitting around it. The program repeatedly calculates the force, acceleration, velocity and position vectors for the satellite (and only the satellite, I forgot about the planet itself) and refreshes the screen periodically. You could change the values of mass, initial position and initial velocity of the planet's satellite to see how it would affect the trajectory of the satellite.

Processing 2.2.1

In September 2010, I decided to spend some time and rewrite this program in Processing - one of the best new easy to use free development tool. In this program (or sketch as it is know) I have renamed the two bodies as "The Star" and "The Planet". This time the program calculations and updates the position of both the bodies (unlike the previous one in which only the position of the satellite was being calculated and updated continually).
Observations: Notice how the Star wobbles in the center as the planet rotates around it. With a little more refinement, the program could even simulate multiple bodies. Safety checks are needed to prevent the bodies from running away out of the screen when they move close to one another (r in the denominator becomes zero and the gravitational force hence the acceleration becomes infinite).

The sketch Gravity.pde is linked to here. One of the very cool features of Processing was that it allowed you to embed the sketch as a Java applet. But that feature no longer works - especially with Google Sites. Also many of the programming APIs for Processing changed since 2.2.1 release, so Gravity.pde no longer works on the latest version of Processing (4.0 as of January 2022).

p5.JS is a in-browser web version of latest version of Processing. So in January 2022, I decided to port the above code from Processing 2.2.1 to p5.js. While doing it, I discovered that there was a bug in my previous code - I was updating the velocity to the new value by adding to it the incremental change and then using it to calculate the incremental change in position. This is wrong - calculation of incremental change in position needs the current velocity, not the new one. I fixed that while porting the code to p5.js and also added a lot of documentation to the code as comments. The code is here.

With p5.js, you can again embed output in other webpages and so here it is:

Here is a screenshot, in case the embedding feature gets discontinued on google sites or on You can also download a local copy of the sketch and the code to run it in the browser from here.

Also checkout the collection of links I have gathered to accompany Feynman's Lectures here.

Page last updated: January 2022