Unity WebGL Player | Joints and Spring
This project simulates real world cloth behavior in a virtual environment. In this simulation, we will treat the cloth as system of particles interconnected with spring-dampers. Each spring-damper connects two particles, and generates a force based on their positions and velocities. Each particle is also influenced by the force of gravity. With those three simple forces (gravity, spring and damping), we form the foundation of the cloth system. We then add some fancier forces such as aerodynamics.
The particles are made up of various variables: position, velocity, acceleration, mass, momentum and force. Acceleration is calculated by dividing one by the mass then multiplying that result with the force value. This acceleration calculation is known as Newton’s Second Law (f=ma).
In order for the cloth simulation to work, we need to do two things: 1) Compute forces. For each particle we need to apply gravity. For each spring-damper we compute and apply forces. For each triangle we compute and apply aerodynamic forces. 2) Integrate Motion. For each particle we apply forward Euler integration.
The basic spring-damper connects two particles and has three constants defining its behavior: Spring Constant which is labeled as ks, Damping Factor which is labeled as kd and Rest Length which is labeled as lo. The basic linear spring force in one dimension is: fspring = -ksx = -ks(lo - l). The linear damping force is: fdamp = -kdv = -kd(v1 - v2). We can define a spring-damper by just adding the two: fsd = -ks(lo - l) - kd(v1 - v2). To compute the forces in 3D: Turn 3D distances and velocities into 1D, compute spring force in 1D then turn 1D force back into 3D force. To find the spring-damper force, we start by computing the unit length vector e from r1 to r2. We can compute the distance l between the two points in the process. Next we find the 1D velocities. Then we can find the 1D force and map it back into 3D.
In order to compute the aerodynamic forces, we need surfaces to apply it to. We will add some triangles to our cloth definition, where each triangle connects three particles. We will need to find the velocity, normal and area of the triangle to compute this force. For the velocity of the triangle, we can use the average of the three particle velocities. We actually want the relative velocity, so we will then subtract off the velocity of the air. The final aerodynamic force is assumed to apply to the entire triangle. We can turn this into a force on each particle by simply dividing by 3, and splitting the total force between them.
This project uses the left and right mouse buttons during runtime.