# Cloth simulation

## Recommended Posts

Mystery    136
Came across this over the net. Realized that it can done using a sort of physics mechanics - mass spring model. There is a something I couldn't understand though. What should be the initial values of the each particle like mass and velocity? I think mass should be easy since we can assume all particles have the same mass. But what the velocities? On first thought, I would initialize it to 0(Resting position). However on second look at the formula, it will not move at all if I do that. I realized also that we can external forces like gravity but it seems to affect only the z-component. Seems to me like the position of x and y components will not move at all. Am I missing something here?

##### Share on other sites
lonesock    807
if you have wind effects, things get very dynamic. Also, if the cloth is attached to something, that might make a good starting velocity.

##### Share on other sites
Mystery    136
So you must assign different initial velocity to different particles?

##### Share on other sites
Guest Anonymous Poster
I think neverwinter nights uses something like this. You should ask some modders for the jist of how its implimented. Dangley mesh I think they were calling it.

##### Share on other sites
Intamin AG    122
Initial velocity is 0 but you can have it drop under influence of gravity and change its velocity through that no?

##### Share on other sites
Mystery    136
Quote:
 Original post by Intamin AGInitial velocity is 0 but you can have it drop under influence of gravity and change its velocity through that no?

Yes. That was I thought so. But I think by using gravity, we can only affect the z-component of the velocity.

I believe in a 3D space, the velocity is made of 3 components rite?

##### Share on other sites
Mystery    136
This is the formula I am talking about:

L - spring vector
l - length of the spring (scalar)
ks - spring constant
kd - damping constant
v1,v2 - velocities of spring's ends
r - rest length of the spring

F = -{ks(l-r) + kd[(v1-v2)*L]/l}*L/l

Each v should have 3 components right, x-axis, y-axis, z-axis?
So what should we initialize it at the beginning of the simulation?

If the same velocity is given to all particles, v1-v2 = 0, also l-r = 0 since the displacement is the same resulting in no extension. In the end, force is equal to 0.

This is getting really confusing.

##### Share on other sites
Jingo    582
Have you read Thomas Jackobsens article?

Has a great model for cloth simulation.

##### Share on other sites
Alright, I may not understand all the physics involved completely (obvious if you see my other posts), but here is the general idea. Your colth looks like this with dots being masses, lines being springs, and the plusses are a flag pole to which the cloth is connected:
*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+                 +                 +

Now, considering it starts off with zero velocity, gravity applies a downward force to all the masses. You're right, gravity gives each one an initial velocity of (0,0,-whatever). But, the masses on the flag pole can't move, so they stay still. The springs connected between the mass on the flag pole and the one next to it stretch. When the spring can strecth no more, it pulls the mass connected to the flag-pole-mass towards the flag pole. This, in turn, pulls each mass connected to it closer to the flag pole. See where I'm going?

If you had a cloth that was not connected to a flag pole and dropped it, in your virtual world (no wind, perfect hand timing when dropping it) then each mass would fall down at the same speed staying the same distance from each other.

Considering that the springs aren't actually springs at all, but just "connectors". If they were springy, then the top masses would fall faster upon release because the cloth was stretched due to gravity while being held up.

Basically, it's a chain reaction. One mass stays still, the others strecth away and then come back, pulling all the others with it, too. You may want to check out SodaPlay's SodaConstructor to see how masses and springs interact. Build a flag, like the one drawn with a few fixed points, then turn on gravity and you'll see what I mean.

##### Share on other sites
Mystery    136
Quote:
 Original post by DuncanBojanglesAlright, I may not understand all the physics involved completely (obvious if you see my other posts), but here is the general idea. Your colth looks like this with dots being masses, lines being springs, and the plusses are a flag pole to which the cloth is connected:*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+| | | | | | | | |+*-*-*-*-*-*-*-*-*+ + +Now, considering it starts off with zero velocity, gravity applies a downward force to all the masses. You're right, gravity gives each one an initial velocity of (0,0,-whatever). But, the masses on the flag pole can't move, so they stay still. The springs connected between the mass on the flag pole and the one next to it stretch. When the spring can strecth no more, it pulls the mass connected to the flag-pole-mass towards the flag pole. This, in turn, pulls each mass connected to it closer to the flag pole. See where I'm going? If you had a cloth that was not connected to a flag pole and dropped it, in your virtual world (no wind, perfect hand timing when dropping it) then each mass would fall down at the same speed staying the same distance from each other. Considering that the springs aren't actually springs at all, but just "connectors". If they were springy, then the top masses would fall faster upon release because the cloth was stretched due to gravity while being held up.Basically, it's a chain reaction. One mass stays still, the others strecth away and then come back, pulling all the others with it, too. You may want to check out SodaPlay's SodaConstructor to see how masses and springs interact. Build a flag, like the one drawn with a few fixed points, then turn on gravity and you'll see what I mean.

