This is the java code I use in my simulation:
[code]
        r12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
cs=(x2-x1)/r12; sc=(y2-y1)/r12;
vp1=vx1*cs+vy1*sc;
vp2=vx2*cs+vy2*sc;
// back to collision time
ddt=(dmin-r12)/(vp1-vp2);
if(ddt>dt)ddt=0.;
x1-=vx1*ddt; y1-=vy1*ddt;
x2-=vx2*ddt; y2-=vy2*ddt;
r12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
//calculate component of velocity
cs=(x2-x1)/r12; sc=(y2-y1)/r12;
vp1=vx1*cs+vy1*sc;
vp2=vx2*cs+vy2*sc;
//  normal components do not change
vn1=vx1*sc-vy1*cs;
vn2=vx2*sc-vy2*cs;
      //calculate component of velocity in original X,Y coordinate
px1=vp1*cs; py1=vp1*sc;
px2=vp2*cs; py2=vp2*sc;
nx1=vn1*sc; ny1=-vn1*cs;
nx2=vn2*sc; ny2=-vn2*cs;
}
[/code]