### Collision and Response

#### Some physics

#### Sphere-sphere collision

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.

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 constant. This is written like this:

m_{1}v_{1} + m_{2}v_{2} = 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 perfect 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 perfect plastic
collision to 1 for perfect elastic. If the velocities of the objects after the collision
are u_{1} and u_{2} then we can express this as follows

e(m_{1}v_{1}^{2} + m_{2}v_{2}^{2}) =
(m_{1}u_{1}^{2} + m_{2}u_{2}^{2})
division by 2 in kinetic energy can be omitted (it is a common factor).

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.

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 u_{1x} and u_{2x}, the masses of the
spheres are m_{1} and m_{2} then the resulting velocities
v_{1x} and v_{2x} are calculated by the formulas:

v_{1x} = ((m_{1}-e*m_{2})/(m_{1}+m_{2}))*u_{1x} +
((1+e)*m_{2}/(m_{1}+m_{2}))*u_{2x}

v_{2x} = ((1+e)*m_{1}/(m_{1}+m_{2}))*u_{1x} +
(m_{2}-e*m_{1})/(m_{1}+m_{2}))*u_{2x}

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 = c_{2} - c_{1} where c_{1} and c_{2}
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

u_{1x} = x * (x dot u_{1}) and

u_{2x} = -x * (-x dot u_{2}) where 'dot' is the dot product of the two vectors.

The 'Y' velocities will be

u_{1y} = u_{1} - u_{1x} and

u_{2y} = u_{2} - u_{2x}, and they are the same after the collision.

So the velocities of the spheres after the collision will be

v_{1} = v_{1x}+u_{1y} and

v_{2} = v_{2x}+u_{2y}

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

41 Kyprou street

Peraia, GR 57019

tel.: (+30) 6944721027

e-mail: info@gusongames.com

Copyright © GusOnGames 2011-2019