myphysicslab – rigid body collisions
this simulation shows rectangular objects colliding in 2 dimensions.
click near an object to exert a rubber band force with your mouse. with the keyboard you can control four "thrusters". the keys s,d,f,e control the blue object. the keys j,k,l,i (and also the arrow keys) control the green object.
you can also set gravity, elasticity (bounciness), and damping (friction). you can choose from one to six objects. the mass of the green object is adjustable (the others are set to mass 1.0). if you don't see the simulation try instructions for enabling java. scroll down to see the math!
here is a picture of how the keyboard controls are arranged. if the keys don't work, try clicking near an object first  this ensures that keystrokes are passed to the simulation instead of the buttons or scrollbars. on some browsers (opera) you might have to also type into one of the numeric controls (like gravity) then click near an object before keystrokes are passed to the simulation.
keyboard thruster controls
if elasticity is less than 1, and gravity is greater than zero, then the objects eventually settle onto the floor. because this simulation does not implement
resting contact the simulation will get 'stuck'.
energy bar graph
to check the correctness of the simulation, you can click the "show energy" checkbox and look at the display of the pre and postcollision momentum and energy. if damping = 0 and elasticity = 1, then the energy should not change. the momentum should not change when objects collide, however it will change when an object collides with a wall (walls are assumed to be infinitely massive, so we don't model their movement).
the bar graph shows the gravitational, rotational and translational energy. note that if gravity is zero, then there is no gravitational potential energy.
see the section on
energy and momentum for how these quantities are calculated.
physics of motion for rigid bodies
rigid body variables
ignoring collisions for the moment, we develop the equations of motion for the objects. the forces on the objects are
 gravity
 friction
 thrust (keyboard controls)
 rubber band (mouse control)
for each body, there are three variables to specify position:
 x = horizontal position of center of mass
 y = vertical position of center of mass
 θ = angle of rotation
in addition, each body has a velocity corresponding to each of these positions:
 x' = v_{x} = horizontal velocity of center of mass
 y' = v_{y} = vertical velocity of center of mass
 θ' = ω = angular velocity
the equations of motion for the body involve the total force
f = (f_{x}, f_{y}) and torque τ on the body as follows:
f_{x} = m x''
f_{y} = m y''
τ = i θ''
where m = mass and i = moment of inertia about the center of mass (moment of inertia is defined in the next section). note that we indicate vectors, such as f, with bold and an overline.
thrust force
thrust force vector
we now build up the equations of motion by adding in one force at a time, beginning with the thrust force. let t = (t_{x}, t_{y}) be the thrust force vector which operates at the point p on the body. the thrust force will accelerate the body according to
m x'' = t_{x}
m y'' = t_{y}
in these equations it doesn't matter where on the body the thrust force is applied. the point p can be anywhere on the body, yet because the body is rigid the thrust accelerates the entire body. on the other hand, for rotational movement it matters a great deal where on the body the thrust is applied.
moment of inertia is the equivalent of mass for rotational physics. it measures how difficult it is to rotate a body about a given point. since our rectangle bodies rotate freely about their center of mass, we use center of mass as the point for calculating moment of inertia. from a physics textbook you can find the equation for the moment of inertia about the center of a thin rectangular plate. it is given by
i = m (width^{2} + height^{2}) ⁄ 12
let r = (r_{x}, r_{y}) = be the distance vector from center of mass to p. the torque at the point p is given by the vector cross product
r × t = r_{x} t_{y} − r_{y} t_{x}
so we have
i θ'' = r × t
actually torque is a vector, but since we are working in 2 dimensions we know that torque is always perpendicular to the plane and so we aren't using vector notation for it. the true vector cross product results in a vector. so the above cross product corresponds to the following 3 dimensional calculation:
(r_{x}, r_{y}, 0) × (t_{x}, t_{y}, 0) = (0, 0, r_{x} t_{y} − r_{y} t_{x})
you can see that the result is always perpendicular to the plane, with zero x and y components. the general vector cross product of two vectors is defined as:
(x, y, z) × (u, v, w) = (y w − z v, −x w + z u, x v − y u)
rubber band force
the rubber band force in the simulation operates along the vector from point
p on the body to the mouse position, call this vector
l. the rubber band force is
b = (b_{x}, b_{y}) = s l where
s is the stretchiness constant. the treatment of this force is identical to the thrust force, so we add it in to get the following equations.
m x'' = t_{x} + b_{x}
m y'' = t_{y} + b_{y}
i θ'' = r×t + r×b
gravity force
let
g = the gravitational constant. gravity causes a force on the center of mass of
−m g in the vertical direction leading to
m y'' = t_{y} + b_{y} − m g
because gravity works on all points of the body, no torque is generated by gravity.
damping force
damping (friction) causes a force opposite to the direction of motion. the faster you go, the more friction resists your motion. so the magnitude of the damping force is proportional to the velocity. let
k = the proportional damping constant. adding this to our equations of motion gives

m x'' = t_{x} + b_{x} − k x'
 (1) 

m y'' = t_{y} + b_{y} − m g − k y'
 (2) 

i θ'' = r×t + r×b − k θ'
 (3) 
in a more realistic simulation, there may be different damping constants for rotational versus translational motion. but here, we use the same constant for both.
equations (13) are the equations of motion for one of our rectangular bodies. as long as no collision occurs, these equations are in charge. here is a recap of some of the variables:
 m = mass
 g = gravity constant
 k = damping (friction) constant
 t = thrust force vector
 b = rubber band force vector
 r = vector from center to point p where thrust is applied
numerical simulation
to use the
rungekutta method for solving a set of differential equations we need to convert the above 3
second order equations into 6 first order equations. define the velocity variables
 v_{x} = x' = horizontal velocity
 v_{y} = y' = vertical velocity
 ω = θ' = angular velocity
then equations (13) become 6 first order differential equations
x' = v_{x}
y' = v_{y}
θ' = ω
v_{x}' = (t_{x} + b_{x} − k v_{x}) ⁄ m
v_{y}' = (t_{y} + b_{y} − m g − k v_{y}) ⁄ m
ω' = (r × t + r × b − k ω) ⁄ i
these equations are now in the form needed for solving numerically with the rungekutta method. each rectangle body will have its own set of 6 variables (3 positions
x, y, θ and 3 velocities
v_{x}, v_{y}, ω) and 6 first order differential equations.
how to calculate energy and momentum
this section explains how the energy and momentum of the objects are calculated. see the description of the
energy bar graph for how to observe these quantities in the simulation.
if there is no loss of energy to friction (damping
= 0) or during collisions (elasticity
= 1) then the energy of the system should not change.
a collision between objects should not change the angular momentum. however, a collision with a wall will not preserve angular momentum because the super massive walls are not included in the calculations of the momentum of the objects.
gravitational energy is given by
m g h where
h = height of the object's center of mass above the floor.
translational energy is
^{1}⁄_{2} m v^{2} where
v is the velocity vector for the object's center of mass. note that we use vector dot product to square the velocity vector.
rotational energy is
1/2 i ω^{2} where
i is the moment of inertia and
ω is the angular velocity.
linear momentum in the horizontal direction is given by
m v_{x}, in the vertical direction by
m v_{y}.
angular momentum is measured with regard to a particular point in space, for example the origin. it is given by:
i ω k + m r × v
where
r is the distance vector from the origin to the object's center of mass. the vector
k is the unit
z vector, which points out of the
xy plane. you can see that the angular momentum has two components: the spinning component
i ω k and the rotation about the origin given by the vector crossproduct
m r × v.
collision detection
at each step in the simulation, we check to see if there is a collision. the bodies can collide with each other or with a boundary wall. for the rectangular shapes we are using it is simple geometry to determine if a collision has occurred by checking if any vertex is within a wall or foreign body.
when a collision is detected, we use a binary search to back up the simulation to an earlier time just shortly before the collision occurred. we then make the approximation that the collision takes place at this exact time, and calculate the resulting changes in velocity as described below. the
colliding blocks simulation further describes these aspects of collision handling.
physics of collision for rigid bodies in 2 dimensions
handling collisions is the most challenging part of this simulation. the explanation here is fairly condensed, so you may want to read some other descriptions as well.
 chris hecker's rigid body dynamics information. see especially his article for game developer magazine on collision response. easy to read, with many other references listed on the web site. unfortunately some of the pdf documents do not print their math correctly.
 physically based modeling is a set of course notes from siggraph '97 by andrew witkin and david baraff. see especially the sections on rigid body dynamics. fairly complete and rigorous explanations, but still quite readable. covers the 3 dimensional case and also resting contact forces.
 physics for game developers by david m. bourg (o'reilly press) has a section on linear and angular impulse on page 95, and resting contact forces on page 258. this book has good explanations, but often relies on hardtofollow programming code to illustrate the physics.
 the textbook engineering mechanics dynamics by robert soutaslittle and daniel inman has relevant sections on eccentric impact.
vectors involved in collision
suppose a vertex on body a is colliding into an edge of body b at the point p. define the following variables
 m_{a}, m_{b} = mass of bodies a, b
 r_{ap} = distance vector from center of mass of body a to point p
 r_{bp} = distance vector from center of mass of body b to point p
 ω_{a1}, ω_{b1} = initial precollision angular velocity of bodies a, b
 ω_{a2}, ω_{b2} = final postcollision angular velocity of bodies a, b
 v_{a1}, v_{b1} = initial precollision velocities of center of mass bodies a, b
 v_{a2}, v_{b2} = final postcollision velocities of center of mass bodies a, b
 v_{ap1} = initial precollision velocity of impact point on body a
 v_{bp1} = initial precollision velocity of impact point on body b
 n = normal (perpendicular) vector to edge of body b
 e = elasticity (0 = inelastic, 1 = perfectly elastic)
we now use a standard formula for the velocity of an arbitrary point on a rotating and translating rigid body to get the precollision velocities of the points of collision (which is the point
p on each body).
v_{ap1} = v_{a1} + ω_{a1} × r_{ap}
v_{bp1} = v_{b1} + ω_{b1} × r_{bp}
similarly we have the final postcollision velocities
v_{ap2} and
v_{bp2} as
v_{ap2} = v_{a2} + ω_{a2} × r_{ap}
v_{bp2} = v_{b2} + ω_{b2} × r_{bp}
here we are regarding the angular velocity as a 3 dimensional vector perpendicular to the plane, so that the cross product is calculated as
ω × r = (0, 0, ω) × (r_{x}, r_{y}, 0) = (−ω r_{y}, ω r_{x}, 0)
now we can find an expression for the velocity with which the colliding points are approching each other. we call this the relative velocity. let
v_{ab1} be the initial (precollision) relative velocity and
v_{ab2} be the final (postcollision) relative velocity. we define the relative velocities as follows
v_{ab1} = v_{ap1} − v_{bp1}
v_{ab2} = v_{ap2} − v_{bp2}
using the formulas given above for velocity of a point on a rigid body we can expand these to

v_{ab1} = v_{a1} + ω_{a1} × r_{ap} − v_{b1} − ω_{b1} × r_{bp}
 (4) 

v_{ab2} = v_{a2} + ω_{a2} × r_{ap} − v_{b2} − ω_{b2} × r_{bp}
 (5) 
let the vector
n be normal (perpendicular) to the edge of body b that is being impacted, and pointing outward from body b. also let the vector
n be of length 1. then we can find the relative velocity in the direction of the normal
n by using the dot product:
relative normal velocity = v_{ab1} · n = v_{ab1x} n_{x} + v_{ab1y} n_{y}
note that for a collision to occur this relative normal velocity must be negative (that is, the objects must be approaching each other).
let
e be the elasticity of the collision, having a value between 0 (inelastic) and 1 (perfectly elastic). we now make an important assumption in the form of the following relation

v_{ab2} · n = −e v_{ab1} · n
 (6) 
this says that the velocity at which the objects fly apart is proportional to the velocity with which they were coming together. the proportionality factor is the elasticity
e.
collision impulse
to resolve the collision, we will use the concept of an
impulse. an impulse is the change in momentum of an object when a large force is applied over a very brief period of time. we imagine that during the collision there is a very large force acting for a very brief period of time. if you integrate (sum) that force over that brief time, you get the impulse.
why do we need this strange concept of an impulse? why not just use the familiar concept of force as in
f = m a ? the answer is that we do not know what the forces are during the collision. with a supercomputer and some very complex software we could model the forces that occur during a collision. we would need to know details about the materials of the bodies, their exact geometry, how they deform under stress, how the stress propagates through the body, etc.
this is far beyond what our simple simulation can do. luckily, we can assume that the collision happens so quickly that the position and orientation of the bodies do not change during the collision. instead, all that changes is the velocities of the bodies. since a change in velocity is a change in momentum (remember momentum = velocity times mass) we have the concept of an impulse.
we are assuming no friction for our collision, so the only force during the collision is in the direction perpendicular to the edge, which is given by the vector
n. (friction would cause a force parallel to the edge as well). let the net impulse of the collision be
j n where
j is a parameter to be determined. body a experiences an impulse of
j n while body b experiences the equal but opposite impulse of
−j n. the impulse is a change in momentum. momentum has units of velocity times mass, so if we divide the impulse by the mass we get the change in velocity. we can relate pre and postcollision velocities as

v_{a2} = v_{a1} + j n / m_{a}
 (7) 

v_{b2} = v_{b1} − j n / m_{b}
 (8) 
the change in angular momentum of body a from the impulse
j n is given by
r_{ap} × j n. you can think of the impulse as being applied at the point
p, so it generates an instantaneous torque there. we then divide by the moment of inertia to convert the change in angular momentum to a change in angular velocity, so the postcollision angular velocities are

ω_{a2} = ω_{a1} + (r_{ap} × j n) / i_{a}
 (9) 

ω_{b2} = ω_{b1} − (r_{bp} × j n) / i_{b}
 (10) 
solving for the impulse parameter
now we can put all these various equations together and solve for the impulse parameter
j. if you aren't interested in the details of solving the equations just skip down to the expression for
j.
we start with equation (6), and then expand using our definition of relative velocity in equations (45).
v_{ab2} · n = −e v_{ab1} · n
(v_{ap2} − v_{bp2}) · n = −e v_{ab1} · n
(v_{a2} + ω_{a2} × r_{ap} − v_{b2} − ω_{b2} × r_{bp}) · n =
−e v_{ab1} · n
let's expand the left hand side, using the impulse relationships in equations (710).
((v_{a1} + j n / m_{a}) + (ω_{a1} + (r_{ap} × j n) / i_{a}) × r_{ap} −
(v_{b1} − j n / m_{b}) −
(ω_{b1} − (r_{bp} × j n) / i_{b}) × r_{bp}) · n =
−e v_{ab1} · n
we recognize that the left hand side contains the quantity
v_{ab1} · n as given by equation (4), so we move that to the right side.
(j n / m_{a} + (r_{ap} × j n) × r_{ap} / i_{a} +
j n / m_{b} +
(r_{bp} × j n) ×r_{bp} / i_{b}) · n =
−(1 + e) v_{ab1} · n
note that
n is assumed to be normalized so that
n · n = 1. also, to simplify the various vector products, we can use the triple scalar product rule
(a × b) · c = (b × c) · a
to derive the following identity
(a × b) × a · b = (a × b) · (a × b) = (a × b)^{2}
(here squaring a vector means taking its dot product with itself.) we can then simplify to
j (1 / m_{a} + 1 / m_{b} + (r_{ap} × n)^{2} / i_{a} +
(r_{bp} × n)^{2} / i_{b}) =
−(1 + e) v_{ab1} · n
dividing leads to our final expression for
j.
j =

−(1 + e) v_{ab1} · n

^{1}⁄_{ma} + ^{1}⁄_{mb} + (r_{ap} × n)^{2} ⁄ i_{a} + (r_{bp} × n)^{2} ⁄ i_{b}

 (11) 
we can now calculate
j at the time of collision using equation (11), and therefore calculate the postcollision velocities using the earlier equations (710).
we can use this same expression for calculating collisions with a wall by assuming that the mass of the wall is infinite. so let
m_{b} → ∞ and
i_{b} → ∞ and equation (11) becomes
j =

−(1 + e) v_{ap1} · n

^{1}⁄_{ma} + (r_{ap} × n)^{2} ⁄ i_{a}

multiple impacts
the above analysis only handles the case of a single corner and edge impact. there are several other simultaneous multiple impact cases, such as:
 two corners of object a impact a single wall
 two corners of object a impact object b
 two corners of object a impact different walls
 two corners of object a impact different objects
if there are simultaneous impacts among unrelated objects, these can be easily handled separately. for example, suppose objects a and b collide at the same time that objects c and d collide, then we can deal with each collision separately because they don't affect each other.
for the case of two adjacent corners of object a impacting either a single wall or another object's edge, we change the impact point to be the midpoint between the two corners and then handle it as other collisions.
the more complicated cases with corners of object a impacting different walls or multiple objects is not handled correctly by this simulation. what the code does is handle each impact separately, which will probably be wrong because these collisions are not independent.
you will notice that certain settings of gravity and elasticity cause the simulation to eventually get stuck. specifically, this happens when gravity
> 0 and elasticity
< 1. the problem occurs when an object is settling down onto the floor. the velocity of the object is becoming smaller with each collision because the elasticity is less than one. eventually the velocity becomes so small that it can't keep gravity from pulling the object down past the floor. this is what results in the simulation getting 'stuck'.
the right thing to do at this point (which this simulation does not do) is recognize that the object is in
resting contact with the floor. this would change the equations of motion for the object to include a contact force from the floor, which prevents the penetration of the object. see the
collisions with contact simulation which handles this.