Game Development Reference
InDepth Information
where
l
is the distance the spring is extended or compressed, and
k
is the “spring
constant,” a value that gives the stiffness of the spring. The force given in this equa
tion is felt
at both ends
of the spring. In other words, if two objects are connected
by a spring, then they will each be attracted together by the same force, given by the
preceding equation.
Notice that we have used
l
in the equation. This is because, at rest, with no forces
acting to extend or compress the spring, it will have some natural length. This is also
called the “rest length” and has the symbol
l
0
. If the spring is currently at length
l
,
then the force generated by the spring is
f
=−
k(l
−
l
0
)
So far we have considered Hook's law only in terms of a onedimensional spring.
When it comes to three dimensions, we need to generate a force vector rather than a
scalar. The corresponding formula for the force is
k

l
0
d
f
=−
d
−
[6.1]
where
d
is the vector from the end of the spring attached to the object we're generating
a force for, to the other end of the spring. It is given by
d
=
x
A
−
x
B
[6.2]
where
x
A
is the position of the end of the spring attached to the object under consid
eration, and
x
B
is the position of the other end of the spring.
Equation 6.1 states that the force should be in the direction of the other end of
the spring (the
d
component), with a magnitude given by the spring coefficient multi
plied by the amount of extension of the spring—the
element
is the magnitude of the distance between the ends of the spring, which is simply the
length of the spring, making
−
k(

d
−
l
0
)
part. The

d

l
0
)
.
Because equation 6.1 is defined in terms of one end of the spring only (the end
attached to the object we are currently considering), we can use it unmodified for the
other end of the spring, when we come to process the object attached there. Alterna
tively, because the two ends of the spring always pull toward each other with the same
magnitude of force, we know that if the force on one end is
f
, then the force on the
other will be
−
k(

d
−
l
0
)
just a different way of writing
−
k(l
−
f
.
In the force generator described in this chapter we will calculate the force sepa
rately for each object, and do not make use of this fact. A more optimized approach
might use the same force generator for both objects involved, and cache the force cal
culation from one to save time recalculating it for the other. A force generator of this
kind is provided in the source code on the CD.
−