Optimized collision function.