There isn't any ground detection. It pretty clearly just positions it about 40 degrees bellow straight ahead. And 40 degrees above for air Pokemon. It's especially evident when you have a desk or something behind you, the Pokemon will look distant.
As far as AR goes the math needed to do what they do is pretty trivial.