Game Development Reference
In-Depth Information
Basically, the first Object , Object[0] , is located somewhere toward the top middle of
the screen with an initial rotation of ninety degrees so that it stands vertically. The second
Object , Object[1] , is positioned so that it lies horizontally with its left end coincident
with the lower end of the first object. We'll put a spring there momentarily, but first,
we'll connect a spring to the upper end of the first object to connect it to a fixed point.
The following code takes care of that spring using the same techniques discussed earlier:
// Connect end of the first object to the earth:
Springs[0].End1.ref = −1;
Springs[0].End1.pt.x = _WINWIDTH/2;
Springs[0].End1.pt.y = _WINHEIGHT/8;
Springs[0].End2.ref = 0;
Springs[0].End2.pt.x = -Objects[0].fLength/2;
Springs[0].End2.pt.y = 0;
pt = VRotate2D(Objects[0].fOrientation, Springs[0].End2.pt) +
Objects[0].vPosition;
r = pt - Springs[0].End1.pt;
Springs[0].InitialLength = r.Magnitude();
Springs[0].k = _SPRING_K;
Springs[0].d = _SPRING_D;
Now, we connect a spring at the corner formed by the two objects using the following
code:
// Connect other end of first object to end of second object
i = 1;
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) +
Objects[i].vPosition;
r = pt - (VRotate2D(Objects[i-1].fOrientation, Springs[i].End1.pt) +
Objects[i-1].vPosition);
Springs[i].InitialLength = r.Magnitude();
Springs[i].k = _SPRING_K;
Springs[i].d = _SPRING_D;
If we stop here, the simulation will behave just like the linked-chain example, albeit we'll
have a very short chain. So, to prevent rotation at the corner, we'll add another spring
connecting the centers of gravity of the objects. You can use other points if you desire;
we chose the centers of gravity for convenience. The following code adds this rotational
restraint spring: