next up previous
Next: Force Accumulation Up: How To Implement a Previous: Algorithm Outline

Create Object

Function which creates a simulation object simply get a point (BALLRADIUS,0) and rotate it with a specified angle ( = $1/NUMP$ part of $2*\pi$). It gives us a set of points which have to be connected by linear springs. It is simple procedure:

void CreateBall(void)
{
    int i;

    // create points
    for(i=1 ; i <= NUMP ; ++i)
    {
        myPoints[i].x = BALLRADIUS * 
          sin(i * (2.0 * 3.14) / NUMP );
        myPoints[i].y = BALLRADIUS *
          cos(i * (2.0 * 3.14) / NUMP ) + 
          SCRSIZE/2;
    }

    // create springs
    for(i=1 ; i <= NUMP ; ++i)
        AddSpring(i,i,i+1);
        AddSpring(i-1,i-1,1);
}

An $AddSpring$ function use mySprings[] table (mentioned before) to store springs. It simply get index in a spring table ($pi$) and indexes of first and second spring point (those indexes are just pointers in myPoints[] table). Function calculates length of the spring and put all informations in actual ($pi$) place in mySprings[] table:

void AddSpring(int pi, int i, int j)
{
    mySprings[pi].i = i;
    mySprings[pi].j = j;
    mySprings[pi].length = 
      sqrt( 
       (myPoints[ i ].x - myPoints[ j ].x) * 
       (myPoints[ i ].x - myPoints[ j ].x) + 
       (myPoints[ i ].y - myPoints[ j ].y) * 
       (myPoints[ i ].y - myPoints[ j ].y) 
      );
}

Now an object is created and we are able to start main part of algorithm.



Maciej Matyka 2004-03-30