var dx = _root.Balls[i]._x - _root.Balls[y]._x; var dy = _root.Balls[i]._y - _root.Balls[y]._y; if((dx*dx + dy*dy) < ((20+20) * (20+20))) // Hit { var hitangle = (Math.atan2(dx, dy)) / Math.PI * 180; var s1 = Math.sqrt(_root.Balls[i].velocity[0] * _root.Balls[i].velocity[0] + _root.Balls[i].velocity[1] * _root.Balls[i].velocity[1]); var s2 = Math.sqrt(_root.Balls[y].velocity[0] * _root.Balls[y].velocity[0] + _root.Balls[y].velocity[1] * _root.Balls[y].velocity[1]); if(s1 > 0) { _root.Balls[i].velocity[0] /= s1; _root.Balls[i].velocity[1] /= s1; } if(s2 > 0) { _root.Balls[y].velocity[0] /= s2; _root.Balls[y].velocity[1] /= s2; } var len = Math.sqrt(dx*dx + dy*dy); if (len > 0) { dx /= len; dy /= len; }