Game Development Reference
In-Depth Information
return true;
First, the local variables r and i are declared. r will be used to compute the initial,
unstretched length of the springs, and i will be used to index the Objects and Springs
arrays. Second, Objects[0] , the one that is fixed, has its bLocked property set to true ,
indicating that it does not move (that is, it's locked).
Next, the particle positions are initialized starting from the first particle—the fixed one
positioned at the middle of the screen—and proceeding to the rest of the particles,
offsetting each to the right by an amount equal to property fLength . fLength is an
arbitrary length that you can define, that represents the spacing of the particles and,
subsequently, the initial length of the springs connecting the particles.
Finally, we set up the springs, connecting each particle to its neighbor on the right.
Starting at the first spring, we set its end references to the index of the particle to its left
and right in the properties End1 and End2 , respectively. These indices are simply i and
i+1 , as shown in the preceding code sample within the last for loop. The initial length
vector of the spring is computed and stored in the vector r , where r = Objects[i
+1].vPosition - Objects[i].vPosition . The magnitude of this vector is the initial
spring length, which is stored in the spring's property, InitialLength . This step isn't
strictly necessary in this example since you already know that the property fLength
discussed earlier is the initial length of each spring. However, we've done it this general
way since you may not necessarily initialize the particle positions as we have simply
Update the simulation
Updating the particle positions at each simulation time step, under the influence of
gravity and spring forces proceeds just like in the earlier examples of Chapter 8 and
Chapter 9 . Essentially, you must compute the forces on the particles, integrate the equa‐
tions of motion, and redraw the scene. As usual in our examples, the function Update
Simulation is called on to perform these tasks. For the current example, UpdateSimu
lation looks like this:
void UpdateSimulation(void)
double dt = _TIMESTEP;
int i;
double f, dl;
Vector pt1, pt2;
int j;
Vector r;
Vector F;
Vector v1, v2, vr;
// Initialize the spring forces on each object to zero.
for(i=0; i<_NUM_OBJECTS; i++)
Search Nedrilad ::

Custom Search