Game Development Reference
In-Depth Information
Finally, we compute the initial length of the spring by taking the magnitude of r and
storing the result in the spring's InitialLength property.
With the first spring out of the way, the Initialize function enters a loop to set up the
remaining springs. Proceeding from left to right in Figure 13-5 , the first endpoint,
End1 , is connected to the right side of Object[i-1] , and the second endpoint, End2 , is
connected to the left side of Object[i] . Be aware that each endpoint of each spring is
specified in different coordinate systems. The left end is in the coordinate system of
Object[i-1] , while the right end is in the coordinate system of Object[i] . It may seem
trivial during this setup, but when things start moving and rotating it is critically im‐
portant to keep these coordinate systems straight. Doing so involves transforming each
endpoint coordinate from the local system of the body to which it's attached to the global
coordinate system. This is illustrated as follows:
pt = VRotate2D(Objects[i].fOrientation, Springs[i]
+ Objects[i].vPosition;
r = pt - (VRotate2D(Objects[i-1].fOrientation, Springs[i]
+ Objects[i-1].vPosition);
The first line converts the spring attachment point End2 from the local coordinate system
of Object[i] to global coordinates by performing a rotation and translation using
functions you've already seen numerous times now. The result is temporarily stored in
the local variable, pt . The second line converts the spring attachment point End1 from
the local coordinate system of Object[i-1] to global coordinates and then subtracts
the result from pt , yielding a vector, r , representing the relative distance between the
spring's endpoints. The magnitude of r is the spring's initial length. Performing these
same calculations during the simulation will result in the spring's stretched or com‐
pressed length. That calculation is performed in UpdateSimulation .
The function UpdateSimulation is substantially the same as that discussed in the rope
example. There are a few differences that we'll highlight here. Again, these differences
are due to the fact that we're now dealing with rigid bodies that rotate rather than simple
particles. The following code sample shows the additions to UpdateSimulation . You
can see there are a couple of new variables, M and Fo . M is used to temporarily store
moments due to spring forces Fo in the local coordinates of each Object .
Just as the property vSprings was initialized to 0 at the start of UpdateSimulation , so
too must we initialize vMSprings to 0. Recall, vSprings aggregates the spring forces
acting on each Object . For rigid bodies that rotate, we'll use vMSprings to aggregate the
moments on each Object resulting from those spring forces:
void UpdateSimulation(void)
Search Nedrilad ::

Custom Search