## 3 Method
### Position-Based Dynamics Progression
```
1:Initialize cloth mesh, rest positions, and parameters
2:For each time step:
3:AdaptiveInitialization()
4://VBD core loop
5:For iteration = 1 to maxIterations:
6:For each color in vertexColors:
7:For each vertex v with current color:
8:If v is not pinned:
9:constraint_energy = ComputeConstraintEnergy(v)
10:hessian = ComputeLocalHessian(v)
11:deltaX = Solve(hessian, force)
12:v.position += deltaX
13:HandleExternalCollisions()
14:HandleSelfCollisions()
15:ApplyVelocityUpdate()
16:Function AdaptiveInitialization():
17:For each vertex v: 25 Estimate acceleration
18:v.predicted_position = v.position + timeStep v.velocity + estimated_acceleration timeStep^2 / 2 The algorithm for cloth simulation initializes with a mesh structure, where each vertex has a set position, velocity, and connectivity to neighboring vertices, forming the cloth structure. The simulation process iterates through time steps, with each step composed of sequential stages that ensure realistic cloth behavior. These stages are as follows:  
1. Mesh Initialization: The algorithm initializes the cloth mesh, setting up the initial positions of each vertex and defining necessary parameters, such as stiffness and damping factors. 2. Adaptive Initialization: At each time step, the algorithm starts with Adaptive Initialization, where it estimates the acceleration of each vertex, and predicts its next position. This is calculated by integrating the velocity and the estimated acceleration over the time step, with the following formula: v.predicted_position = v.position + timeStep \(\times\) v.velocity + estimated_acceleration \(\times\) timeStep\({}^{2}/2\)
3. Iterative Optimization: The algorithm then enters the **core iterative loop**, which runs for a specified maximum number of iterations. Within each iteration, the vertices are processed based on their assigned color group, where vertices in each color can be optimized independently. This vertex-coloring technique groups vertices to allow for parallel processing. For each vertex v in the current color group: A. Compute Constraint Energy: The algorithm calculates the constraint energy for the vertex, which includes both the inertial energy and the spring energy relative to neighboring vertices. The spring energy helps maintain the original distances between connected vertices, preventing excessive stretching or compression. B. Compute Local Hessian Matrix: The local Hessian matrix is then calculated to capture the local deformable of the vertex. This matrix helps in solving for the position update by approximating the local stiffness of the vertex. C. Solve for Position Update: Using the Hessian and the energy gradient, a linear system is solved to compute the position variation \(\Delta\)x, which is then applied to the position of the vertex. * Velocity and Damping Update: After all iterations, the algorithm updates the velocity of each vertex based on the changes in position, applying a damping factor to simulate energy loss. This damping allows for the cloth to settle into a resting state, preventing perpetual motion. * Collision Handling: After updating the positions within each iteration, collision handling takes place:
* External Collisions: For each vertex, collisions with external objects are detected and resolved by repositioning the vertex onto the collision surface, adjusting its velocity accordingly. * Self-Collisions: For internal cloth collisions, the algorithm checks the distance between vertices. If two vertices come closer than a set collision threshold, a repulsive force is applied to separate them. Edge-to-edge collisions are also managed using Continuous Collision Detection (CCD) to resolve intersections between edges within the cloth. These steps in the VBD algorithm in Unity ensure a balance of physical accuracy and computational efficiency, making it suitable for real-time cloth simulations that handle complex interactions while preserving the structural integrity of the cloth.