The next equality constraint we will derive is the distance constraint. A distance constraint can be used to join two bodies at a fixed distance. It can also be used as a spring between two bodies.

- Problem Definition
- Process Overview
- Position Constraint
- The Derivative
- Isolate The Velocities
- Compute The K Matrix

Problem Definition

It’s probably good to start with a good definition of what we are trying to accomplish.

We want to take two or more bodies and constrain their motion in some way. For instance, say we want two bodies to only be able to rotate about a common point (Revolute Joint). The most common application are constraints between pairs of bodies. Because we have constrained the motion of the bodies, we must find the correct velocities, so that constraints are satisfied otherwise the integrator would allow the bodies to move forward along their current paths. To do this we need to create equations that allow us to solve for the velocities.

What follows is the derivation of the equations needed to solve for a Distance constraint.

Process Overview

Let’s review the process:

- Create a position constraint equation.
- Perform the derivative with respect to time to obtain the velocity constraint.
- Isolate the velocity.

Using these steps we can ensure that we get the correct velocity constraint. After isolating the velocity we inspect the equation to find J, the Jacobian.

Most constraint solvers today solve on the velocity level. Earlier work solved on the acceleration level.

Once the Jacobian is found we use that to compute the K matrix. The K matrix is the A in the Ax = b general form equation.

Position Constraint

So the first step is to write out an equation that describes the constraint. A Distance Joint should allow the two bodies to move and rotate freely, but should keep them at a certain distance from one another. In other words:

which says that the current distance, and the desired distance, must be equal to zero.

The Derivative

The next step after defining the position constraint is to perform the derivative with respect to time. This will yield us the velocity constraint.

The velocity constraint can be found/identified directly, however its encouraged that a position constraint be created first and a derivative be performed to ensure that the velocity constraint is correct.

Another reason to write out the position constraint is because it can be useful during whats called the position correction step; the step to correct position errors (drift).

First if we write out how is computed we obtain:

Where and are the anchor points on the respective bodies.

We can rewrite this equation changing the magnitude to:

Since the squared magnitude of a vector is the dot product of the vector and itself.

Now we perform the derivative:

First let:

so

by the chain rule:

by the chain rule again:

Since the dot product is cumulative and distributive over addition:

If we clean up a bit:

Now we know that:

The derivative of a fixed length vector under a rotation frame is the cross product of the angular velocity with that fixed length vector.

So

We can also let:

Giving us:

Isolate The Velocities

The next step involves isolating the velocities and identifying the Jacobian. This may be confusing at first because there are two velocity variables. In fact, there are actually four, the linear and angular velocities of both bodies. To isolate the velocities we will need to employ some identities and matrix math.

The linear velocities are already isolated so we can ignore those for now. The angular velocities on the other hand have a pesky cross product. In 3D, we can use the identity that a cross product of two vectors is the same as the multiplication by a skew symmetric matrix and the other vector; see here. For 2D, we can do something similar by examining the cross product itself:

Remember that the angular velocity in 2D is a scalar.

Removing the cross products using the process above yields:

Now if we employ some matrix multiplication we can separate the velocities from the known coefficients:

Now, by inspection, we obtain the Jacobian:

Compute The K Matrix

Lastly, to solve the constraint we need to compute the values for A (I use the name K) and b:

See the “Equality Constraints” post for the derivation of the A matrix and b vector.

The b vector is fairly straight forward to compute. Therefore I’ll skip that and compute the K matrix symbolically:

Multiplying left to right the first two matrices we obtain:

Multiplying left to right again:

Multiplying left to right again:

Finally distributing the last term and pulling out scalar values we get:

Remember the inertia tensor in 2D is a scalar, therefore we can pull it out to the front of the multiplications.

Since n is normalized:

We get:

Then if we perform the matrix multiplication in the other terms:

We obtain (remember the cross product in 2D is a scalar):

Plug the values of the K matrix and b vector into your linear equation solver and you will get the impulse required to satisfy the constraint.

Note here that if you are using an iterative solver that the K matrix does not change over iterations and as such can be computed once each time step.

Another interesting thing to note is that the K matrix will always be a square matrix with a size equal to the number of degrees of freedom (DOF) removed. This is a good way to check that the derivation was performed correctly.

Hi William,

You have mentioned that the size of the K matrix will be equal to the no. of degrees of freedom removed.

This makes the ‘K’ matrix in the distance constraint a 1×1(a scalar).

And with ‘J’ having a ‘nT’ (transpose of n) term before the 3×12 matrix, that makes ‘b’ a scalar too.

Does this mean ‘x’ (lambda) will be a scalar too? And if so, how will this lambda be used to calculate the impulses to be applied on both the bodies?

Yes, you are exactly right, lambda will be a scalar in this case. Once lambda (the scalar impulse) is found, we can use the equation from the Equality Constraints post to find the change in velocity:

Now if we plug in the mass matrix, J

^{T}and lambda we get 4 equations to find the change in velocity for the two bodies (note in 2D the mass matrix entries are scalars and as you pointed out lambda is a scalar):As a side note, the above explains how to apply the impulse to the bodies. The impulse is simply (in this case):

William

Hi William,

Are Vf and Vi velocities about the pivot or about the COM?

If they are about the pivot, shouldn’t Vf be 0 after every frame(for Pin Constraint of a body with the world[very heavy static object])?

Thanks

v

_{f}and v_{i}are the linear velocities of the bodies at their center of mass.The velocity of the pivot point would be:

This is seen in the derivation of the distance constraint. However, the formulation we have setup here is to find the velocities at the center of mass for the bodies.

William

Hi William,

Thanks for the replies.

I am getting the right responses from the bodies when i’ve implemented the distance constraint, but there seems to be a constant drift arising out of the numerical integration errors. I’ve noticed this error for the point-to-point constraint also.

How do we solve/cancel out these errors and ensure close-to-perfect behavior with the constraints?

Thanks in advance.

Exactly. In the Equality Constraints post I briefly touch on the drift problem. Basically as you pointed out, the velocity constraints can easily be satisfied, however, because our integration techniques are approximations, we will always get drift in the position constraints. The generally accepted solution to this problem is to use some sort of post stabilization method.

Unfortunately I don’t have a post for this yet but I can offer a few details. If we take our original position constraint that we defined:

and we solve this for C we get the position constraint error. We can feed this error back into the velocity constraint by moving/rotating the bodies by this error (using the effective mass to apply the position correction appropriately):

Then we can apply this position correction by:

This will give the change in position of the Center of Mass and the change in Rotation of both bodies. Because we changed the position and rotation, upon the next iteration of the simulation, the velocity constraint will see these changes (that’s why we say “the position error gets feed back into the velocity constraint”).

Naturally, each constraint will have a different position constraint equation. The one above is specifically for the Distance Constraint. But they all follow the same process, solve the position constraint for C (which could be a vector instead of a scalar for some constraints) and then scale this by the “effective mass” (the K matrix). Then apply the result to the bodies.

William

You say it can be used to model a spring – how?