• The previous version of the gravitational simulation produced rather abstract results. The lists of coordinates did not say much and the import for 3DsMax was not very handy.

    To overcome this problem I looked into openGL and found it quite easy to integrate.
    The glut-Package for DevC++ includes all necessary files and an example project.

    The whole initzalizing stuff was just copied from the example and the code from the earlier version adapted to produce the coordinates in the corrent form.

    All the objects are stored in a linked list. For each frame the list is traversed and the new coordinates displayed.

    Gravitational Simulation 3D

    Gravitational Simulation 3D

    I also added a linked list to each object that stores all past coordinates. When this list ist used to plot lines between each two adjacent points the trace of the object is displayed. Currently only the trace of 2 selected objects can be displayed at a time. Those two objects also get marked with little triangles and a 3d-crosshair. Their parameters are displayed in the upper left edge of the screen, along with some additional info. To visualize the forces, accelerations and velocities the corresponding vectors can be displayed.

    Velocity- and forcevectors displayed

    Velocity- and forcevectors displayed

    Code can be downloaded here (Executable included).

    Rotating the view is done by holding a mousbutton down and moving the cursor.

    Other commands:
    i    Toggle  Info
    x   Calculate Step
    z   Reset viewcenter to [0,0,0]
    o   Center blue object
    k   Center yellow object
    t   Toggle trace
    p  Select blue object
    L    Select yellow object

    f    Toggle force vectors
    b   Toggle acceleration vectors
    v   Toggle velocity and force vectors

    +-  Increase / decrease step size

    n   Reset world and generate 50 new objects randomly

    */  Increase / decrease gravitational constant

    5 and 0  zoom in and out

    Numblock to move viewcenter

    Tags: , , ,

  • 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.

    Download sourcecode

    Tags: , ,