The Distance Constraint | XPBD

XPBD uses particle constraints to ensure that particles move in a certain manner, relative to other particles. Let's take a deeper look into a common constrant that we'll find in a cloth simulation, the distance constraint.

Keywords: position based dynamics, cloth simulation, lagrange, physics, computer graphics

By Carmen Cincotti Β 

A big piece of the XPBD puzzle, which we already talked about a few weeks ago, is the satisfaction of particle constraints. Particle constraints force a particle to move in a certain way, relative to the other particles in the system.

How a particle moves back to the string

If you’ve been following me for the past few months, you most likely know of my goal to simulate fabric 🧡. Such a simulation may have complex constraints but for now we will only consider distance constraints and bending constraints.

Here are those famous articles that we already studied together during an article a few weeks ago on XPBD that I will reference for the math during this post:

A summary of XPBD

Recall that the XPBD algorithm with small steps is defined as follows:

Algorithm of XPBD with small steps

⚠️ As I said, we have already covered this algorithm thoroughly, and I recommend that you read the article if you are already lost.

For now, we are only interested in the loop that is in lines (6) - (11). As you can see, the function of this loop is to satisfy the constraints of the simulation in order to directly correct the positions of each particle that participates in the constraints.

We just covered the reason why this simulation method is called as Position Based Dynamics - PBD πŸŽ‰.

Satisfying the constraints

Our objective during the loop is to calculate the correction vector that a particle must move in order to satisfy, or come closer to satisfying, the constraints.

Ξ”x=ΔλMβˆ’1βˆ‡CxT\Delta \mathbf{x} = \Delta \lambda \mathbf{M}^{-1} \nabla C \mathbf{x}^T

where Δλ\Delta \lambda is defined as:

Δλ=βˆ’C(x)βˆ‡C(x)Mβˆ’1βˆ‡C(x)T+Ξ±~\Delta \lambda = \frac{-C(\mathbf{x})}{\nabla C(\mathbf{x}) \mathbf{M}^{-1} \nabla C(\mathbf{x})^T + \tilde{\alpha}}

Note that Ξ±~=Ξ±Ξ”t2\tilde{\alpha} = \frac{\alpha}{\Delta t^2} where Ξ±\alpha is the constraint compliance. Recall that Ξ±=1k\alpha = \frac{1}{k} where kk is the stiffness of the constraint. C(x)C(\mathbf{x}) is the constraint equation, βˆ‡C(x)\nabla C(\mathbf{x}) is the gradient of βˆ‡C(x)\nabla C(\mathbf{x}) and is therefore a vector. Mβˆ’1\mathbf{M}^{-1} is a diagonal matrix of inverse masses of the particles participating in the constraint.

Δλ\Delta \lambda, C(x)C(\mathbf{x}), and Ξ±~\tilde{\alpha} are scalar values.

Using these equations, we will simulate some constraints with code! πŸ–₯️

The distance constraint

To start, we will talk about the distance constraint because it is rather easy to understand. The objective is to satisfy the constraint by moving two particles so that they approach each other at the rest length, dd:

Distance constraint

where pp is the position of a particle and mm is the mass.

We can solve the equations from the last section in order to find the final position corrections, Ξ”xn\Delta \mathbf{x}_n :

C(x1,x2)=∣x1,2βˆ£βˆ’dn=x1,2∣x1,2βˆ£Ξ”x1C(x1,x2)=nΞ”x2C(x1,x2)=βˆ’nβˆ΄Ξ”x1=βˆ’w1w1+w2(∣x1,2βˆ£βˆ’d)nβˆ΄Ξ”x2=+w2w1+w2(∣x1,2βˆ£βˆ’d)n C(\mathbf{x_1}, \mathbf{x_2}) = |\mathbf{x}_{1,2}| - d\\[6pt] \mathbf{n} = \frac{\mathbf{x}_{1,2}}{|\mathbf{x}_{1,2}|}\\[6pt] \Delta_{\mathbf{x_1}}C(\mathbf{x_1}, \mathbf{x_2}) = \mathbf{n}\\[6pt] \Delta_{\mathbf{x_2}}C(\mathbf{x_1}, \mathbf{x_2}) = -\mathbf{n}\\[6pt] \therefore \Delta \mathbf{x_1} = - \frac{w_1}{w_1 + w_2}(|\mathbf{x}_{1,2}| - d) \mathbf{n}\\[6pt] \therefore \Delta \mathbf{x_2} = + \frac{w_2}{w_1 + w_2}(|\mathbf{x}_{1,2}| - d) \mathbf{n}\\[6pt]

The code solution

Here is the code where I define two particles. The distanceConstraint function is based on calculating Δλ\Delta \lambda and immediately updating the position, pp of the particle pnp_{n}. There is a distance constraint with rest length equal to 11.

You may notice that there is a little trick during the calculation. Recall that the calculation to find Δλ\Delta \lambda is:

Δλ=βˆ’C(x)βˆ‡C(x)Mβˆ’1βˆ‡C(x)T+Ξ±~\Delta \lambda = \frac{-C(\mathbf{x})}{\nabla C(\mathbf{x}) \mathbf{M}^{-1} \nabla C(\mathbf{x})^T + \tilde{\alpha}}

Fortunately, βˆ‡C(x)\nabla C(\mathbf{x}) is a normalized vector. So:

βˆ‡C(x)Tβˆ‡C(x)=1\nabla C(\mathbf{x})^T\nabla C(\mathbf{x}) = 1

We can therefore simplify our equation as follows:

Δλ=βˆ’C(x)Mβˆ’1+Ξ±~\Delta \lambda = \frac{-C(\mathbf{x})}{\mathbf{M}^{-1} + \tilde{\alpha}}

Finally, I used a logging function to see how the particles move before and after the simulation loop:

BEFORE APPLYING XPBD LOOP ------------------------- POSITION P1: [ '2.0000', '2.0000', '0.0000' ] POSITION P2: [ '-2.0000', '-2.0000', '0.0000' ] AFTER APPLYING XPBD LOOP ------------------------ POSITION P1: [ '0.3536', '0.3536', '0.0000' ] POSITION P2: [ '-0.3536', '-0.3536', '0.0000' ]

We see that the two particles approach each other and move to the points p1=(.3536,.3536,0),p2=(βˆ’.3536,βˆ’.3536,0)p_1 = (.3536, .3536, 0), p_2 = (-.3536, -.3536, 0). Do these new points satisfy the distance constraint? Recall that the length of rest is equal to 11:

.35362+.35362+.35362+.35362β‰ˆ1\sqrt{.3536^2 + .3536^2} + \sqrt{.3536^2 + .3536^2} \\[6pt] \approx 1

Yes! The distance constraint has been satisfied!

Next time

We will see together the isometric bending constraint. Stay tuned!


Comments for The Distance Constraint | XPBD

Written by Carmen Cincotti, computer graphics enthusiast, language learner, and improv actor currently living in San Francisco, CA. Β Follow @CarmenCincotti


Interested in contributing to Carmen's Graphics Blog? Click here for details!