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] = _WINWIDTH/2;
Springs[0] = _WINHEIGHT/8;
Springs[0].End2.ref = 0;
Springs[0] = -Objects[0].fLength/2;
Springs[0] = 0;
pt = VRotate2D(Objects[0].fOrientation, Springs[0] +
r = pt - Springs[0];
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
// Connect other end of first object to end of second object
i = 1;
Springs[i].End1.ref = i-1;
Springs[i] = Objects[i-1].fLength/2;
Springs[i] = 0;
Springs[i].End2.ref = i;
Springs[i] = -Objects[i].fLength/2;
Springs[i] = 0;
pt = VRotate2D(Objects[i].fOrientation, Springs[i] +
r = pt - (VRotate2D(Objects[i-1].fOrientation, Springs[i] +
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:
Search Nedrilad ::

Custom Search