Game Development Reference
Springs.k = _SPRING_K;
Springs.d = _SPRING_D;
// Connect end of all remaining springs.
for(i=1; i<_NUM_LINKS; i++)
Springs[i].End1.ref = i-1;
Springs[i].End1.pt.x = Objects[i-1].fLength/2;
Springs[i].End1.pt.y = 0;
Springs[i].End2.ref = i;
Springs[i].End2.pt.x = -Objects[i].fLength/2;
Springs[i].End2.pt.y = 0;
pt = VRotate2D(Objects[i].fOrientation, Springs[i].End2.pt)
r = pt - (VRotate2D(Objects[i-1].fOrientation, Springs[i].End1.pt)
Springs[i].InitialLength = r.Magnitude();
Springs[i].k = _SPRING_K;
Springs[i].d = _SPRING_D;
The local variables r and i are the same as before; however, there's a new variable, pt ,
that we use to temporarily store the coordinates of specific points when converting from
one coordinate system to another. We'll see how this is done shortly.
After the local variables are declared, the Object positions are initialized, starting from
the first Object positioned at the middle of the screen and proceeding to the rest of the
Object s, offsetting each to the right by an amount equal to property fLength . Here,
fLength is an arbitrary length representing the length of each rigid body, not the length
of the springs connecting each rigid body. As you'll see momentarily, the initial length
of all the springs in this example is 0.
You should be aware that the coordinates for each object computed here are the coor‐
dinates of the object's center of gravity, which in this example we defined as the middle
of the rectangle representing each object. Since these are rigid bodies, not only must
you specify their initial positions, but you must also specify their initial orientations as
shown in the preceding code sample. The way we have this example set up, each object
is initialized with an orientation of 0 degrees.
The next task is to set up the spring connecting the first link, the one on the left, to a
fixed point in space. The following code handles this task:
// Connect end of the first object to a fixed point in space.
Springs.End1.ref = −1;