- Inequality constraints are constraints of the form . The most common (only?) application for inequality constraints is resolving collisions and resting contacts.
- The derivation and mathematics are nearly identical to equality constraints actually. The only difference, as you will see, is that only non-negative are allowed. But first things first...
- The constraint equation is , where delta is penetration depth - the distance of one body's penetrating vertex to the penetrated surface of body B. More about how this is expressed in terms of body A and body B gemoetry in the next section. means the bodies collide. One therefore requires
- The reason this is an inequality constraint is that an equality constraint would not allow separation. Example of a book lying on a table:
- if only gravity force acts, the constraint impulse must counteract the predicted downward velocity
- if there is an upward force on the book (or other impulses applied to it) exactly counteracting gravity, the constraint impulse must be 0
- if the upward force is higher than gravity, the constraint impulse must still be 0 - i.e., not keep the book from separating - which illustrated that there is an inequality in the equation.

- In the simulation, one proceeds until a collision occurs (detected by the collision detection engine). A collision implies . The constraint solver aims to resolve the collision by requiring , computing the required corrective impulse, and change the velocities.
- In research papers, one often finds this problem being formulated as a
*Linear Complementarity Problem*, a special case of a quadratic programming problem. From a didactic point of view, I think it makes more sense to show directly how collisions are resolved and inequality constraints are handled because the algorithm makes sense intuitively, then show up the parallels to LCPs.

- the following uses the cross product in 2D instead of time derivatives of 2D rotation matrices, in two variants:
- Definition for a scalar : . It can be derived from the 3D cross product by taking the angular velocity equations and assuming rotation in the x-y plane: and neglecting the z direction again
- Definition for two vectors:

- the constraint equation is (the projection onto the outward facing is needed to give the constraint a direction, as opposed to the joint constraint where we simply used the squared length of )
- if collision detected (), then ensure . So as above, the time derivative is computed.
- For the following, I'll use an approximation I found in Erin Catto's slide that works pretty well - to simplify the formulas, is assumed to be time-invariant (i.e. we treat it as a constant during differentiation). Then we arrive at
- The bias term is again .
- If one wants "bouncy" collisions, the constraint for the post-impulse velocity does not require a zero normal velocity, but a fraction of the pre-impulse normal velocity , i.e. , so the bias term becomes .
- as described in the previous section, the update step consists of , with , but to fulfill the non-penetration constraint, once clamps the total . The reason is, simply scales the outward-facing (repulsive) velocity impulse, and our constraint was to no allow attractive constraint impulses, i.e. negative . A bit more about this in the next section. There are several iterations, a different being computed in each; what is clamped is not the individual but the sum of all previous and this, the
*accumulated*. - The graphic below shows two cases (upper and lower row) of a collision, the resulting impulses, and how they affect the bodies' linear and angular velocities. Note the ramp has infinite mass so the impulse does not change its velocity or position.

- The next section briefly explains the relationship of this straightforward algorithm to the more formal, underlying mathematic problem, the
*Linear Complementarity Problem*. You will likely encounter this and related terms rather often.

- The above procedure is, in academical papers, cast into a mathematical formulation known as the
*Linear Complementarity Problem*(LCP) - definition taken from wikipedia:- Given a real matrix and vector , the linear complementarity problem seeks vectors and which satisfy the following constraints:
- (that is, each component of these two vectors is non-negative)
- for all i. (The complementarity condition)

- Given a real matrix and vector , the linear complementarity problem seeks vectors and which satisfy the following constraints:

- I'll now show how the derivation and procedure is actually exactly such an LCP:
- Solve subject to two inequality constraints and a complementarity condition:

- The only difference really is that our () and () are not a vector and a matrix, respectively, but a scalar and a vector - but that's only because we look at an isolated constraint. If all constraints are combined into one big equation, would be a matrix, and we would have a vector of constraints
- I think it helps intuition a lot if one understands why the above inequality constraints and the complementarity condition are fulfilled by the simple algorithm introduced for collisions:
- - We computed by aiming for ; if turned out to be negative, that was because the bodies would otherwise separate (), in which case we set to let them separate ()
- - since and we clamp , this is always true (the Jacobian points into the correct direction - away from each body).
- - this one is interesting. It says OR - this is ensured by how is computed: the above formula for is derived with the aim of guaranteeing .
- If bodies would penetrate, is ensured by a positive , so
- If bodies separate (because of other forces or impulses), the above computation gives a negative , which is then clamped to , i.e. and thus

mass: | |

x0: | |

y0: | |

theta0: | |

vx0: | |

vy0: | |

vAng0: | |

tStop: |

t=0s

domino spacing: | |

vAng0: | |

tStop: |

t=0s

tStop: |

t=0s