The next equality constraint we will derive is the line constraint. A line constraint is like a prismatic constraint (which will most likely be the next post) except allows rotation about the anchor point. A prismatic constraint constraints the linear motion of the bodies along a line. An example of a prismatic joint might be a roller coaster on the track. The cars cannot translate or rotate except along the track. For simplicity the prismatic constraint we will define is only for straight lines.
- Problem Definition
- Process Overview
- Position Constraint
- The Derivative
- Isolate The Velocities
- Compute The K Matrix
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 Line constraint.
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.
So the first step is to write out an equation that describes the constraint. A Line Joint should allow the two bodies to only translate along a given line, but should allow them to rotate about an anchor point. In other words:
If we examine the equation we can see that this will allow us to constraint the linear motion. This equation states that any motion that is not along the vector u is invalid, because the tangent of that motion projected onto (via the dot product) the u vector will no longer yield 0.
The initial vector u will be supplied in the construction of the constraint. From u, we will obtain the tangent of u, the t vector. Each simulation step we will recompute u from the anchor points and use it along with the saved t vector to determine if the constraint has been violated.
Notice that this does not constrain the rotation of the bodies about the anchor point however. To also constrain the rotation about the anchor point use a prismatic joint.
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).
By the chain rule:
Where the derivative of u:
The derivative of a fixed length vector under a rotation frame is the cross product of the angular velocity with that fixed length vector.
And the derivative of t:
Here is one tricky part about this derivation. We know that t, like r in the u derivation, is a fixed length vector under a rotation frame. A vector can only be fixed in one coordinate frame, therefore you must choose one: a or b. I chose b, but either way, as long as the K matrix and b vector derivations are correct it will still solve the constraint.
Substituting these back into the equation creates:
Now we need to distribute, and on the last term I’m going to use the property that dot products are commutative:
Now we need to group like terms, but the terms are jumbled. We can use the identity:
and the property that dot products are commutative to obtain:
Now we can use the property that the cross product is anti-commutative on the last term to obtain the following. Then we group by like terms:
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.
All the velocity terms are already ready to be isolated, by employing some matrix math we can obtain:
By inspection the Jacobian is:
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:
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.