Make your own free website on
Calculate the sub-momentum along the collision line

The math could be very complex with trigonomy functions. Lets try a more understandable way.

Here we get a model. -- Assume the collision line angle is 0; That is horizontal. Now, the left ball goes with x speed of dx1 and y speed of dy1. OK, fortunately, the dx1 is the sub-momentum along the collision line. For the right ball, we also know the submomentum is dx2; Ok, after bounce, we exchange the submomenta. The left ball will go with x speed of dx2 and y speed of dy1;

Too simple to believe ?

But, we are not so lucky, usually the collision line is not so horizontal. Yes, but we can rotate the coordinate system to make it horizontal. We just rotate the coordinate and take advantages of localToGlobal and globalToLocal functions.

I create an empty movieClip with this code and put this dummy clip at 0,0 point.  

function bounce(clip, clip2) {
    var angle = Math.atan2(clip2.y-clip.y, clip2.x-clip.x);
    this._rotation = angle*180/Math.PI;
    var pt2 = {x:clip2.dx*10, y:clip2.dy*10};
    var pt = {x:clip.dx*10, y:clip.dy*10};
    var subVector2 = {x:pt2.x, y:0};
    var subVector = {x:pt.x, y:0};
    //This is the subvectors along the collision line
    if ((pt2.x>=0) && (pt.x<=0)) {
        //Although two balls are overlapping but this is not hit, they are separating. 
        //Dont exchange momentum in this condition. That will pull them together and stuck.
    var xSub = (subVector2.x-subVector.x)/10;
    var ySub = (subVector2.y-subVector.y)/10;
    clip.dx += xSub;
    clip.dy += ySub;
    clip2.dx -= xSub;
    clip2.dy -= ySub;


Here you might wonder why I multiply speed by 10 and later divide speed by 10 ?

These two functions: localToGlobal and globalToLocal will round our data precision to 0.05 pixel. I need the precision to 0.005, that is all. It is not necessary if you dont require high precision.

The implementation of these codes can be found in my snoocker game swf.

What about collision in 3D ? How to calculate x,y,z ?

The rule "exchanging sub-momentum with each other" is still correct. But, how to calculate the sub-momentum ? We can not use localToGlobal or globalToLocal function now. Here is the math. If you like, you can make your equation to calculate collision in 2 D and replace the localToGlobal/globalToLocal functions.

Basically, it is calculation about some side "length" of various triangle. You can forget about x,y,z. It is a calculation of A,B and C.

Click play to see the next slide.

The real codes implementation can be found in my 3dCollisio swf.