Hi Duncan

Thanks for the link to the SodaConstructor. It is really cool.

Coming back to the cloth simulation, so we have an initial velocity of (0,0,-whatever) due to the gravity. The Z-component of the velocity has a value now and it will result in some kind of displacement to the particle. This will result in a stretch in the spring and so on. We can then get the force, then acceleration and finally a new velocity. SO far so good over this area. But what about the x, y component? SOmehow I don't see how it can be changed since they is no stretch in those directions.

##### Share on other sites
Mystery    136
Quote:
 Original post by JingoHave you read Thomas Jackobsens article?Has a great model for cloth simulation.

Thanks for the link. I gave a quick browse. Seems like they left out the initial conditions of the particle system and also the velocity to simplify things. Will look at it again.

##### Share on other sites
The x and y components do change. Think about it this way: You've got a length of thick string, like clothes line (string like sewing thread is not a good example because the air resistance affects it a lot). You hold the string outstretched between your two hands. Then, with your left hand you let go. If you consider the string to be a finite set of masses connected by springs, like this:
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*()()()()   <-right hand

Gravity affects each one of those masses the same, but because your all powerful right hand is holding the right end, that end cannot fall. The x and y components do change because of this simple principle: Imagine a swing. Imagin looking at this swing from the side, like this:
   /\                   .  /||\                  . / || \                 ./  ==  \

You know that when the swing goes up, it moves in a circular motion, following the arc determined by the length of chain. Now, compare this to your rope. Your rope is like the swing at the top of its, erm, well, swing.
   /======||  <- swing  rope -> *-*-*-*-*-*-*-*-*-*()()()()<-hand  /  \                             .       /    \                             ./      \

When the swing falls, due to gravity, it is constrained to its top point, center, whatever you want to call it. So it falls, and follows its circular path towards the bottom. Your rope does the same thing, but instead of one mass, or swing, there are many masses, each connected to the mass before it. And for a cloth, it is the same thing, just with an extra dimension.

The math involved in this isn't that difficult. Since you are only dealing with masses and springs, you only have to worry about gravity and the forces of the springs, nothing else. Just remember that a spring's force acts along the length of the spring. Example:
#----------*

Two masses, one spring. The mass on the left is fixed where it is, it cannot move. It is a stone. In midair. The mass on the right can freely move.
So, in the very beginning, the spring is "at rest", and gravity starts pulling on the right mass. Well, the right mass falls straight down for a very brief period of time.
#----                    .     -------*

Then, in the next time interval, the spring is stretched out, due to the fact that the right mass moved down a little and got a little farther away. So, in addition to gravity acting on the right mass, the spring is also pulling the right mass towards the left mass. The rock.
#--                    .   \--                 .      \                .        -*

Then, gravity pulls again, but this time the spring is really pulling back on that little mass after being stretched out so much.
#|                         .   |                        .      |                      .       *                       .

Eventually, the mass will be hanging below the rock, bobbing up and down as the spring settles into equlibrium.

Well, I hope this helped, and I know, ASCII sucks. Unless you have aalib.

edit: ASCII seriously blows. This is my fourth edit.

##### Share on other sites
Guest Anonymous Poster

Mystery, let me add a little bit to this convo, if you don't mind. Your thinking is a little bit off. one of newtons laws of motion states that an entity remains at a constant velocity until acted upon by an external force. (zero is also a constant velocity, thus the object can be at rest or moving through space or whatever). now... gravity is an external force... it acts upon every thread/molucule/atom/whatever of a piece of cloth. so do other forces... for instance wind, a moving object like a man walking who is wearing a shirt, a flag pole providing a normal force (if you don't even know what a normal force is, do not attempt to proceed with this project until you take a college level physics course, or even better... a university level physics course, difference being that a University course is calculus based rather than algebra based) but at any rate... you do need to understand simple concepts & the laws that describe the world around us. physics is math applied to the physical world. about the cloth & how it crumples or waves... I can describe this to you in 2 ways... but both hitting on the same concept.

