Rigid body game physics 2

part 1 part 2 part 3 part 4 part 5 part 6

Update: See Getting started with the Jolt Physics Engine

The following article is heavily based on Hubert Eichner’s article on equality constraints. The math for the joint types was taken from Kenny Erleben’s PhD thesis.

Constraint Impulses

A constraint between two objects is specified as a multi-dimensional function C(y(t)). The constrained is fulfilled by attempting to keep each component of the derivative of C at zero: latex formula J is the Jacobian matrix and u is a twelve-element vector containing the linear and rotational speed of the two bodies. Here the derivative of the orientation is represented using the rotational speed ω. latex formula The two objects without a connecting joint would together have twelve degrees of freedom. Let’s assume that the two objects are connected by a hinge joint. In this case the function C has five dimensions because a hinge only leaves seven degrees of freedom. The Jacobian J has five rows and twelve columns. In practise an additional vector b is used to correct for numerical errors. latex formula

The twelve-element constraint impulse P does not do any work and therefore must be orthogonal to u. latex formula The rows of J span the orthogonal space to u. Therefore P can be expressed as a linear combination of the rows of J latex formula where λ is a vector with for example five elements in the case of the hinge joint.

Given an initial estimate for u and the constraint impulse P one can compute the final velocity u. latex formula M is the twelve by twelve generalised mass matrix which contains the mass and inertia tensors of the two objects. latex formula Inserting the equation for u into the constraint equation yields latex formula This equation can be used to determine λ latex formula λ then can be used to determine the constraint impulse P! When performing the numerical integration, the external forces (multiplied with the timestep) and the constraint impulse are used to determine the change in linear and rotational speed of the two bodies.

Joints

Ball-in-Socket Joint

The matrix J for bodies i and j and the velocity vector u can be split up into the linear and angular parts. latex formula v is the three-dimensional linear speed and ω is the rotational speed of the individual object. At the anchor point of the joint the speed of the two rigid bodies must be the same: latex formula Thus the linear components of J are latex formula and the angular components of J are latex formula where the cross-product matrix of a vector is defined as follows: latex formula The correcting vector b simply corrects for the offset at the anchor point. latex formula

Hinge Joint

A hinge joint has the constraints of the ball-in-socket joint and two additional angular constraints. I.e. there are five constraints altogether. latex formula The rotation axis in world coordinates can be computed from the average of the two rotated axes which should be coinciding under ideal circumstances. latex formula

The relative rotation of the two rigid bodies must be parallel to the axis s of the hinge. Given two vectors t1 and t2 orthogonal to the rotation axis of the hinge joint, the two additional constraints are: latex formula Therefore the angular parts of the Jacobian are latex formula

The error u of axis misalignment is the cross product of the two rotated axes. latex formula The error u is orthogonal to the rotation axis s. It is projected onto the vectors t1 and t2 when computing the correction vector b: latex formula

See Kenny Erleben’s PhD thesis for these and other types of joints (slider joint, hinge-2 joint, universal joint, fixed joint).

Sequential Impulses

To fulfill multiple constraints, an algorithm called sequential impulses is used. Basically the constraint impulses are updated a few times until the impulses become stable.

The sequential impulse iterations have to be performed four times when using the Runge-Kutta method.