Game Development Reference

In-Depth Information

Upon entering the
for
loop in the preceding code sample,
End1
of the current spring is

checked to see if it's connected to a fixed point in space. If so, the temporary variable

pt1
stores the global coordinates of the endpoint, and the variable
v1
stores the velocity

of the endpoint, which is 0. If the endpoint reference is a valid
Object
, then we compute

the position of the endpoint, stored in
pt1
, just like we did in the
Initialize
function,

using a coordinate transform as follows:

pt1 = Objects[j].vPosition + VRotate2D(Objects[j].fOrientation,

Springs[i].End1.pt);

We compute the velocity of that point as shown in
Chapter 9
by first computing the

velocity of the point due to rotation in body-fixed coordinates, converting that to global

coordinates, and then adding the result to the
Object
's linear velocity. This is accom‐

plished in the following code:

v1 = Objects[j].vVelocity + VRotate2D(Objects[j].fOrientation,

Objects[j].vAngularVelocity^Springs[i].End1.pt);

We then repeat these calculations for the second endpoint of the spring.

Once we've obtained the positions and velocities of the spring endpoints, we compute

the spring-damper force in the same manner as in the rope example. The resulting spring

forces are aggregated in the
vSprings
property of each object. Note that if the spring

endpoint reference is a fixed point in space, we do not aggregate the force on that fixed

point.

Since the
Object
s are rigid bodies here, we now have to compute the moment due to

the spring force acting on each object. You must do this so that when we integrate the

equations of motion, the objects rotate properly.

For the
Object
connected to
End1
of the current spring, the following lines compute

the moment:

Fo = VRotate2D(-Objects[j].fOrientation, F);

r = Springs[i].End1.pt;

M = r^Fo;

Objects[j].vMSprings += M;

Fo
is a vector representing the spring force computed earlier on the current
Object
in

the current
Object
's local, body-fixed coordinate system. The line:

Fo = VRotate2D(-Objects[j].fOrientation, F);

transforms
F
from global to local coordinates of the current
Object
,
Object[j]
.

r
is set to the local, body-fixed coordinates of the spring attachment point for the current

Object
, and we compute the resulting moment by taking the vector cross product of
r

with
Fo
. The result is stored in the vector variable
M
, which gets aggregated in the
Object

property
vMSprings
. We then perform these same sorts of calculations for the
Object

connected to the other end of the spring.