 Home
 Articles
 Best Practices
 Elastic Collisions Inspiration...
Elastic Collisions Inspiration...
Alex Nino
My bio... hmmm... what to say... I arrived on this crazy world clearly by mistake on March 81, in a far, far forgotten small town with more problems than a whole nation, close to the pacific ocean in the Colombian territory, it is called Cali. Early I began playing with some gadgets like "telebolito", "intellivision", "Atari", and it was here when I started feeling interested in games and how they are being done but before start programming I've learned to play piano a guitar (I love music, can't live without it). Then I learned some programming languages such pascal with Borland Delphi, and C++ but I have to confess that it was Macromedia Flash who changed my life once we met each other in 98. I'm working with it since it didn't have support of mp3 audio format files, yes the version 3. And then together (Flash and I) started travelling around south america looking for a better job opportunity and professional development than my hometown could offer us at that moment. Special thanks to: Brazil, Costa Rica and Colombia for being a great school. At this moment I'm happily living and paying taxes in London, UK but like a salmon fish one day I will be back to procreate :)
Hi there,
Last weekend I had some spare time for writing an article I got in mind from long time ago, this is about Elastic Collisions, pretty much simple concept but apparently there are some people implementing it in the wrong way (I heard about some crazy exceptions, many bugs and problems people have) and in most of these cases it is because of they are detecting collisions in a wrong workflow. When we speak about elastic collisions we are speaking about the response of it after the collision happens and not when it actually happens (its detection), you must be careful in the way how you detect when the collisions occur, one think is detecting when the collision happens and other completely different is what happen after it.
First time I worked with elastic collisions was using action script 2, after many tests I did the performance of all these formulas were quite good due to its simplicity of how it works. The main concept is quite simple; an elastic collision is a collision between two objects (bodies which have mass) where the total momentum of the objects colliding are preserved. In other words, none of the energy involved in the collision is lost; it is distributed between the action objects depending on their mass and speed they got when the collision happened.
If you google about "elastic collisions" you may find thousands of articles and information around but unfortunately not all of this is correct, there are many people out there "as usual" explaining things in the most complicated way. I didn’t find any simple website where we can find out and understand about it easily or maybe I wasn’t lucky enough. I recommend to you take a look on the content published in Wikipedia if you want to know a bit more about it, it is really simple and well explained over there, they tell you how it works and also covers some aspects of 2 and 3 dimension collisions, the link is Elastic collision, note that the elastic collision formulas always deal with the response once the collision happens, you have to detect when the collision happen by yourself which is not hard but can be tricky to sort out when you’re facing many objects colliding at the same time.
Anyway the formula applied in one dimension (linear) is pretty much this:
and
Wait!! Don’t run away! It is so simple to interpret it, basically what it says is; if we have two objects A and B, and the object A has a mass of 3 Kilograms and it is moving with a speed of 1 meter/sec and the Object B which has a mass of 1 Kilogram is moving at 2 meters/sec, What will happen when these two objects collide? Which are the new forces (speed) for each? easy...
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
That’s all you need to know about it. Departing from this point I have made the following example using Flash Develop and the Open Source Flex SDK, the source of it is here download sourcecode. This example shows how the elastic collision can be implemented in flash in a very, very simple way; the whole program doesn’t have more than 30 instructions, I think that it really worth to have a go with it.
And then here is something interesting I found by chance at the science museum in London one day I was walking nearby and I decided to take a quick look inside. I met a machine that made me come back later with my camera and make a short video of it. This is a short video (no longer than 2 minutes) but I think enough for explaining what it is about and its connection with elastic collisions. After you watch it, you will be able to understand what the next experiment is about:
Well, when I saw that crazy machine (Energy ring by soda.co.uk) the first thing that came to my mind was if it was actually using elastic collision or not, and I was wondering if it is possible to simulate its behaviour in flash using simply elastic collisions. My approach on this experiment is totally oriented to the movement algorithm and the collision between the blocks you can see on the video, so things like the small particles flying around and the text on the display panel are not begin treated here, this is 100% oriented to the collision behaviour applying some gravity on the objects inside a circular path, I hope you see my point. Here is the sourcecode if you want to take a look on it. I think this source code is pretty much the same of the previous example apart of moving objects in a circle rather than on the x axis and colliding between these two limits (the walls), here they just collide between them and also I’m applying gravity taking advantage of the sine curve. Does it look similar to the video? I think that it is quite much what we can achieve using elastic collisions.
What about 2D elastic collisions?
Another approach of this elastic collision concept could be the collisions between balls, think about a billar pool game, the collision between the balls of a pool game is pretty much an authentic elastic collision with some extra factors that makes everything harder to achieve when coding a game such as; the friction of balls with air and on the table surface plus the spinning effect that they could have when moving that alters completely the collision response. I've been working long time ago on a game that unfortunately I never got the chance of finish. It is about an "8 ball" pool game using 15 balls and handling elastic collisions between them (without ball effects). So, here is what I have done so far, in this unfinished version I’m not making any validations about the game, it is just a free table all the time, hit what ever you want and then if put the white ball in a pocket I just put it back to the table behind the line. Unfortunately the source code is quite old, rusty and messy and is not available for downloading but I promise it will be as soon as I have a chance to tidy it up a bit. Take a look on it.
When we are speaking about physics, may be the most important thing about it, is to know when things exactly happen. It is quite important detect collisions in the right way, fast (in terms of performance) and highly responsible in terms of accuracy. The way how I'm detecting the balls collisions in the previous experiment is using the following function that by the way It doesn't use Math.sin and Math.cos functions at all which makes everything runs much faster.
//note that Ball properties vx and vy are the speed in x and y axis.
var collision_time:Number = 1;
// Compute parts of quadratic formula
// a = (v2x  v1x) ^ 2 + (v2y  v1y) ^ 2
var a:Number = Math.pow(b2.vx  b1.vx, 2) + Math.pow(b2.vy  b1.vy, 2);
// b = 2 * ((x20  x10) * (v2x  v1x) + (y20  y10) * (v2y  v1y))
var b:Number = 2. * ((b2.x  b1.x) * (b2.vx  b1.vx) + (b2.y  b1.y) * (b2.vy  b1.vy));
// c = (x20  x10) ^ 2 + (y20  y10) ^ 2  (r1 + r2) ^ 2
var c:Number = Math.pow(b2.x  b1.x, 2) + Math.pow(b2.y  b1.y, 2)  Math.pow(b1.r + b2.r, 2);
// Determinant = b^2  4ac
var det:Number = Math.pow(b,2)  (4 * a * c);
if (a != 0.) { // If a == 0 then v2x==v1x and v2y==v1y and there will be no collision
var t:Number = (b  Math.sqrt(det)) / (2. * a); // Quadratic formula. t = time to collision
if (t >= 0.) { // If collision occurs...
collision_time = t;
}
}
return collision_time; //if it is less or equal 1 it means that will be in contact before next frame
}
Here is this function in practice... drag and drop components to find out how does it work.
And then if you’re wondering what the response of the collision using elastic collisions is, here is the function I used for it and also for predicting the white ball path before you hit it:
// Compute unit normal and unit tangent vectors
// v_n = normal vec.  a vector normal to the collision surface
var v_n:Point = new Point(b2.xb1.x, b2.yb1.y);
var hyp:Number = getHypotenuse(v_n.x,v_n.y); //distance between dots
var v_un:Point = new Point(v_n.x/hyp, v_n.y/hyp); // unit normal vector
var v_ut:Point = new Point(v_un.y, v_un.x); // unit tangent vector
// Compute scalar projections of velocities onto v_un and v_ut
var v1n:Number = v_un.x * b1.vx + v_un.y * b1.vy;
var v1tPrime:Number = v_ut.x * b1.vx + v_ut.y * b1.vy;
var v2n:Number = v_un.x * b2.vx + v_un.y * b2.vy;
var v2tPrime:Number = v_ut.x * b2.vx + v_ut.y * b2.vy;
// Compute new velocities using onedimensional elastic collision equations in the normal direction
// Division by zero avoided. See early return above.
var v1nPrime:Number = (v1n * (b1.m  b2.m) + 2. * b2.m * v2n) / (b1.m + b2.m);
var v2nPrime:Number = (v2n * (b2.m  b1.m) + 2. * b1.m * v1n) / (b1.m + b2.m);
// Compute new normal and tangential velocity vectors
var v_v1nPrime:Point = new Point(v_un.x*v1nPrime, v_un.y*v1nPrime);
var v_v1tPrime:Point = new Point(v_ut.x*v1tPrime, v_ut.y*v1tPrime);
var v_v2nPrime:Point = new Point(v_un.x*v2nPrime, v_un.y*v2nPrime);
var v_v2tPrime:Point = new Point(v_ut.x*v2tPrime, v_ut.y*v2tPrime);
//update velocities.
b1.vx = v_v1nPrime.x + v_v1tPrime.x;
b1.vy = v_v1nPrime.y + v_v1tPrime.y;
b2.vx = v_v2nPrime.x + v_v2tPrime.x;
b2.vy = v_v2nPrime.y + v_v2tPrime.y;
}
These functions are quite fast in term of performance, they don’t use Math.sin and Math.cos at all, I hope this article is useful to someone, visit yoambulante.com to find out more info, cheers!
Spread The Word
23 Responses to "Elastic Collisions Inspiration..."
said this on 13 Apr 2010 3:49:28 AM CDT
Excelent! fantastic job..

