viernes, septiembre 26, 2008

Cloth Physics

I have been working in something very interesting, that our 3D engine (Unity) doesnt come with, and its cloth physics, who would think that something as simple as cloth would be so difficult to simulate in a PC!.

In order to start correctly, I would like to point out that there are several methods for cloth simulation, but the one I selected for my simulation was the mass spring model system =).

In this model, we treat the cloth itself as a grid of nodes at which all the mass of the cloth is assumed to be concentrated, and then we let the nodes interact with each other.

This particular method used in this simulation connects the grid with a series of linear springs designed to resist forces pulling the fabric apart.

This mass spring model uses three types of springs in order to mantain the cloth shape.

Structural Springs (Blue): These springs connect each node with its 4 adjacent non-diagonal neighbors, and serve to keep the cloth in a "sheet".

Shear Springs(Black): These springs connect each node with the four adjacent diagonal neighbors, and oppose shearing deformations.

Flexion Springs (Green): The flexion springs connect each node with the node two over orizontally and diagonally. These flexion springs have little impact unless the points are non-coplanar, in wich case the flexion springs serve to restrict the bending of the sheet.

These springs are used to set up a system of differential equations to solve the position of the nodes. At any time t, the forces applied to each node can be calculated from the spring forces and external forces like gravity, wind, balls hitting, etc.

The position can be derived through a simple Euler method integration:

\longrightarrow a ( t + dt) = \frac{1}{\gamma} * F(t)

\longrightarrow v ( t + dt) = v(t) + dt * a(t + dt)

\longrightarrow p(t+dt) = p(t) + dt * v(t+dt)


* miu: mass of the particles.
* dt : (discrete) time step. (a too large time step will blow up! the simulation).

In Unity, I used a set of Empty Game Objecs in order to simulate the nodes with the masses, added the springs with a script, wich you can play with the spring force values, and made all the Game Objects as rigid bodies for the interaction with the gravity.

Here's a demo of what I came up with: (Sadly the player only works for Mac OS or Windows, no linux support T_T)

* press Esc to reset the scene.

* Click on the scene and move the mouse to move the cloth.

For Cloth with No colliders in the nodes:

And for Cloth with colliders in the nodes: (the rotations of the nodes are frozen, that's why we see the cloth different from the abobe one)

No hay comentarios.: