When simulating any kinds of physical systems in graphics we often have to deal with contact. Contact is the interaction between two objects that are in close proximity. The forces between these objects are called contact forces and they are there to prevent the objects from interpenetrating. There are 3 rough types of contact simulations: 1. constraint based methods, 2. penalty based methods, and 3. impulse based methods. We will focus on the first two.
Basics of Contact Simulation
Newton-Euler equations of motions that govern dynamics of physical systems as a 2nd order ODE is written as:
where is the mass matrix over time, is the velocity of the system, is the generalized position of the system, and is the external forces acting on the system. The contact forces are a subset of these external forces. We can abbreviate the above equation as:
and we evaluate it using numerical integrators that take discrete steps in time. In this section we denote explicit quantities with superscript and implicit quantities with . In the implict setting the 1st order Taylor approximation gives:
which when placed into the Newton-Euler equation gives gives the following Euler update:
Here can be seen as the momentum term and is the velocity determined at the end of the timestep. The form of will depend on the type of representation we use for our body.
Now we can similiarly update the generalized positions:
where is the kinematic matrix that maps velocities to positions.
Since most contant models are based on Coulomb model, we get limited performance boost from higher order methods, so we just we assume planar surface at the contact points.
Basics of Constraints
To restrict movement of bodies to some desired state we introduce kinematic constraints, specifically with constraints functions we define the desired state as a manifold embeeded in .
Assuming constraints are initially satisfied, we can formulate constraints in terms of how fast they change with respect to :
We call the constraint Jacobian / gradient. It encodes the direction in which bodies may be pushed or pulled without exerting any forces on the system while staying on the manifold. With this the bilateral and unilateral constraints can be written as:
and respectively. More generally the constraint forces are written as:
where is the vector of Lagrange multipliers. The multipliers relate to the magnitudes of when the gradients / directions are normalized. So tells the force direction and tells the magnitude. The constraint forces can be thought of as the forces that keep the system on the manifold.
To actually enforce that our system stays on the manifold we need to add the constraint forces to the external forces acting on the system in the form of an impulse:
where the constraint forces are evaluated explicity. If we just consider bilateral constraint and write this as a linear system, then we can use Shur complement to reduce the system:
which is a linear system of the form
Non-interpenetration Contact
Unilateral constraints are only active when the bodies are in contact. We model the distnace between two bodies with a gap function —i.e. a constraint function that when the bodies are in contact, when bodes are separated, and when bodies are interpenetrating.
If we make the gross assumption that surfaces are smooth, then we can imagine a plane at the contact point and its normal vectors is parallel to surface normal. If the bodies are in contact, then an impulse is applied at the contact location in the direction of (i.e. perpendicular to the contact plane).
As such we have 2 cases:
No contact, No force. and .
Contact, Force applied. and .
Since they are independent we can write them as one “position level non-penetration complementary condition”:
Since the contact is only when we can again think about it in terms of velocities of bodies. In the implict setting, if bodies are in contact at time , then if —that is the normal component of relative velocity is positive—they will not be in contact at time (note ), so . If —that is they have negative or 0 relative velocity—then they will be in contact at time , so . Combined we have the condition:
We replace , since it is the relative velocity of the contact points in the normal direction. If we want to anticipate the collision and avoid tunneling effects we can replace with .
Non-interpretation Jacobian. The question now is how do we compute ? We can use the chain rule to get:
where body-space Jacobian of the gap function.
How do we construct ? Let’s consider collision of two bodies at contact point and centes of mass respectively. Defining to point , then by taking and we can write:
where are linear velocities of and are angular velocities of respectively. Finally we see how to compute :
To extend to multiple contacts, we would just place a new row for each contact point.
Soft-Bodies Non-interpenetration Constraints. This is very similar to the rigid body case we were considering. We usually use a mesh with nodes to represent a deforamble model.
Considering colliding tetrahedral elements of a mesh between we can formulate the contact point using Barycentric coordinates:
The relative contact point velocity in the normal direction is then:
Coulumnb Friction Model
So far we assume that tangential movement between contacts was allowed and not opposed. In graphics we often want to simulate friction as well. We can model friction as a force that opposes the relative tangential velocity of the contact points. We can model this as a force that is proportional to the normal force and the relative tangential velocity.
To describe this friction we think about a local contact frame for each contact point. The local frame is defined by the normal vector and two tangent vectors that are orthogonal to and each other; define the shared contact plane between the smooth surfaces.
can be defined from . Since we are discussing this in the local frame, we need to have relative velocity of the two surfaces
where is the world-space relative velocity. We rewrite the context frame basis with . Now recalling the equation for the relative world velocity we can write:
so
To refelct we recall that in the non-friction case we actually had so we indeed got the friction Jacobian by just changing the normal vector to the contact frame basis!
We defined how we can compute the contact frame relative velocity, but to determine the impulses that separate objects and the friction forces between the objects, we need to separate into normal and tangential components.
Theory of Friction. Since Column force couples the normal and tangential impulses, we restrict the tangential friction with a cone constraint. The cone constraint is defined by the friction coefficient and the normal force :
where . Each corresponds to some slice of the cone and that slice tells us all the possible tangential forces that can be applied given the normal force. The equation is quadratic so the cone is a quadratic cone.
We separate friction into 2 types: 1. slipping and 2. sticking. If the relative tangential velocity is less than the threshold, then the contact is sticking and the tangential friction force can have any direction. If the relative tangential velocity is greater than the threshold, then the contact is slipping and the tangential friction force is is opposing the movement and has magnitude . We can write this as:
The first condition is always active and tells us that the tangental impulse has to be in the quadratic cone. If then the other two conditions activate: first tells us that , i.e. the impulse must be maximized in the cone, the last condition says that the tangential friction impulse should oppose the relative tangential velocity. We call this a (non-linear) complementary problem.
To give more intuition on these conditions, we can think about the friciton force as maximally decreasing energy of a system. If we define to be the set of possible friction forces, then the principle of maximum dissipiation gives:
Following this setup we can arrive to the variational inequality (VI) and you can read more about it in [the notes].
LCP - Linear Complementary Problem
We saw that our friction model is non-linear and can be hard to solve efficiently. We can linearize the problem by creating a linear approximation of our friction cone. We call it a polyhedral cone and it is given by the span of unit vectors . Each tangent vector has a twin in the opposite direction, so we can write all friction forces with non-negative coefficients. This allows us to re-write the quadratic constraint above as:
To form the stick-slip conditions we only need to know whether we are slipping or not. For this we can measure the maximum slipping velocity along all of the tangential directions. Then for :
where is the vector of all ones and is the relative tangential velocity along all directions. is the maximum slipping velocity and can be 0 iff , i.e. we are sticking. Otherwise . We also sometimes call the slack variable.
Again by principle of maximum dissipation we know that:
To get the right direction of the friction force we let . Finally this gives a complete linear model for 1 contact point:
contact points. To generalize our system to multiple contact points we need to assemble a global Jacobian containing non-penetration and tangential friction constraints for all contact points. For the non-penetration conditions each contact point we have a separate row:
For the friction Jacobian (tangential friction) we have a similar setup for all tangential directions:
where is the Jacobian for the -th contact point. The global Jacobian is then:
subject to:
With rearamnging we can get the LCP form:
After all this gargol we see that it really boils down to a nice and simple linear system!
Boxed LCP
Another way to discretize the friction cone is by using only 2 orthogonal tangent directions and have them limited:
for
Note that to solve these discretizations we need more general algorithms than LCP solvers. One nice benefit we get from writing our system this way is that we can run away from the whole mess of having different constraints for friction and non-penetration and just use the same algebraic expression (with different bounds) for both! Concretely, we can write:
We can further decompose the residual velocity as and write BLCP as 3 separate LCPs:
To assmble the global Jacobian we recall the Jacobian for a single contact as:
where we now replace with . Then the global Jacobian becomes: and the global BLCP for the constrainted equations of motion becomes:
where We could again use Shur complement to reduce the system to a smaller one and you can see that in [the notes].
Numerical stabilization. Add springs, because we have numerical drift in linear approximation.
Deformable Contact
While most of the friction contact formulations above were derived from a rigid-body setting, we can extend them to deformable bodies too. The main differneces are that there are more variables and we use implicit discretization approaches in deformable objects.
If we first consider the contact between 2 rigid bodies, we can write the relative contact point velocity as:
Most often we pick to be the direction of sliding and to be orthogonal to . We can combine all contacts and bodes into one big system of the familiar form:
Finally we write the block matrix, the external forces , and the generalized coordinates . Since we incorporate quaternions we have to change using a kinematic matrix (refer to the notes for the exact form). Then and , as we have seen before.
To change this to a deformable body we need to add elastic forces. Recall that applying FEM on a deformable body results in a second order differential equation:
where is all the nodes in the mesh, is the stiffnes matrix, is the damping matrix, and is the contact forces. Observe that we do not have any quaternions or angualr spin present anymore. Using Euler integration and finite differences we get:
where . Integrating genearlized poisitions we get:
Note: no , because we are not dealing with quaternion rotations anymore. Now we can write a velocity level dynamic equation for this system:
or equivalently in the impulse form:
Here is sparse, block-symmetric, and positive definite. That’s every numerical analysit wet dream. We can now easily solve the system using Conjugate Gradients or any other iterative solver to get , then , and repeat.
Note: Deformable objects make numerics easier compared to rigid bodies.
Contact Generation
We talked about how to simulate contact, but we did not talk about how to generate it, based on the representation of the objects. We will focus on the most common representation: tetrahedral meshes. First we define some terms: the contact position / point will be denoted as w.r.t points on the surfaces of objects . At the exact time of contact they obey , but since we are doing discreitzation in time, this is not guaranteed to happen. That said, we will still apply forces at . We will define for simplicty the contact normal to be the normal of the surface at . Again, note that might not be equal to if the surfaces are non-smooth. We also introduce a measure of penetration (same convention as the gap function); it tells how much we have to move along to get to .
When dealing with mesh-based collisions, we recognize 2 types of collisions: 1. vertex-face and 2. edge-edge . Since we are timestepping, we should expect to the detect the collision when the objects are interpenetrating.
Continuous Collisoin Detection (CCD). Rather then trying to reason about when a collision will happen in our descretized setting, we can think about it in a continuous setting. CCD processes all the possible collisions that might occur from to (e.g. to ). To do so we can either look forward in time and look for the time when a collision will happen or look backward in time and find a time just before the collision happend. We can pefrorm CCD with a course-to-fine approach to speed it up.
To prevent collisions we can either set a large enough time-step based on CCD or we can warp points to the start of the timestep based on the computed normal.
To compute a and at the contact we will make the assumption that when , then motion between time-steps can be considered linear, while the error increases with . We can “sweep” a triangle over the time-step to get the “swept-volume.” We can then compute the time of collision by finding the intersection of the swept-volume with the other object. Using the forward prediction approach we get candidate positions at the next timestep for one of the triangles as:
for . For the backward formulatoin we get:
In fact for the backward formulation we already have the exact candidates for the previous timestep. We can instaed compute linear velocity as Using bounding boxes around previous and next timestpe we can see whether there might exist a collision or not.
CCD for contact. The idea is to use 3 nodes from one triangle to get the surface normal and another node to check whether it is in the plane of . First we get . Distance to the origina is .
For a new point to lie in the triangle we need:
Now introducing time-dependence and linear motion equations from beofre into the test we get:
and we want to determine the minmial non-negative such s.t. the above is true. This is a polynomial so we can find these values using a root-finding algorithms. This gives the point of contact .
CCD for contact. We can use the same approach as above, but we need to consider the edges as well. We must check if the edges are parallel:
If this is a case we can project vertices on the line through the other edge and check for intersection.
When the two edges are not parallel—possibly touching at only 1 point—then we can reparametrize the edges as line equations:
Then the contact point must also be the closest point between these two lines:
From preschool we know that we can solve this in the normal equations form:
The solutions give the candidate closest point. If we can simply write the point as:
If we really want to get sweaty, we can write this using Barycentric coordinates:
where and are Barycentric coordinates for the traingle.
Deformable Bodies / Soft Body Contact
While we did discuss how to frame contact simulation for deformable bodies briefly, we will now give a more nuanced discussion of the topic.
First, reacall the DiffEq that govers dynamics of a deformable body:
where is the damping force, is the (internal) elastic force, and is the contact force. The first conceptual shift we will do is to consider a system of deformables as one global tetrahedral mesh, so e.g. where is the number of vertices in the “mesh soup” of our system. From a numerical standpoint this view is no differnet from before, but it will give us some benefits when computing contact forces.
With this in mind let us turn to computing contact forces, specifically how rewrite the constraint Jacobian for constrain-based approaches:
where containts the Lagrange multipliers of all the contact forces. Consider two tetrahedral elements defined by , respectively. Then the point of contact is
again through our favourite Barycentric coordinates. At the time of contact we have and is the normal to the surface at . As we did many times before, we now need to compute the relative velocity of the contact point:
which is expressed in world space. To convert it into contact space:
where is the contact space coordinate system from before. This expression projects relative velocity onto . But since are not time-dependent we can rewrite:
and please bear with me as I use to denote a vector of velocities . To construct the global Jacobian we can combine these local Jacobians for all the contact points to get .
Time Discretization for Deformables
Instead of using semi-implicit integrators we usually gravitate towards implicit schemes, because stiff elasticity limits time-step sizes.
The first way to solve implicit formulations is using root-finding which gives time-updated state vectors. The second popular way is by minimizing an energy potential using a first-order optimizer. The minimization approaches can be broken into position and velocity-level methods and we will very briefyl discuss them after we introduct root-finding methods.
Root-finding. Rewrite order ODE as a system of coupled order ODEs:
For why we do this, refer to [notes]. We can now formulate root finding using the following (rough) steps:
- Write primary or dual form of the system of equations.
- Use finite differences to compute time-derivatives.
- Derive Newton’s method equation by plugging in order Taylor approximations. This should give us a linear system which can be solved using different methods (Schur complement, etc.).
- Iterate until convergence.
We derive the Primary form of the system. Assume for the moment that contact forces are where is a contact potential function. Then the descretized equations of motion in Euler method are:
which can be rewritten in matrix form as a homogeneous system:
This gives a rootfinding problem:
which can be solved using Newton’s method. Assuming are known we want the updates:
where are the updates. We can incorporate line-search into the Newton’s method to ensure convergence by ading a step-length parameter. Next step is to compute order Taylor approximations of elements. First we note that , , , , where are the dambing and stifness matrices. Particularly we have:
which by noting we can write as:
A solution can be obtained by solving 1st row for , then 2nd row to solve for . We can then update and and repeat until convergence.
Dual.
Position-level Minimization. We rewrite the time-integration as a minimizatoin problem:
is a solution to implicit Euler formulation. The first part is the momentum potential where , the second term minimizes the energy potential of elastic strain , and the third minimizes work done by .
To actually solve it we need to form the optimality conditions, i.e. a Lagrangian function:
where are Lagrange multipliers for constraints . We can then compute the gradient of the Lagrangian and set it to zero to get the ( order) optimality conditions:
With expanding and using some tricks (refer to the [notes] ) we get the minimization problem equivalent to the primal form:
where is the contact potential.
Velocity-level Minimization. Before we talk about this, note that velocity-level minimization is not that popular for deformable somulations. That said, it is very similar to the position-level minimization, we get the optimization problem:
where , ignoring the damping forces and constraint is approximated to first order as . Looking at as function of :
Little Detour: Incremental Potential
We will do something very similar to above, but with more standard notation that we will later use when covering IPC. We start with Backward Euler discretization of the equations of motion and reformulate them as an optimization problem.
which is a conservative system. Using Beckward Euler discretization of the continuous system and approximation of with previous two time-steps we get:
and by rearranging with we get:
Seeing the above as a gradient of an expression, we see:
so the minimizer prediction of the next node position becomes:
where is the incremental potential of the system. This is a very useful concept in contact simulation, because it allows us to write the equations of motion as a minimization problem. The first term of is the inertia term, which is the energy of the system if it was at rest. The second term is the potential energy (e.g. elastic potential) of the system. The minimization problem is then to find the configuration of the system that minimizes the energy. For example, in context of elastic deformables we will have this optimization as:
where note that since I multiplied the second term by I can drop the in the first term, since the minimizer will be the same. Here is the elastic potential of the system.
Recall, we want to solve the above minimization problem w.r.t to the constraint that , i.e. interpenetration free! This is very nonlinear, nonconvex, and hard to solve. In the next section we will see how to solve this using penalty and barrier methods.
Penalty and Barrier Methods
This is a completely different approach to handling contact than the constraint-based approaches we have been talking about so far. The idea is to use a potential function to penalize the interpenetration of objects. The potential function is a function of the penetration depth and the normal force :
Overview
The methods we have covered so far attempted to remove interpenetration of objects by either shifting objects apart or applying impulses to them at the point of contact. In contrast, in penalty based methods, we use a spring-mass system to prevent / penalize interpenetration. The idea is that if we model the forces due to penetration as a* spring force with rest length of *, then we can find a force (and torque )—external forces—s.t. at some we can use them in the equations of motion to perform integration.
If a rigid body penetrates , then for the contact points we have a force gives rise to a torque term that is applied at the contact point. If the vectorf rom the center of mass of to the contact point is , then the torque is . To create a “penalty method” affected external force vector we can sum all the force and torque vectors:
Then in the simulation loop we would: 1. find contact points (collision detection) 2. compute and accumulate spring forces 3. integrate equations of motion foward in time.
Contact Spring Forces. Considering the contact at between with a contact normal and a measure of penetration depth we can write the force exerted by body onto as a spring force:
where is the spring constant, is the relative velocity of the contact points, and is the damping constant. The force exerted by onto is then . The torque is then . The is in the paranthesses because we only really care about relative velocity in the normal direction.
Now recall how we computed relative velocity :
which we have seen before. Looking back at we find its magnitude to be:
HOWWWWWW IS THIS A MAGNITUDE???????????????????/ IT SHOULD HAVE A SIGN
Now we can write the forces on the whole system due to contact as:
For all the contacts we can generalize as:
where is a diagonal matrix with on the diagonal. If we have contacts and bodies we have dimensions and and .
Soft Bodies. If we want to generalize this to soft bodies, we will replace the dimension into dimension and remove , since there will be no torque component to the spring force. If the contact does not fall onto a specific node, then we distribute the penalty force on the enclosing nodes of the element, using some weighing method like Barycentric coordinates.
Finally, we can use the above general system, discretize it and perform timestepping. Xu [2014] shows an implict time-stepping scheme.
Penalty based friction. We will also use a spring system to model the friction in our simulator. Usually, we denote the first point of contact as (anchor) and then track the actual point of contact through time. If the coefficient of the spring from to is and its rest length is 0, then the friction force is:
If where is the Coulomb coefficient of friction, then we have static friction. If not, then we are dealing with dynamic friction (objects are sliding relative to each other). In the latter case, we move to the new contact point so that the condition above is true.
Alternative way of computing the friction force is to relate it to the relative tangential velocity:
but this model ignores static friction.
One problem with penalty based methods is that we have a tradeoff between having a stiff, difficult problem to solve for a high and an easy system to solve with small , that causes tunneling. The alternative is to use barrier methods.
Barrier Methods
The core idea of barrier methods is to prevent interpenetration by adding a barrier function to the potential function. These barrier functions diverge as we get closer to the object. Again, we have to be careful how to solve this stiff problem.
One simple method is called asynchronous contact mechanics. We start with a quadratic penality:
,
where $is the thickness of th esurface and are the closest points on the surfaces of the objects. We can then write the contact potential as:
if , else 0, where is the penalty barrier stiffness. Some suggested we can stack these barriers as a sequence of quadratic barrier functions where s are increasing and s are decreasing. Then their sum will just converge to . That said, these methods are too computationally intesive.
While well-suited for small time-step explicit methods, the incremental construction of the barriers challenge application in implicit time integration with Newton-type optimization.
IPC - Incremental Potential Contact
Another way to achieve robust contact handling is by using barrier potentials within an optimization-based implicit time integration. As you can recall, we saw that implict time integration can be reframed as an optimization over nodal positions and velocities :
Here is set of all contacts that are active at time . So far we treated contacts as inequality constraints, but we can also turn those constraints into barrier potentials. This is exactly what IPC does. This transforms our objective to:
where is the barrier function, is the unsigned distance between the objects. We can define the barrier function:
if , else . Here is smoothly clamed barrier that goes to 0 as . Now we can use to trade-off accuracy and performance. The smaller makes the barrier thinner and the simulation more realistic, but harder to optimizer. The larger makes the barrier thicker and the simulation less realistic, but easier to optimize.
We could say all constraints are active, but becuase we have local support from the clamed barrier function, we can use a data structure like spatial hasing to not consider contacts with 0 force.
The main benefit of IPC approach is that it’s an unconstrained minimization, which can be robustly solved using Newton’s method with line search. By using CCD within the extend of line-search we can get a guarantee that for every step we will have no collisions independent of the parameter choices.
The limitatoin here is that we have to make sure that the input geometry and do not cause interpenetration in the initial state.
(Consistent) Distance Functions. Commonly with distance-based constraints we use a measure like using a SDF. But under large dispalcements, this constraint can be violated without interpenetration.
IPC on the other hand uses an unsigned distnace function. This gives a consisten constraint measure as the contact moves through time. In 3D this boils down to computing minimal distance between 2 triangles; as long as the triangles do not interesect, the distance is positive. In tha case we can formulate the problem as minimum distance between vertices and triangles as well as between edges and edges.
First, the distance between a point and triangle as we have allready seen can be expressed using Barycentric coordinates u,v:
s.t. . The distance between two edges parametrized with can be expressed as:
s.t. . The best thing is that both have closed form solutions.