refactor a collide detect function