Game Development Reference
In-Depth Information
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();
Springs[0].k = _SPRING_K;
Springs[0].d = _SPRING_D;
The first spring, Spring[0] , has its first endpoint, End1 , set to refer to −1 , which, as
explained earlier, means that this end of the spring is connected to some fixed point in
space. The location of the point, stored in the End1.pt property, must be specified in
global coordinates as shown previously.
Now the second end of the first spring is connected to the left end of the first link;
therefore, End2.ref of the first spring is set to 0 , which is the index to the first Object .
The point on Object[0] to which the spring is attached is the leftmost end on the
centerline of the object; thus, its coordinates—relative to the object's center of gravity
location and specified in local, body-fixed coordinates—are:
Springs[0].End2.pt.x = -Objects[0].fLength/2;
Springs[0].End2.pt.y = 0;
Now remember, the points on Objects to which springs are attached are specified in
body-fixed, local coordinates of each referenced object, whereas any point fixed in space
to which a spring is attached and not on an Object must be specified in global, earth-
fixed coordinates. You have to keep these coordinates straight and make the appropriate
rotations when computing spring lengths throughout the simulation. The code;
pt = VRotate2D(Objects[0].fOrientation, Springs[0].End2.pt)
+ Objects[0].vPosition;
r = pt - Springs[0].End1.pt;
illustrates how to do this. To compute the initial spring length, we need to compute the
relative distance between the endpoints of the spring. In case of the first spring, End1
was specified in global coordinates, but End2 was specified in the local coordinate system
of Object[0] . Therefore, we have to convert the coordinates of End2 from local coor‐
dinates to global coordinates before calculating the relative distance between the ends.
The preceding line, which calls the VRotate2D function you saw in earlier chapters,
rotates the locally specified point, End2.pt , from local to global coordinates; it then adds
the Object 's position to the result, arriving at a point, pt , in global coordinates coinci‐
dent with the second endpoint of the spring. The relative distance, r , is the second
endpoint, pt , minus the first endpoint, End1.pt .
Search Nedrilad ::




Custom Search