One of the most useful forces we can create for our engine is a spring force. Although springs have an obvious use in driving games (for simulating the suspension of a car), they come into their own in representing soft or deformable objects of many kinds. Springs and particles alone can produce a whole range of impressive effects such as ropes, flags, cloth garments, and water ripples. Along with the hard constraints we’ll cover in the next chapter, they can represent almost any kind of object.
HOOK’S LAW
Hook discovered that the force exerted by a string depends only on the distance the spring is extended or compressed from its rest position. A spring extended twice as far will exert twice the force. The formula is therefore
\[f = -k\Delta l \]where \(\Delta 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. When it comes to three dimensions.The corresponding formula for the force is
\[f = -k(\lvert d \rvert - l_{0}) \widehat{d} \qquad \qquad [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.
THE LIMIT OF ELASTICITY
Real springs only behave this way within a range of lengths: this range is called their “limit of elasticity.” If you continue to extend a metal spring, eventually you will exceed its elasticity and it will deform. Similarly, if you compress a spring too much, its coils will touch and further compression is impossible.
SPRINGLIKE THINGS
Hook’s law applies to a huge range of natural phenomena. Anything that has an elastic property will usually have some limit of elasticity in which Hook’s law applies.We could simulate the buoyancy of water in the same way, connecting the submerged object to the nearest point on the surface with an invisible spring.
SPRINGLIKE FORCE GENERATORS
A BASIC SPRING GENERATOR
两个particle之间有个弹簧
class ParticleSpring : public ParticleForceGenerator
{
/** The particle at the other end of the spring. */
Particle *other;
/** Holds the spring constant. */
real springConstant;
/** Holds the rest length of the spring. */
real restLength;
public:
/** Creates a new spring with the given parameters. */
ParticleSpring(Particle *other,
real springConstant, real restLength);
/** Applies the spring force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
AN ANCHORED SPRING GENERATOR
一点固定,另一点连接着一个particle.
class ParticleAnchoredSpring : public ParticleForceGenerator
{
/** The location of the anchored end of the spring. */
Vector3 *anchor;
/** Holds the spring constant. */
real springConstant;
/** Holds the rest length of the spring. */
real restLength;
public:
/** Creates a new spring with the given parameters. */
ParticleAnchoredSpring(Vector3 *anchor,
real springConstant, real restLength);
/** Applies the spring force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
AN ELASTIC BUNGEE GENERATOR (松紧绳)
An elastic bungee only produces pulling forces. You can scrunch it into a tight ball and it will not push back out, but it behaves like any other spring when extended.
STIFF SPRINGS
In real life almost everything acts as a spring. If a rock falls onto the ground, the ground gives a little, like a very stiff spring.With a model of spring behavior we could simulate anything. Collisions between objects could be modeled in a similar way to the buoyancy force. With the correct spring parameters for each object, this method would give us perfect collisions. It is called the “penalty” method and has been used inmany physics simulators, including several used in games.
缺点:(不同spring constant引起问题)
In fact, to avoid having everything in a game look really spongy as things bounce around on soggy springs, we have to increase the spring constant so that it is very high. If you try to do that, and run the engine, you will see everything go haywire: objects will almost instantly disappear off to infinity, and your program may even crash with numerical errors. This is the problem of stiff springs, and it makes penalty methods almost useless for our needs.