Game Development Reference
In-Depth Information
Figure 13-5. Linked-chain setup
The code for this setup is only a little more involved than that for the rope example; the
additional complexity is due to having to deal with specific points on the rigid bodies
to which each spring is attached. The following code sample contains the modified
Initialize function:
bool Initialize(void)
{
Vector r;
Vector pt;
int i;
// Initialize objects for linked chain.
for(i=0; i<_NUM_LINKS; i++)
{
Objects[i].vPosition.x = _WINWIDTH/2 + Objects[0].fLength * i;
Objects[i].vPosition.y = _WINHEIGHT/8;
Objects[i].fOrientation = 0;
}
// Connect end of the first object to a fixed point in space.
Springs[0].End1.ref = −1;
Springs[0].End1.pt.x = _WINWIDTH/2-Objects[0].fLength/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();
 
Search Nedrilad ::




Custom Search