Two years ago I started coding a small program in C++ to simulate gravitational forces between masspoints. After seeing a video of a simulation of matter clustering under gravitation in my astrophysics lecture I decided to continue and improve the code.
I wanted the simulation to be easily extentable, so I made a class for the objects, which holds their parameters and some I/O-functions, and a class for the world, which contains all objects in a list and the functions to calculate their interactions.
The algorithm is quite simple. For each calculation step the world goes through all objects and calculates the forces excerted on it by every other object. It then calculates the acceleration of the object and subsequently its new velocity and position.
For visualisation of the results the coordinates of each object are written to a file after each step. (This part isn’t working correctly yet. There seems to be some problem with the fstream.)
These coordinates are then read by a 3ds-max script to keyframe coordinates of spheres or alternatively to pointcoordinates of splines.
I also wrote a similar algorithm in Delphi, which only computes the movement of two masspoints, but directly visualises the result by drawing to lines. The linewidth varies with the absolute value of the z-position and the hue represents the speed at this point.
A more detailed description of the mathematical background can be found here.