Simulating Cloth
In the modern world of computer graphics, flowing cloth is becoming more of a common presence in the visuals of computer animation. It's no longer practical for animators to manually animate the movement of the cloth as it reacts to the surfaces of the world, particularly in an environment such as a computer game where the environment may be dynamically changing. To get realistic motion in cloth, we have to create a physical model for the cloth.
The Model
The most commonly used cloth model is to create a lattice of cloth "nodes" connected by several types of "springs". The nodes have a mass and velocity, and represent the bulk of the cloth, while the springs keep the nodes in a configuration recognizable as the original cloth, and allow the general shape of the cloth to bend and stretch, while still retaining the original shape when at rest.
The springs apply forces to the nodes, which then apply the forces to their own velocities, and then the positions are updated with the new velocities. Then the forces for the springs are recalculated, etc. The three types of springs typically used in these simulations are structural springs, shear springs, and bend springs, each of which gives the cloth a certain physical characteristic.
Structural Springs
Structural springs give the cloth
elasticity. They connect all nodes to their
adjacent neighbors, vertically and horizontally. Strong structural springs are similar to
paper, or
silk; they resist changes to the surface area of the cloth. A cloth patch with weak structural springs would be more similar to
nylon or
rubber.
Shear Springs
Shear springs connect nodes to their diagonal neighbors. This prevents the cloth from collapsing along the diagonal. This is normally accomplished in cloth by the
density of the
weave. So a cloth with weak shear springs would be a loosely woven cloth, so that you could pull on opposite corners of the cloth and see the cloth elongate along that axis while the other axis would shorten. Surface area isn't changed, but the shape collapses from a
square to a
rhombus. Stronger shear springs resist this tendency to collapse.
Bend Springs
Bend springs connect every node to the node just past their neighbors, vertically and horizontally. From a coordinate view, structural springs connect (x,y) to (x-1,y), (x+1,y), (x,y-1), and (x,y+1). Shear springs connect (x,y) to (x+1,y+1),(x-1,y+1), (x+1,y-1), and (x-1,y-1). Bend springs connect (x,y) to (x-2,y), (x+2,y), (x,y-2), and (x,y+2). The bend springs are what give the cloth the resistance to
folding and
bending. Weaker bend springs would create a cloth that could easily collapse onto itself, like silk once again. Stronger bend springs create a texture more like paper, which resists folding without a strong pressure. An interesting note is that when paper is pushed past a
threshold, it
creases. This can also be simulated by giving bend springs a property beyond which they permanently alter their resting length. However, to get desirable results with this, the cloth mesh must be very detailed, which is very computationaly expensive.
Collision Detection
Ok, so we have a bunch of nodes now, and we can pretty easily map a
triangle strip onto the surface to render it. Unfortunately, the cloth patch can pass through itself and other objects.
Collision detection with other objects is not so difficult. Generate a
bounding box for the cloth based on the furthest nodes in each direction. Then you can use your own collision detection algorithms to decide whether the cloth may or may not be colliding, and if so, check each node individually. Since each node has a mass and velocity, you can handle the collision however you want with your
rigid body physics system, treating each cloth node as a particle.
Cloth self collision is much more complex, however, because each node must be compared to every other node, giving us an N squared algorithm, which is terribly slow. The most accurate methods for resolving this entail using polygon intersection tests for every section of the cloth. This is probably too slow for most applications, however, forcing us to find an alternate method.
One method of approximating self collision prevension is to give each node a repulsive force to all other non-neighboring nodes. This works well unless extreme forces are applied to a section of the cloth, in which case cloth nodes can pass through, creating a section of cloth locked into itself by having nodes on either side of another section, each trying to get away from the intersection line. However, in practice, this usually resolves itself quickly, as it's usually a corner that passes through. The corner will have less nodes pushing away from the cloth than the other side, allowing the other side to pull the nodes through with the same force.
You can also reduce this tendency to pass through itself by increasing the radius of the force generated by each node. This algorithm naturally creates a "force field" which rests between cloth folds. Increasing the force radius makes this only more obvious, and this effect which can normally be passed off as cloth "thickness" by the human perception, starts to become unrealistic.