Implement collision detection function