Elastic Collisions - Page 1
       by alex nino and kirupa  |  25 April 2010

When walking around London one day, I found a science museum and decided to drop by and take a look inside. Inside, I found this cool machine that has everything to do with what this article is going to be about, elastic collisions. Anyway, I decided to take a recording of it and you can see it below:

[ a real-life simulation of an elastic collision ]

When I saw that crazy machine, properly known as Energy Ring by soda.co.uk, the first thing that came to my mind was trying to emulate it in code. This article will provide an overview of elastic collisions and examples of code where you can see it in action.

With that said, this isn't a traditional article where I provide some code and explain it line-by-line. Instead, the goal is to provide increasingly complicated examples and the associated source files so that you can explore how elastic collisions can be used and implemented.

Some Optional Light Reading First
As you can guess, there is some level of physics knowledge required to fully understand elastic collisions. While you are not required to read the following articles, doing so will bring some clarity to the madness that you are about to be subjected to for the next two pages:

If you opted to not read the above articles, don't worry. I will provide a very brief overview of the important concepts that you need to know.

What are Elastic Collisions?
You probably know what a collision is. It is when objects collide with each other. There are two parts to a collision. The first part is the actual impact when two or more objects hit each other. The second part is what happens after they collide with each other. A collision where the total momentum of all objects before the first part is equal after the total momentum in the second part is called an elastic collision.

Notice that I am emphasizing momentum instead of energy. The reason is that momentum only cares about speed because the mass of the colliding objects isn't changing. When you talk about energy, in reality with a collision, you will lose some energy to heat, sound, and other things that will subtract from the total energy left over for dealing with movement.

Here is a very simple example of elastic collision at work where you have blocks of varying weight colliding with each other:

[ download source file (Flex / AS3) ]

Notice the reaction after the collision between the blocks. Some blocks bounce back faster than when they initially collided. Some other blocks bounce back slower.

To reiterate, the main concept of elastic collisions is quite simple. An elastic collision is a collision between two objects (bodies which have mass) where the total momentum of the objects colliding is preserved. In other words, none of the energy involved in the collision is lost. It is distributed between the objects depending on their mass and speed they had when the collision happened.

Let's say you have two objects of different masses moving towards each other:

They each have a certain speed before the collision. To put another way, they have a certain momentum going into the collision. After the collision, the speed they have as they bounce back will be something else:

How is that speed calculated? One thing we know is that the total momentum the objects have before the collision needs to be the same as the total momentum the objects have after the collision. This is an ideal case where energy isn't wasted on heat, sound, etc. Everything is purely transferred into movement.

Well...this is where some math comes into play. In the above diagram, I labeled the mass and velocity (speed) of each block. The formula for calculating the speed after a 2D collision is:

 Elastic collision formula

...and...

 Elastic collision formula

The variables u specify the speed of the object before the collision. The variables v specify the speed of the object after the collision.

Plugging in some values and converting the above formula into code, you simply have:

mass1 = 3 //mass of object A
vel1 = -1 //speed of object A (negative because of collision, opposite direction against object B)
mass2 = 1 //mass of object B
vel2 = 2 //speed of object B
bothmass = mass1 + mass2
newvel1 = (vel1 * (mass1 - mass2) + (2 * mass2 * vel2)) / bothmass
newvel2 = (vel2 * (mass2 - mass1) + (2 * mass1 * vel1)) / bothmass
//result...
//newvel1 = 0.5
//newvel2 = 2.5

Ok, now seems like a good place to take a short breather and look at more involved examples in the next page!

Onwards to the next page.


1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.