The
Latest Springy Fashions
The
mass and spring dynamics simulation I developed in a previous column
("Collision
Response: Bouncy, Trouncy, Fun") proved effective for simulating
soft body objects in real time. I thought it should be possible to use
these techniques to create a cloth simulation. In fact, several of the
commercial cloth animation systems for 3D animation programs such as
3D Studio Max, Softimage, and Maya use similar techniques. So how do
I go about creating a piece of cloth?
I
am going to be using the same spring force formulas for the cloth simulation
as the ones I used in the March column. If you are unfamiliar with the dynamic forces generated
by springs, you should go back and read the March column or at least
take a look at the March source code on the Game Developer web site
(http://www.gdmag.com).
 |
|
Figure
1A. A simple
cloth grid.
|
I
start by creating a rectangular grid, and then connect each point to
neighboring points with springs, as you can see in Figure 1A. These
springs define the rough structure of the cloth and so I refer to them
as structural springs. The resulting cloth patch looks pretty good and
requires few springs. However, once I run the simulation, problems appear
immediately as shown in Figure 1B.
 |
| Figure
1B. Stretched cloth grid. |
The
simple spring connections are not enough to force the grid to hold its
shape. Much like the box in the March column, there are simply no springs
to maintain the shape. If I held on to only one point, the entire surface
would collapse into a single line creating a rope. Not exactly what
I wanted, but it points out something I want to address a bit later.
I really want to keep the model from shearing too much. That is, I want
the space between diagonal elements of the model preserved. So, I just
add a few more springs to the grid along the diagonals creating a group
of shear springs, as you can see in Figure 2A. Run this new structure
through the simulation and the results are much better, as you see in
Figure 2B.
 |
|
Figure
2a. Added
shear springs.
|
 |
|
Figure
2B. A better
cloth model.
|
This
new form of cloth works pretty well hanging from hooks on the wall.
However, if you drop the cloth on the floor, it wads up into a big mass
of springy spaghetti. The reason for this failure is that the model
is still incomplete. If you look at the structure in Figure 2A, you
may see that there is nothing to keep the model from folding along the
edges of the structural springs, much as you fold a handkerchief. The
fibers that comprise actual cloth run the length of the fabric and generally
resist folding and bending. In order to simulate this effect adequately,
I need to do a little more work.
My
research uncovered two methods for dealing with this problem. The first
minimized the bend between two adjacent cells by using the dot product
to determine the angle of bend. The second method simply added an extra
set of springs called flexion or bend springs to apply the bend force.
I created the bend springs by stretching a spring across two cells alongside
the structural springs. These springs end up connecting every other
cell in the cloth mesh.
 |
|
Figure
3. The interconnection of cloth springs.
|
I
prefer the second method because it works within the existing spring
system without the need for a new method for calculating forces. I also
get the benefit of having only one calculation to optimize later. For
other applications, it’s possible that the angle minimization method
may work out better.
I
now have a sufficient spring network to simulate a variety of different
types of cloth. You can see how all the springs are connected in Figure
3.
Stretch
Without Tearing
These
three types of springs make it possible to simulate a variety of different
cloth types. By varying the stiffness of the springs, it’s possible
to simulate anything from stiff cardstock to stretchy nylon. For example,
spandex would have very flexible structural and shear springs to allow
strong stretching capability. Paper, on the other hand, is very resistant
to shearing and stretching, so its springs would be very stiff. When
considering how much a material will bend, a surface such as cardboard
should have the very stiff bend springs to make it resistant to folding.
I find experimenting with different values for spring stiffness the
only real way to find adequate surface properties.
Stiff
springs can make a numerical simulation unstable. To combat this, it’s
important to use a good numerical integrator. The midpoint method and
Runge-Kutta integrators developed last month seem to do the trick nicely.
Making
It Move
 |
|
Figure
4. Collision boxes
allow for the draping effect
on this tablecloth.
|
I
already have a simulator from the March column that is capable of handling
a cloth patch. I can even apply gravity to it and lock the position
of individual vertices. That’s pretty interesting, but it needs some
improvement to come alive. In March, I also discussed the use of planes
for collision. With this same method, I can create collision boxes that
enable me to simulate a tablecloth draped over a table, as you see in
Figure 4.
This
model is interesting and realistic looking but not terribly animated.
In fact, in this case it’s probably better to freeze the simulation
and avoid the constant recalculation. Unless, of course, the wind kicks
up or someone pulls on the corner.
 |
|
Figure
5. Collision with
a sphere.
|
For
characters, a moving box is not the most realistic way to displace the
cloth. Moving bounding spheres allow much more pleasing character animation.
Fortunately, this is easy to add to the simulation. Determining whether
a point is inside a sphere is very easy. If the distance from the point
to the center of the sphere is less than the radius of the sphere, the
point is on the inside. If a point in the cloth is found inside a sphere,
I have a penetrating collision. Just like handling collisions in the
March simulator, I need to back up the simulation time to find the actual
point of contact. In a sphere, contact takes place when the distance
of the point to the sphere’s center is equal to the radius of the sphere.
Now that the contact point has been established, I need to resolve the
collision. The collision normal, N, between a point and a sphere is
the vector between the point of contact and the center of the sphere.
You can see this in Figure 5. Fortunately for me, the rest of the collision
response is handled just like the collision with a plane. This means
my existing collision response code works great.
 |
|
Figure
6. Cloth hanging from
a pair of invisible hips.
|
I
can now add collision spheres to my simulation. The cloth slides realistically
off the spheres. You can see how the simulation looks with two collision
spheres in Figure 6. By animating the spheres along with the 3D model,
I can get a nice animated hip sway and other alluring effects. The motion
of the cloth continues after the animation stops, creating entertaining
effects that are diffiicult to achieve with traditional animation techniques.
________________________________________________________
Problems
to Avoid and Ignore