Implemented collision detection.