This applet simulates rectangular shaped objects colliding in 2 dimensions. You can set gravity, elasticity (bounciness), and damping (friction) using the slider controls. You can choose from one to six objects. The mass of the objects is initially set to 1.0, but the mass of the green object is adjustable.
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. (If the keys don't work, try clicking near an object first - this ensures that key strokes are passed to the simulation instead of the buttons or scrollbars).
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.
Ignoring collisions for the moment, we develop the equations of motion for the objects. The forces on the objects are
For each body, there are three variables to specify position:
In addition, each body has a velocity corresponding to each of these positions:
The equations of motion for the body involve the total force
Fy = m y''
τ = I θ''
where
We now build up the equations of motion by adding in one force at a time, beginning with the thrust force. Let
m y'' = Ty
In these equations it doesn't matter where on the body the thrust force is applied. The point
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
Let
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:
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:
The rubber band force in the simulation operates along the vector from point
m y'' = Ty + By
I θ'' = R × T + R × B
Let
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
m y'' = Ty + By - m g - k y' (eqn 2)
I θ'' = R × T + R × B - k θ' (eqn 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 (1-3) are the equations of motion for one of our rectangular bodies. To use the Runge-Kutta method for solving a set of differential equations we need to convert these 3 second order equations into 6 first order equations. Define the velocity variables
Then we have the 6 first order differential equations
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
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
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
Translational energy is
Rotational energy is
Linear momentum in the horizontal direction is given by
Angular momentum is measured with regard to a particular point in space, for example the origin. It is given by:
where
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. If so, we use a binary search to back up 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 velocities as described below. The Colliding Blocks simulation further describes these aspects of collision handling.
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.
Suppose a vertex on body A is colliding into an edge of body B at the point P. Define the following variables
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
vbp1 = vb1 + ωb1 × rbp
Similarly we have the final post-collision velocities
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
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
vab2 = vap2 - vbp2
Using the formulas given above for velocity of a point on a rigid body we can expand these to
vab2 = va2 + ωa2 × rap - vb2 - ωb2 × rbp (eqn 5)
Let the vector
Note that for a collision to occur this relative normal velocity must be negative (that is, the objects must be approaching each other).
Let
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
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
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
vb2 = vb1 - j n / mb (eqn 8)
The change in angular momentum of body A from the impulse
ωb2 = ωb1 - (rbp × j n) / Ib (eqn 10)
Now we can put all these various equations together and solve for the impulse parameter
We start with equation (6), and then expand using our definition of relative velocity in equations (4-5).
(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).
We recognize that the left hand side contains the quantity
Note that
to derive the following identity
(Here squaring a vector means taking its dot product with itself.) We can then simplify to
Dividing leads to our final expression for
-(1 + e) vab1 ∙ n j = ———————————————————————————————————————————— (eqn 11) 1/ma + 1/mb + (rap × n)2/Ia + (rbp × n)2/Ib |
We can now calculate
We can use this same expression for calculating collisions with a wall by assuming that the mass of the wall is infinite. So let
-(1 + e) vap1 ∙ n j = ———————————————————— (eqn 12) 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:
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
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. Some of the references cited earlier have sections on calculating resting contact forces.