the findNearestPoly may return a point which is outside the search box. Instead I just use the queryPolygons and filter the points inside the search box and return a closest point then I use getPolyHeight to get the projected height.
here is my code below:
dtPolyRef polys[128];
int polyCount = 0;
float nearestDistSqr = FLT_MAX;
if (dtStatusSucceed(_navquery->GetDetourQuery()->queryPolygons(pos, extend, &filter, polys, &polyCount, 128))) {
for (int i = 0; i < polyCount; ++i) {
dtPolyRef ref = polys[i];
float closestPtPoly[3];
bool posOverPoly = false;
_navquery->GetDetourQuery()->closestPointOnPoly(ref, pos, closestPtPoly, &posOverPoly);
if (dtAbs(closestPtPoly[0] - pos[0])>extend[0] || dtAbs(closestPtPoly[1] - pos[1]) > extend[1] || dtAbs(closestPtPoly[2] - pos[2]) > extend[2]) {
continue;
}
float d = dtVdistSqr(closestPtPoly, pos);
if (d < nearestDistSqr) {
nearestDistSqr = d;
poly = ref;
dtVcopy(nearest, closestPtPoly);