Some Notes‎ > ‎

Processing Gravity



    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 attached below 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.

    

  
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). The sketch (Gravity.pde) is attached below. The Java applet you see on the top right is generated by Processing (this one of the very cool features of Processing!). 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)

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

Page last updated: September 2010

ċ
Gravity.pde
(2k)
Anurag Chugh,
Sep 17, 2010, 7:52 AM
ċ
SATELLITE.CPP
(2k)
Anurag Chugh,
Sep 17, 2010, 8:11 AM
Comments