Collision and Response

In the introductory article we saw a simple approach to collisions and their results. The little game we developed there was not taking into account the laws of physics. As we have discussed in the 'Trajectories' article we should put some physics in out life to make our games more realistic. So now we start with the behavior of two spheres colliding. The reason I chose this situation is because the collision detection between spheres is easy as well as the mathematics for the physics calculations. In future postings I will enhance the collision engine with more combinations of objects.

Before we write down the equations and the code let's take a look at the physics behind a collision. This is not a physics class so I will try to keep things simple, just the basics.


Some physics

In the beginning we must define our system. We assume we have two spheres moving that will eventually collide. No external forces are applied to this system. According to Newton's laws the kinetic state of the spheres remains unchanged. This means that their linear momentum remains unchanged and thus the linear momentum of the system remains stable. This is written like this:

m1v1 + m2v2 = C (constant)

This is the pre-collision state. Now let's take a look at the collision. Depending on the material of the spheres we may have from perfectly elastic collision, to a perfectly plastic collision. What do we mean with these terms? In the first case the two spheres collide and they bounce without any loss of energy. Energy loss occurs because during impact some of the kinetic energy of the objects is converted to heat. This is the usual case unless the two bodies merge in a plastic manner and the continue their journey after the impact as one.

Expressing the above in terms of kinetic energy, the elastic collision preserves it while in any other case some of it is converted to heat. The amount of energy that will be converted to heat depends on the elasticity coefficient 'e'. This take the values from 0 for plastic collision to 1 for elastic. If the velocities of the objects  velocity after the collision are u1 and u2 then we can express this as follows

e(m1v12 + m2v22) = (m1u12 + m2u22)   division by 2 in kinetic energy can be omitted (it is a common factor).


Sphere-sphere collision

Now  that we covered the general stuff let's take a close look at the main subject of this article. What happens when two spheres collide.

We start with the attempt to determine weather they collide. Here comes the comment I made in the beginning. The two spheres collide if the distance between their centers is less than the sum of their radii. The fact that the spheres are so symmetrical objects makes collision detection so simple. It also simplifies the calculation of the response to the collision.

collision

For display purposes only we will focus on 2D movement and collision. In order to solve real world 3D collisions we need a simple generalization and we are done.

In the image we see two spheres colliding. Their velocity vectors indicate that the collision occurred at some angle. This is actually the general situation. The line connecting their centers is the line of action. The forces applied on that axis actually alter the kinetic state of the two spheres. This means that in order to calculate what is going to happen we have to analyze each velocity to two components. One ON the direction of the collision axis and on perpendicular.

The components lying on the collision axis is the interesting ones. They are the ones responsible for the collision and the ones that take part in the calculations. The perpendicular components can be ignored for the response calculations. Let's call the line of action 'X' axis. Then the perpendicular axis will be 'Y'. If the initial velocities on the 'X' axis are u1x and u2x, the masses of the spheres are m1 and m2 then the resulting velocities v1x and v2x are calculated by the formulas:

v1x = ((m1-e*m2)/(m1+m2))*u1x + ((1+e)*m2/(m1+m2))*u2x

v2x = ((1+e)*m1/(m1+m2))*u1x + (m2-e*m1)/(m1+m2))*u2x

where 'e' is the elasticity factor described above.

Now all we have to do is analyze the velocities in the two components. First we calculate the direction of the 'X' axis

x = c2 - c1 where c1 and c2 are the coordinates of the centers of the spheres, and then we make it unit vector by dividing its coordinates by its length. Then the 'X' velocities are

u1x = x * (x dot u1) and

u2x = -x * (-x dot u2) where 'dot' is the dot product of the two vectors.

The 'Y' velocities will be

u1y = u1 - u1x and

u2y = u2 - u2x, and they are the same after the collision.

So the velocities of the spheres after the collision will be

v1 = v1x+u1y and

v2 = v2x+u2y

The notation so far was vector notation. If in our description of the process we change the 'Y' axis with a plane perpendicular to the line of action then we have the full 3D solution. The 'Y' components of the velocities will be on the two planes and we do not have to break them down any more since these planes are parallel and the vectors never intersect.

Download the sample game to see the solution in action. The game implements a full simulator with gravity although an option without the gravity is provided.

GusOnGames is member of Obscure Horizons group
Increase your website traffic with Attracta.com