The following text is intented for those who decided to code their own LCP solver of resting contacts in rigid body simulations and who would appreciate having a brief extract of all the important information. *To newcomers*: without reading some of the references below the following won't make much sense.

**Notation.** **p**_{a} is an arbitrary point on body A in world space, **x**_{a} is the center of mass of body A in world space, **r**_{a} denotes **p**_{a} - **x**_{a}, **p**_{a} can be expressed as **R**_{a}**p**_{0} + **x**_{a} where **p**_{0} is an arbitrary point on body A in body space and **R**_{a} is the body's 3x3 rotation matrix. **n**_{i} is normal at i-th contact and by convention it always points towards body A (first body reference in the contact data structure). All state variables are functions of time - i.e. **R**_{a} is actually **R**_{a}(t) - but this is omitted for the sake of concise writing and better understanding.

**1 Acceleration-based LCP formulation for contact solving.** See [1] for in-depth explanation. Relative acceleration at each contact point is expressed as:

(1) |

and we want to find contact forces that meet the following constraints:

(2) | |

(3) | |

(4) |

Because **p**_{a} = **p**_{b} at time t_{0}, the relative acceleration at i-th contact is:

(5) |

All forces acting on either body in the i-th contact can contribute to the relative acceleration and must be taken into account when *a*_{i1}..*a*_{in} are being solved for. *b*_{i} can be computed directly from velocities and external forces/torques acting on the bodies in given contact. The linear and angular contributions from *f*_{j} to the acceleration of body A at i-th contact point are:

The same can be written for body B. These four results combined and multiplied by *f*_{j} give the acceleration contribution resulting from the force acting at j-th contact point. The yet unknown *f*_{j} is thus multiplied by:

The parts of (1) unaffected by contact forces are kept in *b*_{i}. Its linear and angular components for body A can be derived to be:

The same can be written for body B. All four components combined along with the remaining part of (5) gives us *b*_{i} in the following way:

The only remaing part is the time derivative of contact normal. When the normal belongs to a face, the negative derivative is its cross product with the body's angular velocity, when the collision is edge-edge it's a little bit trickier and it involves a time derivative of a cross product of normalized edge directions. The derivation is in [3], with the following as the result (**e**_{a} and **e**_{b} are edge direction vectors):

In vector form, we can write:

(6) |

and together with conditions (2), (3) and (4), we're looking for an equivalent LCP formulation. As shown in [2] they can be easily transformed into:

, | and | (7) |

which is what is known as a linear complementarity problem (LCP). As **A** is symmetric and positive semidefinite (PSD) the solution always exists. It is not so in systems where static and dynamic friction forces are taken into account.

**2 Velocity and impulse-based LCP formulation for contact solving.** Let function **C**(**x**): **R**^{n} -> **R**^{m} describe *m* constraints placed on the *n* dimensional system. If **C** = **0** was satisfied at the previous time-step it is enough to have **C**' = **Jx**' = **0** in order to ensure that the constraints will be satisfied in the current time-step. To make **Jx**' equal to **0** we must add an unknown constraint impulse **j**, so the equation becomes:

(8) |

**J** is the Jacobian (a matrix of gradients of each of *m* constraint functions), **M**^{-1} is a mass matrix that comprises the change in linear and angular velocity due to an impulse. If we want to model the restitution law, i.e., have the impact resolution as part of the LCP, we can assign the right hand side of the equation a vector **c**. This vector can also be used in some error reduction scheme that will probably be inevitable to implement anyway. So equation (8) becomes:

(9) |

The problem with (9) is that we have *m* equations and only *n* variables. Because **j** must not bring energy to the system we may assume that **j** = **J**^{T}**λ**h, where **λ** is an unknown *m*-dimensional vector of *Lagrange multipliers* and *h* is the integration step. The current velocity has two elements: linear and angular velocity resulting from body's kinetic energy and velocity resulting from acceleration due to external forces. Also here the integration step plays its role and the rewritten equation looks like this:

Rearranging it we get:

(10) |

Equation (10) is valid under the assumption that we'll use Euler integrator for evolving the dynamic and kinematic variables of the system. That can be disadvantage to acceleration-based LCP formulations, such as (6), because by using (10) we can't trade speed for accuracy by plugging in more precise integration module. The equation (10) can be written as:

and conditions (2), (3), and (4) can be written the same way in terms of **λ** and **b**. If we have a mixed LCP solver available, we can have the conditions (2-4) valid only for certain rows of **A** and the remaining rows can describe bilateral constraints such as joints with a simpler set of conditions (equality, unbounded **λ**, zero **c**, etc.). Thus we'll have the whole simulation system with many types of constraints evolved by a single call to the solver.

**References:**

[1] Baraff, D., An Introduction to Physically Based Modeling: Rigid Body Simulation II - Nonpenetration Constraints

[2] Baraff, D., Fact Contact Force Computation for Nonpenetrating Rigid Bodies

[3] Eberly, D., Game Physics, ISBN 1-55860-750-4

© 2002-4 Marek Trefny