Generalization of the ball-collision algorithm