said this on 13 Apr 2010 3:56:59 AM CDT
Really nice article, I li

said this on 13 Apr 2010 4:01:53 AM CDT
Very good article, very w

said this on 16 Apr 2010 12:27:14 PM CDT
All I can say is FANTASTI

said this on 17 Apr 2010 2:28:18 PM CDT
Interesting, thanks for t

said this on 20 Apr 2010 11:28:25 AM CDT
Great suff  you're a cle

said this on 22 Apr 2010 7:07:23 AM CDT
cooooooooooooooool.......

said this on 07 May 2010 11:28:06 AM CDT
Very nice articles Alex.

said this on 08 May 2010 3:43:36 AM CDT
very nice article alex...

said this on 17 Jun 2010 11:14:02 PM CDT
It's awesome, Hey I have
Tha vishnubhatla 
said this on 26 Jun 2010 6:47:02 PM CDT
Well done. You should ver

said this on 18 Jul 2010 11:33:07 AM CDT
So simple and yet so powe

said this on 25 Aug 2010 5:09:32 AM CDT
Good job Alex, waiting fo

said this on 22 Sep 2010 9:36:58 AM CDT
Awesome tutorial, trying

said this on 31 Oct 2010 2:56:13 PM CDT
well done. good job you g

said this on 02 Dec 2010 2:36:20 AM CDT
woah!! awesome . . .

said this on 02 Mar 2011 9:32:03 AM CDT
2D elastic collisions is

said this on 16 Mar 2011 2:17:55 AM CDT
Excellent work.

said this on 23 Apr 2011 6:38:12 PM CDT
Glad I've finally found s
