A Simple Cloth Simulator

Selected Topics in Computer Graphics

A Simple Cloth Simulator

Authors

Getting the simulator to work

Controlling the simulator

Mouse Control

Use the left button to control the arcball and rotate the cloth.
Use the right button to control the arcball and rotate the light source.

Keyboard control

KeyDescription
mChange cloth movement algorithm (none, Euler, midpoint, Runga-Kutta)
xToggles adaptive step on/off
pPause
spaceMoves the cloth a single step
dSwitch particles drawing method (none, dots, lines)
cToggles cloth drawing on/off
oSwitch obstacle drawing method (none, wire, solid)
nToggles normal drawing on/off
bToggles blending on/off
fFocus on the cloth
aToggles auto-focus on/off
rResets the cloth's parameters (position and velocity)
lLoads a different cloth file
kReloads the last cloth from file
tResets the cloth's and light's arcballs
s(Un)stuck the stationary points in place
qQuit

Cloth file format

AttributeParametersDescriptionDefault value
Sizeint u,vThe cloth's size: uxv20 20
Massdouble mThe mass of each particle2
Basedouble x,y,zThe base position of the cloth (particle 0,0 that the cloth is positioned by)0 0 0
Direction({x,y,z},{x,y,z})The direction to the u,v axis of the cloth. i.e for standing cloth Direction y xx z
Distancedouble dThe starting distance between each two neighboring particles (up,down,left,right)1
Breakingdouble dThe maximal stretch between two neighboring particles before the cloth tears. To disable tearing set to 00
Stepint sThe number of milliseconds between each simulation step20
Timedouble tThe number of seconds that had passed between each step (delta(t))0.02
Gdouble gThe gravity constant20
KSdouble KSKS value for all 3 spring types
KDdouble KDKD value for all 3 spring types
Neardouble KS,KDThe KS and KD of the near spring (up,down,left,right)3000 50
Fardouble KS,KDThe KS and KD of the far spring (up up,down down,left left,right right)500 10
Crossdouble KS,KDThe KS and KD of the cross spring (up left, up right,down left, down right)2000 10
Dragdouble dThe drag force constant (0 to disable)0.2
Winddouble x,y,z,fThe wind's direction (x,y,z) and power p (set power 0 to disable)1 0 0 0
Posint u,v double x,y,zSets the starting position of particle (u,v) to (x,y,z)Per particle according to grid
Stuckint u,vMakes particle (u,v) stationary(non stationary by default)
Holeint u,vDisconnects particle (u,v) from all its neighbors(no holes by default)
Color{front, back, background} double r,g,b,aSets the color of the cloth (two sides) and the color of the backgroundfront 0.2 0.2 1.0 1.0
back 0.2 1.0 0.2 1.0
background 0 0 0 1
BlendingnoneBegins simulation with blending turned on(blending is off by default)
Movement{null, euler, midpoint, runga-kutta}The movement algorithm to userunga-kutta
Spheredouble x,y,z,rPlaces a sphere obstacle with the center at (x, y, z) and with radios r(No spheres by default)
AdaptivenoneBegins simulation with adaptive step turned on (adaptive step is off by default)

An example cloth file with the default values is available.

Technical details, implementation, special features, etc.

Cloth

The cloth is represented by an array of particles (particle_t). Each particle (besides the position, velocity, forces and mass) has 4 pointers to its neighbors (left, bottom, right and up). Using this implementation, it was easy to rip the cloth apart by simply disconnecting far away particles (originally used as a debugging tool), or to make holes in the cloth (or splitting it completely).

Movement

Three movements algorithms are implements: Euler (forward), midpoint and Runga-Kutta (4th order). In addition, the adaptive step algorithm was implemented, and can be activated on each of the movement algorithms. We also read about higher order Runga-Kutta and about a method to do adaptive step Runga-Kutta using 6th order. But we decided that it wasn't that interesting to implement as even Euler with adaptive step was stable and fast enough on most clothes.

Forces

Each force type, calculates it's affect on a particle, and add the value to the particle's total force.

Obstacles

Currently there is only one obstacle - a sphere. This is (very) simply implemented, by simply checking the position of the particles as they move, and throwing them outside the sphere if they get inside. This isn't a very good simulation of the real world, but it acts nicely on our cloths.

Example clothes

Every exploding/riping cloth is best viewed with dots drawing enabled (the 'd' key).

To show how our exercise behaves we chose some clothes with interesting features,and some which don't behave exactly as one might have wanted but still are fun to see. (at least for us...).

hanging image

Most of the cloth simulation work is about finding the right parameters but until you do, you can expect some unstable behavior. For example while trying to stabilize the 50x50 hanging.cloth we made it too heavy causing it to rip (rip.cloth), or we chose too strong springs creating a nice vibration effect (wavy.cloth) or making it explode all together (shredded.cloth better viewed with dots enabled). And last but no least tearUp.cloth which is not related to the hanging but still has nice tearing effect.The biggest cloth we tried which is a good example for the power of adaptive step is huge.cloth. Disabling the adaptive step will cause a shower of cloth particles (With dots on of course).

shredded image
falling image

Another set of parameters we got to play with was the wind and drag constants. We placed 3 clothes, with no stationary points, and let them fall. In addition, the edge of each of them was a bit tipped over. in falling.cloth the cloth starts to fall left because of the drag, although the wind is to the right. In falling2.cloth, the edge is tipped stronger, letting the wind have more effect. in falling3.cloth, there is no wind at all.

A feature we found very interesting was the hole feature. We added a simple ability to disconnected particles. This came up after adding the tearing effect and seeing that it created some cool clothes. We took for example flagbig.cloth (which got bigger since) and with a few holes created flag.cloth, which is a different type of flag shape. Another example for the hole feature is four.cloth we actually have 4 different clothes (with dots on you can see the disconnected particles). And of course we can use the holes to make, well, holes in the cloth like in holes.cloth.

flag image
bullets image

We didn't have time to make all the obstacles we planned, for example we didn't get to add a floor, but we felt that we must have some obstacle, so we used a sphere. A simple example will be our sphere.cloth and sphere2.cloth which are just a cloth falling on a basic sphere.We of course added the ability to add more than one sphere (as seen in sphere2.cloth). A fun thing we noticed was the impact of the spheres on the cloth when the sphere is small enough and the speed is high enough. We created bullet.cloth and then by the long tradition of violent games created bullets.cloth....

Last some wind experiments: simulating a sail in sail.cloth, few nice strips in the wind in strip2.cloth and strip3.cloth and attempt at water.cloth simulation using wind and playing with the springs constants.

water image