When a car smashes into the side of an immovable object (say an 12 foot thick cement wall), how come the hood of the car just doesn't stop cold, how come it changes shape? Its because the small molecules on the front of the hood experience the force of impact first & they push back the molecules behind them, this pushing back is accompanied by the force of the rest of the hood's momentum, because the hood is still traveling at a velocity forwards & perhaps the rest of the car which is pushing forward on the hood in some fasion. So the front molecules of the car keep pushing back over time until they have applied a rearward facing force long enough to dispense the energy stored up in the car's motion, this force that is pushing back keeps applying force to molecules further & further back on the hood... like dominios, & sometimes the force forward from the rest of the hood pushes back enough to actually bend metal in mid-air, how awesome is that? In a sense, you can think of hood as the same spring-mass system as the cloth... only its "springs" are much much stronger (thus, a stronger material than cloth... steel).

As far as cloth, forces to keep in mind that will act upon cloth:
1. Any normal force applied to the cloth from another object (flag pole with a flag, human body wearing a t-shirt, bed sheets hanging off the bed). In relation to the object, the normal force is exactly equal to the force the cloth applies to the object & opposite in direction. A flag pole has 2 points of contact usually with the flag, so two places to calculate normal force. So does a piece of laundry hanging from a clothes-line.

2. WIND, but this is too general; "wind" affects the cloth in several ways... maybe I should say "air" instead.

2.a Wind blowing with a vector (in any direction, xyz).

2.b Friction from wind along the sides of a moving object. Sometimes interesting to consider if your object is not symetric or if the wind doesn't hit you straight on... think about rotational forces. (there is a whole other can of worms about fluid dynamics here, but we will stop at the mention of it).

2.c Wind resistance (sometimes also thought of as friction), but I think of it as the air that opposes movement of any kind, kinda like a fake fricional force in the air, maybe thought of as a form of kinetic friction (choose a low coefficient of friction when you try to model this).
When you calculate wind, you will be able to add up 2.a & 2.c before applying the "co-efficient" to calculate the force. When you think about it... driving down the road at 30mph is the same on your little antena pole flag as say... sitting still with a 30mph winds blowing against you.

If you still don't see the importance of a "normal force" from the air resisting your cloth's motion... then think about a skateboarder standing on a slanted ramp... somebody holds him there & the lets him go, now gravity is the ONLY external force pulling him STRAIGHT DOWN to the center of the earth... so how come he rolls sideways down the ramp? ITS BECAUSE OF THE NORMAL FORCE FROM THE RAMP BROTHER : )

peace, good luck

##### Share on other sites
Mystery    136
Hi Duncan

Thanks for taking the time to come up with such detailed explanation. In your diagrams, I can see that the x and y components are indeed changing. But am I right to say that they are due to other forces like centripetal force in this case? Gravity seems to affect only the z-component of the velocity since it is only acting directly downward(taking the z-axis to represent the downward direction). And according to a very kind Anonymous Poster for his explanation, it seems that we need a consider the normal force as well.

##### Share on other sites
Yeah, sorry about the cruddy ASCII. Next time I'll fire up the GIMP and draw some real diagrams.

As far as gravity only acting upon the z component, you are correct. And you really shouldn't worry about centripedal acceleration right now. Just get the basics.

I will now attempt another scenario to describe this phenomenon:
Consider a plank, broom handle, whipping cane, etc. Now consider that you have one end of this long, straight, unbendable object in your hand, loosely. Let's just say that you are holding it with your forefinger and thumb.

There are basically two "masses" and one "spring". The masses are at the ends of the stick, and the spring connects the two masses, though it won't stretch, cause you've got a strong wooden pole.

So, you've got this long shaft of wood between your fingers, and the other end is in your other hand. You are holding this smooth, polished pool stick in both hands, held horizontally (flat).

You now drop one end of this stick. Since nothing is holding that end of the stick up, gravity begins to pull it down. Gravity is also acting on the other end of the stick, your hand, your whole body, your shoelaces, the remnants of the Big Bang, Saturn's rings, and all the cookie crumbs of the world.

But since you are holding up the other end of the stick, it stays in place. Since you're holding that end of the stick lightly, it can rotate, allowing the now loose and falling end of the stick to rotate also.

So, gravity begins to pull down one end (the loose end). Because the "spring", the length of wood, can't stretch, the loose end has to go somewhere that is the same distance (length of staff) from the end in your hand, but still fall due to gravity's pull. How could this be done? Fall in an arc. It's like a clock pendulum in its motion. Falling in an arc allows it to stay the same length and fall due to gravity.

You can do the same thing with a pen at your computer.

That is how the y, z, x, w, q, o, p, and :) axes change. For less sarcasm and more content, just ask and I'll edit.