Added function to detect collisions between elements