Introduction
When simulating a particle movement, we naturally think of Euler integration, where the position of the particle can be expressed as such: (free falling motion)
This is an extremely easy use case. The position of the particle can be represented using a function of time. But in real life, a particle can be affected by multiple forces (and even unstabled force caused non-constant acceleration) and Euler integration also has inaccurate estimation when time step is very large.
In my understanding, the use of Verlet is good for complex particle movement. What it essentially does is relate force with position rather than velocity. The process first calculate the current particle position and the position one step back:
This works magically, here’s some sample code to help understand it, and a Demo.
1 | // without other influence, the following updates the particle position |
Calculate Force
What if we want to add gravity to the particle like the free falling above? Just add the acceleration to the current position, and the change will add-up on the next integration. The following also applies to forces like friction.
1 | posNow += GRAVITY; // not sure if we should multipy Time.deltatime |
Constraint
Constraint are usually in the form of constraining particles to a specific distance, like spring. When mulitple constraints acts on single particle, one constraint will affect the other. To solve this, loop the constraint multiple time for each time step.
1 | for (int count = 0; count < ITERATION; count++){ |
Collision
Penalty based system or projection collision reaction
1 | // example for bounding box collision |
Resources
Gamedev.net - A Verlet based approach for 2D game physics
Youtube - Coding Math: Episode 36 - Verlet Integration Part I
Youtube - 2D Rope Tutorial - Make Swinging Rope in Unity (Verlet Integration)