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.

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:

⚠️ 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.

where $\Delta \lambda$ is defined as:

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

$\Delta \lambda$, $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, $d$:

where $p$ is the position of a particle and $m$ is the mass.

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

### 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, $p$ of the particle $p_{n}$. There is a distance constraint with **rest length** equal to $1$.

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

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

We can therefore simplify our equation as follows:

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 $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 $1$:

Yes! The distance constraint has been satisfied!

## Next time

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