# Clothsim

Computer Graphics

This details my implementation of a real-time simulation of cloth using a mass and spring based system. This involved building data structures to discretely represent the cloth, which consisted of equally spaced point masses connected by springs. To simulate the way cloth moves over time, I calculate point mass positions every time step based on external forces as well as structural, shearing, and bending constraints. With these physical contraints, I compute the total force and use Verlet integration to calculate the new position for each point mass in the cloth data structure. I also implemented collisions with other objects (spheres and planes) as well as self-collisions to prevent cloth clipping.

## Masses and Springs

Here I built a grid of point masses and connecting springs. Springs can be of type structural, shearing, and bending. Below are some screenshots of scene/pinned2.json from a viewing angle where you can clearly see the cloth wireframe.

These images show the fireframe with all constraints: structural, shearing, and bending.

Shown below: what the wireframe looks like (1) without any shearing constriants, (2) with only shearing constriants, and (3) with all constraints.

## Simulation via Numerical Integration

Below I experiment with some parameters in the simulation, exploring the effects of changing the spring constant ks, and values for density, and damping.

### Spring Constant ks

The constant ks controls the stretchiness of the cloth material. For a very low ks, the cloth becomes stretched from start to rest as the point masses are pulled apart; for a high ks, the cloth does not give as much from start to rest, and the point masses become less likely to spread apart. The first image below (ks = 2500 N/m) shows the cloth collapses more in the center, especially compared to the final image (ks = 100000 N/m), which becomes tighter and certainly doesn't collapse as easily under its own weight. The following images are taken at the final resting state for various spring constant ks values.

ks = 1000 N/m (left), ks = 2500 N/m (right)

ks = 5000 N/m (left), ks = 10000 N/m (right)

### Density

As we increase density, the cloth gets pulled down more; the force of gravity exerted on the cloth is affected by its density. Below we can clearly observe point mass positions are affected by external forces and gravity, and that the cloth is pulled down more with higher density values. The following images are taken at the final resting state for various density values.

density = 5 g/cm^2 (left), density = 10 g/cm^2 (right)

density = 15 g/cm^2 (left), density = 30 g/cm^2 (right)

density = 50 g/cm^2 (left), density = 100 g/cm^2 (right)

### Damping

Damping controls the oscillation of the spring force. The smaller the coefficient, the more the cloth continues to oscillate and move. Higher damping constants result in energy dissipating faster, causing less movement over time, as the damping force brings the system to a resting state. Below, we can observe that with no damping, the cloth continues to move, seemingly without resistance, with many ripples across the material. As the damping increases, the springs oscillate less, and the material comes to a final resting state sooner, with noticeably less ripples.