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 post-collision 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
- 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' = vx = horizontal velocity of center of mass
- y' = vy = vertical velocity of center of mass
- θ' = ω = angular velocity
the equations of motion for the body involve the total force
f = (fx, fy) and torque τ on the body as follows:
fx = m x''
fy = 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 vector
we now build up the equations of motion by adding in one force at a time, beginning with the thrust force. let t = (tx, ty) 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'' = tx
m y'' = ty
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 (width2 + height2) ⁄ 12
let r = (rx, ry) = 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 = rx ty − ry tx
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:
(rx, ry, 0) × (tx, ty, 0) = (0, 0, rx ty − ry tx)
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 = (bx, by) = s l
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'' = tx + bx
m y'' = ty + by
i θ'' = r×t + r×b
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'' = ty + by − m g
because gravity works on all points of the body, no torque is generated by gravity.
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'' = tx + bx − k x'
m y'' = ty + by − m g − k y'
i θ'' = r×t + r×b − k θ'
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 (1-3) 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
to use the runge-kutta 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
- vx = x' = horizontal velocity
- vy = y' = vertical velocity
- ω = θ' = angular velocity
then equations (1-3) become 6 first order differential equations
x' = vx
y' = vy
θ' = ω
vx' = (tx + bx − k vx) ⁄ m
vy' = (ty + by − m g − k vy) ⁄ m
ω' = (r × t + r × b − k ω) ⁄ i
these equations are now in the form needed for solving numerically with the runge-kutta method. each rectangle body will have its own set of 6 variables (3 positions x, y, θ
and 3 velocities vx, vy, ω
) 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 v2
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
is the moment of inertia and ω
is the angular velocity.
linear momentum in the horizontal direction is given by m vx
, in the vertical direction by m vy
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
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 x-y
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 cross-product m r × v
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 hard-to-follow programming code to illustrate the physics.
- the textbook engineering mechanics dynamics by robert soutas-little 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
- ma, mb = mass of bodies a, b
- rap = distance vector from center of mass of body a to point p
- rbp = distance vector from center of mass of body b to point p
- ωa1, ωb1 = initial pre-collision angular velocity of bodies a, b
- ωa2, ωb2 = final post-collision angular velocity of bodies a, b
- va1, vb1 = initial pre-collision velocities of center of mass bodies a, b
- va2, vb2 = final post-collision velocities of center of mass bodies a, b
- vap1 = initial pre-collision velocity of impact point on body a
- vbp1 = initial pre-collision 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 pre-collision velocities of the points of collision (which is the point p
on each body).
vap1 = va1 + ωa1 × rap
vbp1 = vb1 + ωb1 × rbp
similarly we have the final post-collision velocities vap2
vap2 = va2 + ωa2 × rap
vbp2 = vb2 + ωb2 × rbp
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, ω) × (rx, ry, 0) = (−ω ry, ω rx, 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 vab1
be the initial (pre-collision) relative velocity and vab2
be the final (post-collision) relative velocity. we define the relative velocities as follows
vab1 = vap1 − vbp1
vab2 = vap2 − vbp2
using the formulas given above for velocity of a point on a rigid body we can expand these to
vab1 = va1 + ωa1 × rap − vb1 − ωb1 × rbp
vab2 = va2 + ωa2 × rap − vb2 − ωb2 × rbp
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 = vab1 · n = vab1x nx + vab1y ny
note that for a collision to occur this relative normal velocity must be negative (that is, the objects must be approaching each other).
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
vab2 · n = −e vab1 · n
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
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
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 post-collision velocities as
the change in angular momentum of body a from the impulse j n
is given by rap × 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 post-collision angular velocities are
ωa2 = ωa1 + (rap × j n) / ia
ωb2 = ωb1 − (rbp × j n) / ib
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 (4-5).
vab2 · n = −e vab1 · n
(vap2 − vbp2) · n = −e vab1 · n
(va2 + ωa2 × rap − vb2 − ωb2 × rbp) · n =
−e vab1 · n
let's expand the left hand side, using the impulse relationships in equations (7-10).
((va1 + j n / ma) + (ωa1 + (rap × j n) / ia) × rap −
(vb1 − j n / mb) −
(ωb1 − (rbp × j n) / ib) × rbp) · n =
−e vab1 · n
we recognize that the left hand side contains the quantity vab1 · n
as given by equation (4), so we move that to the right side.
(j n / ma + (rap × j n) × rap / ia +
j n / mb +
(rbp × j n) ×rbp / ib) · n =
−(1 + e) vab1 · 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 / ma + 1 / mb + (rap × n)2 / ia +
(rbp × n)2 / ib) =
−(1 + e) vab1 · n
dividing leads to our final expression for j
−(1 + e) vab1 · n
1⁄ma + 1⁄mb + (rap × n)2 ⁄ ia + (rbp × n)2 ⁄ ib
we can now calculate j
at the time of collision using equation (11), and therefore calculate the post-collision velocities using the earlier equations (7-10).
we can use this same expression for calculating collisions with a wall by assuming that the mass of the wall is infinite. so let mb → ∞
and ib → ∞
and equation (11) becomes
−(1 + e) vap1 · n
1⁄ma + (rap × n)2 ⁄ ia
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.