Verlet integration is a numerical method used to calculate the trajectory of particles in molecular dynamics. But it's widely used in computer graphics and software simulations to simulate physical phenomena and visual effects.
In each time step, it calculates the position of the particle in the next time step using current position, previous position and its accelaration. Then it applies constraints (if there's any) to maintain the right position of each particle.
We see a rope as a string of particles/nodes connected to each other with a fixed distance from each other. By running the Verlet integration for each node in the rope, we can simualte its physics with good accuracy.
We apply the constraint of nodes having the same distance from each other in each frame, and resolve collisions.
I also render a cylender-like mesh for the rope, which is generated procedurally in each timestep.
- Clone this repository.
- Open the project in Unity 2022.3.16 (Other versions are not tested but they might work).
- Open
SampleScene
scene in the scenes folder. - Press play. After pressing Play, the rope appears.
- Move the rope around in the scene view and see how it goes (I haven't implemented any control for it to use in the game view. Sorry :( )
In the Prefabs
folder you can find the Rope
prefab which has two components.
The first is the Verlet Rope
component. It has some fields:
- Rope Length: The length of the rope.
- Number of Nodes: How many nodes the rope consists of. the more the nodes are, the more accurate the simualtion is, but it will be computationally heavier.
- Constraint Iteration Count: How many times the constraints are applied in each timestep. The more the iterations, the stiffer the rope becomes.
- Gravity: The gravity vector.
- Rope Radius: The radius of the rope.
- Substeps: Each fixed delta time is divided into smaller time steps called substeps for better accuracy. This field sets the number of substeps.
The other component is the Rope Renderer
. It has only one field:
- Rope Segment Sides: The number of sides the rope will have when the 3D mesh is rendered.
You can read the detailed breakdown of this implementation here and you can read about how the rope is rendered here.