Game Development Reference
Initialize the particles and springs
Initially, our particle rope is set up horizontally, as shown in Figure 13-3 , with the left‐
most particle, p0 , fixed—that is, the particle p0 will not move, and the remainder of the
rope will pivot about p0 . For convenience, all remaining particles are incrementally
indexed from left to right.
Figure 13-3. Particle rope setup
There are nine springs, which are indexed from left to right as illustrated in
Figure 13-3 . Spring 0 connects particle 0 to particle 1, spring 1 connects particle 1 to
particle 2, and so on. The following code sample shows how all this is initialized:
Objects.bLocked = true;
// Initialize particle locations from left to right.
for(i=0; i<_NUM_OBJECTS; i++)
Objects[i].vPosition.x = _WINWIDTH/2 + Objects.fLength * i;
Objects[i].vPosition.y = _WINHEIGHT/8;
// Initialize springs connecting particles from left to right.
for(i=0; i<_NUM_SPRINGS; i++)
Springs[i].End1 = i;
Springs[i].End2 = i+1;
r = Objects[i+1].vPosition - Objects[i].vPosition;
Springs[i].InitialLength = r.Magnitude();
Springs[i].k = _SPRING_K;
Springs[i].d = _SPRING_D;