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

Recently, some one shows up a scripts about 2D collison.

var distx = ball1.x-ball2.x;
var disty = ball1.y-ball2.y;
var hy =Math.sqrt( (distx*distx)+(disty*disty));

var cos = distx/hy;
var sin = disty/hy;

var dVector = (ball1.dx-ball2.dx)*cos+(ball1.dy-ball2.dy)*sin;
var dvx = dVector*cos;
var dvy = dVector*sin;

ball1.dx -= dvx;
ball1.dy -= dvy;
ball2.dx += dvx;
ball2.dy += dvy;

I would guess the underlying math:

Consider the coordinate system is moving with the speed of ball2. Ball2 will be a stand still ball. Ball1 will move with a speed of (dx2-dx1) and (dy2-dy1);

When collision happens, the ball2 ball exert two force vectors: (dx2-dx1,0) and (0,dy2-dy1); These vector project along the collision line will be  Xforce*cos+Yforce*sin; (The value is "amplitude along the collision line");

So, the ball1 will get this force and ball2 will lose this force. The amplitude will be break into real vactor with x and y, so here is cos and sin again.