DetourNavMeshQuery.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <float.h>
  19. #include <string.h>
  20. #include "DetourNavMeshQuery.h"
  21. #include "DetourNavMesh.h"
  22. #include "DetourNode.h"
  23. #include "DetourCommon.h"
  24. #include "DetourMath.h"
  25. #include "DetourAlloc.h"
  26. #include "DetourAssert.h"
  27. #include <new>
  28. /// @class dtQueryFilter
  29. ///
  30. /// <b>The Default Implementation</b>
  31. ///
  32. /// At construction: All area costs default to 1.0. All flags are included
  33. /// and none are excluded.
  34. ///
  35. /// If a polygon has both an include and an exclude flag, it will be excluded.
  36. ///
  37. /// The way filtering works, a navigation mesh polygon must have at least one flag
  38. /// set to ever be considered by a query. So a polygon with no flags will never
  39. /// be considered.
  40. ///
  41. /// Setting the include flags to 0 will result in all polygons being excluded.
  42. ///
  43. /// <b>Custom Implementations</b>
  44. ///
  45. /// DT_VIRTUAL_QUERYFILTER must be defined in order to extend this class.
  46. ///
  47. /// Implement a custom query filter by overriding the virtual passFilter()
  48. /// and getCost() functions. If this is done, both functions should be as
  49. /// fast as possible. Use cached local copies of data rather than accessing
  50. /// your own objects where possible.
  51. ///
  52. /// Custom implementations do not need to adhere to the flags or cost logic
  53. /// used by the default implementation.
  54. ///
  55. /// In order for A* searches to work properly, the cost should be proportional to
  56. /// the travel distance. Implementing a cost modifier less than 1.0 is likely
  57. /// to lead to problems during pathfinding.
  58. ///
  59. /// @see dtNavMeshQuery
  60. dtQueryFilter::dtQueryFilter() :
  61. m_includeFlags(0xffff),
  62. m_excludeFlags(0)
  63. {
  64. for (int i = 0; i < DT_MAX_AREAS; ++i)
  65. m_areaCost[i] = 1.0f;
  66. }
  67. #ifdef DT_VIRTUAL_QUERYFILTER
  68. bool dtQueryFilter::passFilter(const dtPolyRef /*ref*/,
  69. const dtMeshTile* /*tile*/,
  70. const dtPoly* poly) const
  71. {
  72. return (poly->flags & m_includeFlags) != 0 && (poly->flags & m_excludeFlags) == 0;
  73. }
  74. float dtQueryFilter::getCost(const float* pa, const float* pb,
  75. const dtPolyRef /*prevRef*/, const dtMeshTile* /*prevTile*/, const dtPoly* /*prevPoly*/,
  76. const dtPolyRef /*curRef*/, const dtMeshTile* /*curTile*/, const dtPoly* curPoly,
  77. const dtPolyRef /*nextRef*/, const dtMeshTile* /*nextTile*/, const dtPoly* /*nextPoly*/) const
  78. {
  79. return dtVdist(pa, pb) * m_areaCost[curPoly->getArea()];
  80. }
  81. #else
  82. inline bool dtQueryFilter::passFilter(const dtPolyRef /*ref*/,
  83. const dtMeshTile* /*tile*/,
  84. const dtPoly* poly) const
  85. {
  86. return (poly->flags & m_includeFlags) != 0 && (poly->flags & m_excludeFlags) == 0;
  87. }
  88. inline float dtQueryFilter::getCost(const float* pa, const float* pb,
  89. const dtPolyRef /*prevRef*/, const dtMeshTile* /*prevTile*/, const dtPoly* /*prevPoly*/,
  90. const dtPolyRef /*curRef*/, const dtMeshTile* /*curTile*/, const dtPoly* curPoly,
  91. const dtPolyRef /*nextRef*/, const dtMeshTile* /*nextTile*/, const dtPoly* /*nextPoly*/) const
  92. {
  93. return dtVdist(pa, pb) * m_areaCost[curPoly->getArea()];
  94. }
  95. #endif
  96. static const float H_SCALE = 0.999f; // Search heuristic scale.
  97. dtNavMeshQuery* dtAllocNavMeshQuery()
  98. {
  99. void* mem = dtAlloc(sizeof(dtNavMeshQuery), DT_ALLOC_PERM);
  100. if (!mem) return 0;
  101. return new(mem) dtNavMeshQuery;
  102. }
  103. void dtFreeNavMeshQuery(dtNavMeshQuery* navmesh)
  104. {
  105. if (!navmesh) return;
  106. navmesh->~dtNavMeshQuery();
  107. dtFree(navmesh);
  108. }
  109. //////////////////////////////////////////////////////////////////////////////////////////
  110. /// @class dtNavMeshQuery
  111. ///
  112. /// For methods that support undersized buffers, if the buffer is too small
  113. /// to hold the entire result set the return status of the method will include
  114. /// the #DT_BUFFER_TOO_SMALL flag.
  115. ///
  116. /// Constant member functions can be used by multiple clients without side
  117. /// effects. (E.g. No change to the closed list. No impact on an in-progress
  118. /// sliced path query. Etc.)
  119. ///
  120. /// Walls and portals: A @e wall is a polygon segment that is
  121. /// considered impassable. A @e portal is a passable segment between polygons.
  122. /// A portal may be treated as a wall based on the dtQueryFilter used for a query.
  123. ///
  124. /// @see dtNavMesh, dtQueryFilter, #dtAllocNavMeshQuery(), #dtAllocNavMeshQuery()
  125. dtNavMeshQuery::dtNavMeshQuery() :
  126. m_nav(0),
  127. m_tinyNodePool(0),
  128. m_nodePool(0),
  129. m_openList(0)
  130. {
  131. memset(&m_query, 0, sizeof(dtQueryData));
  132. }
  133. dtNavMeshQuery::~dtNavMeshQuery()
  134. {
  135. if (m_tinyNodePool)
  136. m_tinyNodePool->~dtNodePool();
  137. if (m_nodePool)
  138. m_nodePool->~dtNodePool();
  139. if (m_openList)
  140. m_openList->~dtNodeQueue();
  141. dtFree(m_tinyNodePool);
  142. dtFree(m_nodePool);
  143. dtFree(m_openList);
  144. }
  145. /// @par
  146. ///
  147. /// Must be the first function called after construction, before other
  148. /// functions are used.
  149. ///
  150. /// This function can be used multiple times.
  151. dtStatus dtNavMeshQuery::init(const dtNavMesh* nav, const int maxNodes)
  152. {
  153. if (maxNodes > DT_NULL_IDX || maxNodes > (1 << DT_NODE_PARENT_BITS) - 1)
  154. return DT_FAILURE | DT_INVALID_PARAM;
  155. m_nav = nav;
  156. if (!m_nodePool || m_nodePool->getMaxNodes() < maxNodes)
  157. {
  158. if (m_nodePool)
  159. {
  160. m_nodePool->~dtNodePool();
  161. dtFree(m_nodePool);
  162. m_nodePool = 0;
  163. }
  164. m_nodePool = new (dtAlloc(sizeof(dtNodePool), DT_ALLOC_PERM)) dtNodePool(maxNodes, dtNextPow2(maxNodes/4));
  165. if (!m_nodePool)
  166. return DT_FAILURE | DT_OUT_OF_MEMORY;
  167. }
  168. else
  169. {
  170. m_nodePool->clear();
  171. }
  172. if (!m_tinyNodePool)
  173. {
  174. m_tinyNodePool = new (dtAlloc(sizeof(dtNodePool), DT_ALLOC_PERM)) dtNodePool(64, 32);
  175. if (!m_tinyNodePool)
  176. return DT_FAILURE | DT_OUT_OF_MEMORY;
  177. }
  178. else
  179. {
  180. m_tinyNodePool->clear();
  181. }
  182. if (!m_openList || m_openList->getCapacity() < maxNodes)
  183. {
  184. if (m_openList)
  185. {
  186. m_openList->~dtNodeQueue();
  187. dtFree(m_openList);
  188. m_openList = 0;
  189. }
  190. m_openList = new (dtAlloc(sizeof(dtNodeQueue), DT_ALLOC_PERM)) dtNodeQueue(maxNodes);
  191. if (!m_openList)
  192. return DT_FAILURE | DT_OUT_OF_MEMORY;
  193. }
  194. else
  195. {
  196. m_openList->clear();
  197. }
  198. return DT_SUCCESS;
  199. }
  200. dtStatus dtNavMeshQuery::findRandomPoint(const dtQueryFilter* filter, float (*frand)(),
  201. dtPolyRef* randomRef, float* randomPt) const
  202. {
  203. dtAssert(m_nav);
  204. // Randomly pick one tile. Assume that all tiles cover roughly the same area.
  205. const dtMeshTile* tile = 0;
  206. float tsum = 0.0f;
  207. for (int i = 0; i < m_nav->getMaxTiles(); i++)
  208. {
  209. const dtMeshTile* t = m_nav->getTile(i);
  210. if (!t || !t->header) continue;
  211. // Choose random tile using reservoi sampling.
  212. const float area = 1.0f; // Could be tile area too.
  213. tsum += area;
  214. const float u = frand();
  215. if (u*tsum <= area)
  216. tile = t;
  217. }
  218. if (!tile)
  219. return DT_FAILURE;
  220. // Randomly pick one polygon weighted by polygon area.
  221. const dtPoly* poly = 0;
  222. dtPolyRef polyRef = 0;
  223. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  224. float areaSum = 0.0f;
  225. for (int i = 0; i < tile->header->polyCount; ++i)
  226. {
  227. const dtPoly* p = &tile->polys[i];
  228. // Do not return off-mesh connection polygons.
  229. if (p->getType() != DT_POLYTYPE_GROUND)
  230. continue;
  231. // Must pass filter
  232. const dtPolyRef ref = base | (dtPolyRef)i;
  233. if (!filter->passFilter(ref, tile, p))
  234. continue;
  235. // Calc area of the polygon.
  236. float polyArea = 0.0f;
  237. for (int j = 2; j < p->vertCount; ++j)
  238. {
  239. const float* va = &tile->verts[p->verts[0]*3];
  240. const float* vb = &tile->verts[p->verts[j-1]*3];
  241. const float* vc = &tile->verts[p->verts[j]*3];
  242. polyArea += dtTriArea2D(va,vb,vc);
  243. }
  244. // Choose random polygon weighted by area, using reservoi sampling.
  245. areaSum += polyArea;
  246. const float u = frand();
  247. if (u*areaSum <= polyArea)
  248. {
  249. poly = p;
  250. polyRef = ref;
  251. }
  252. }
  253. if (!poly)
  254. return DT_FAILURE;
  255. // Randomly pick point on polygon.
  256. const float* v = &tile->verts[poly->verts[0]*3];
  257. float verts[3*DT_VERTS_PER_POLYGON];
  258. float areas[DT_VERTS_PER_POLYGON];
  259. dtVcopy(&verts[0*3],v);
  260. for (int j = 1; j < poly->vertCount; ++j)
  261. {
  262. v = &tile->verts[poly->verts[j]*3];
  263. dtVcopy(&verts[j*3],v);
  264. }
  265. const float s = frand();
  266. const float t = frand();
  267. float pt[3];
  268. dtRandomPointInConvexPoly(verts, poly->vertCount, areas, s, t, pt);
  269. float h = 0.0f;
  270. dtStatus status = getPolyHeight(polyRef, pt, &h);
  271. if (dtStatusFailed(status))
  272. return status;
  273. pt[1] = h;
  274. dtVcopy(randomPt, pt);
  275. *randomRef = polyRef;
  276. return DT_SUCCESS;
  277. }
  278. dtStatus dtNavMeshQuery::findRandomPointAroundCircle(dtPolyRef startRef, const float* centerPos, const float maxRadius,
  279. const dtQueryFilter* filter, float (*frand)(),
  280. dtPolyRef* randomRef, float* randomPt) const
  281. {
  282. dtAssert(m_nav);
  283. dtAssert(m_nodePool);
  284. dtAssert(m_openList);
  285. // Validate input
  286. if (!startRef || !m_nav->isValidPolyRef(startRef))
  287. return DT_FAILURE | DT_INVALID_PARAM;
  288. const dtMeshTile* startTile = 0;
  289. const dtPoly* startPoly = 0;
  290. m_nav->getTileAndPolyByRefUnsafe(startRef, &startTile, &startPoly);
  291. if (!filter->passFilter(startRef, startTile, startPoly))
  292. return DT_FAILURE | DT_INVALID_PARAM;
  293. m_nodePool->clear();
  294. m_openList->clear();
  295. dtNode* startNode = m_nodePool->getNode(startRef);
  296. dtVcopy(startNode->pos, centerPos);
  297. startNode->pidx = 0;
  298. startNode->cost = 0;
  299. startNode->total = 0;
  300. startNode->id = startRef;
  301. startNode->flags = DT_NODE_OPEN;
  302. m_openList->push(startNode);
  303. dtStatus status = DT_SUCCESS;
  304. const float radiusSqr = dtSqr(maxRadius);
  305. float areaSum = 0.0f;
  306. const dtMeshTile* randomTile = 0;
  307. const dtPoly* randomPoly = 0;
  308. dtPolyRef randomPolyRef = 0;
  309. while (!m_openList->empty())
  310. {
  311. dtNode* bestNode = m_openList->pop();
  312. bestNode->flags &= ~DT_NODE_OPEN;
  313. bestNode->flags |= DT_NODE_CLOSED;
  314. // Get poly and tile.
  315. // The API input has been cheked already, skip checking internal data.
  316. const dtPolyRef bestRef = bestNode->id;
  317. const dtMeshTile* bestTile = 0;
  318. const dtPoly* bestPoly = 0;
  319. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  320. // Place random locations on on ground.
  321. if (bestPoly->getType() == DT_POLYTYPE_GROUND)
  322. {
  323. // Calc area of the polygon.
  324. float polyArea = 0.0f;
  325. for (int j = 2; j < bestPoly->vertCount; ++j)
  326. {
  327. const float* va = &bestTile->verts[bestPoly->verts[0]*3];
  328. const float* vb = &bestTile->verts[bestPoly->verts[j-1]*3];
  329. const float* vc = &bestTile->verts[bestPoly->verts[j]*3];
  330. polyArea += dtTriArea2D(va,vb,vc);
  331. }
  332. // Choose random polygon weighted by area, using reservoi sampling.
  333. areaSum += polyArea;
  334. const float u = frand();
  335. if (u*areaSum <= polyArea)
  336. {
  337. randomTile = bestTile;
  338. randomPoly = bestPoly;
  339. randomPolyRef = bestRef;
  340. }
  341. }
  342. // Get parent poly and tile.
  343. dtPolyRef parentRef = 0;
  344. const dtMeshTile* parentTile = 0;
  345. const dtPoly* parentPoly = 0;
  346. if (bestNode->pidx)
  347. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  348. if (parentRef)
  349. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  350. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  351. {
  352. const dtLink* link = &bestTile->links[i];
  353. dtPolyRef neighbourRef = link->ref;
  354. // Skip invalid neighbours and do not follow back to parent.
  355. if (!neighbourRef || neighbourRef == parentRef)
  356. continue;
  357. // Expand to neighbour
  358. const dtMeshTile* neighbourTile = 0;
  359. const dtPoly* neighbourPoly = 0;
  360. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  361. // Do not advance if the polygon is excluded by the filter.
  362. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  363. continue;
  364. // Find edge and calc distance to the edge.
  365. float va[3], vb[3];
  366. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  367. continue;
  368. // If the circle is not touching the next polygon, skip it.
  369. float tseg;
  370. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  371. if (distSqr > radiusSqr)
  372. continue;
  373. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  374. if (!neighbourNode)
  375. {
  376. status |= DT_OUT_OF_NODES;
  377. continue;
  378. }
  379. if (neighbourNode->flags & DT_NODE_CLOSED)
  380. continue;
  381. // Cost
  382. if (neighbourNode->flags == 0)
  383. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  384. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  385. // The node is already in open list and the new result is worse, skip.
  386. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  387. continue;
  388. neighbourNode->id = neighbourRef;
  389. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  390. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  391. neighbourNode->total = total;
  392. if (neighbourNode->flags & DT_NODE_OPEN)
  393. {
  394. m_openList->modify(neighbourNode);
  395. }
  396. else
  397. {
  398. neighbourNode->flags = DT_NODE_OPEN;
  399. m_openList->push(neighbourNode);
  400. }
  401. }
  402. }
  403. if (!randomPoly)
  404. return DT_FAILURE;
  405. // Randomly pick point on polygon.
  406. const float* v = &randomTile->verts[randomPoly->verts[0]*3];
  407. float verts[3*DT_VERTS_PER_POLYGON];
  408. float areas[DT_VERTS_PER_POLYGON];
  409. dtVcopy(&verts[0*3],v);
  410. for (int j = 1; j < randomPoly->vertCount; ++j)
  411. {
  412. v = &randomTile->verts[randomPoly->verts[j]*3];
  413. dtVcopy(&verts[j*3],v);
  414. }
  415. const float s = frand();
  416. const float t = frand();
  417. float pt[3];
  418. dtRandomPointInConvexPoly(verts, randomPoly->vertCount, areas, s, t, pt);
  419. float h = 0.0f;
  420. dtStatus stat = getPolyHeight(randomPolyRef, pt, &h);
  421. if (dtStatusFailed(status))
  422. return stat;
  423. pt[1] = h;
  424. dtVcopy(randomPt, pt);
  425. *randomRef = randomPolyRef;
  426. return DT_SUCCESS;
  427. }
  428. //////////////////////////////////////////////////////////////////////////////////////////
  429. /// @par
  430. ///
  431. /// Uses the detail polygons to find the surface height. (Most accurate.)
  432. ///
  433. /// @p pos does not have to be within the bounds of the polygon or navigation mesh.
  434. ///
  435. /// See closestPointOnPolyBoundary() for a limited but faster option.
  436. ///
  437. dtStatus dtNavMeshQuery::closestPointOnPoly(dtPolyRef ref, const float* pos, float* closest, bool* posOverPoly) const
  438. {
  439. dtAssert(m_nav);
  440. const dtMeshTile* tile = 0;
  441. const dtPoly* poly = 0;
  442. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  443. return DT_FAILURE | DT_INVALID_PARAM;
  444. if (!tile)
  445. return DT_FAILURE | DT_INVALID_PARAM;
  446. // Off-mesh connections don't have detail polygons.
  447. if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  448. {
  449. const float* v0 = &tile->verts[poly->verts[0]*3];
  450. const float* v1 = &tile->verts[poly->verts[1]*3];
  451. const float d0 = dtVdist(pos, v0);
  452. const float d1 = dtVdist(pos, v1);
  453. const float u = d0 / (d0+d1);
  454. dtVlerp(closest, v0, v1, u);
  455. if (posOverPoly)
  456. *posOverPoly = false;
  457. return DT_SUCCESS;
  458. }
  459. const unsigned int ip = (unsigned int)(poly - tile->polys);
  460. const dtPolyDetail* pd = &tile->detailMeshes[ip];
  461. // Clamp point to be inside the polygon.
  462. float verts[DT_VERTS_PER_POLYGON*3];
  463. float edged[DT_VERTS_PER_POLYGON];
  464. float edget[DT_VERTS_PER_POLYGON];
  465. const int nv = poly->vertCount;
  466. for (int i = 0; i < nv; ++i)
  467. dtVcopy(&verts[i*3], &tile->verts[poly->verts[i]*3]);
  468. dtVcopy(closest, pos);
  469. if (!dtDistancePtPolyEdgesSqr(pos, verts, nv, edged, edget))
  470. {
  471. // Point is outside the polygon, dtClamp to nearest edge.
  472. float dmin = FLT_MAX;
  473. int imin = -1;
  474. for (int i = 0; i < nv; ++i)
  475. {
  476. if (edged[i] < dmin)
  477. {
  478. dmin = edged[i];
  479. imin = i;
  480. }
  481. }
  482. const float* va = &verts[imin*3];
  483. const float* vb = &verts[((imin+1)%nv)*3];
  484. dtVlerp(closest, va, vb, edget[imin]);
  485. if (posOverPoly)
  486. *posOverPoly = false;
  487. }
  488. else
  489. {
  490. if (posOverPoly)
  491. *posOverPoly = true;
  492. }
  493. // Find height at the location.
  494. for (int j = 0; j < pd->triCount; ++j)
  495. {
  496. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  497. const float* v[3];
  498. for (int k = 0; k < 3; ++k)
  499. {
  500. if (t[k] < poly->vertCount)
  501. v[k] = &tile->verts[poly->verts[t[k]]*3];
  502. else
  503. v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3];
  504. }
  505. float h;
  506. if (dtClosestHeightPointTriangle(pos, v[0], v[1], v[2], h))
  507. {
  508. closest[1] = h;
  509. break;
  510. }
  511. }
  512. return DT_SUCCESS;
  513. }
  514. /// @par
  515. ///
  516. /// Much faster than closestPointOnPoly().
  517. ///
  518. /// If the provided position lies within the polygon's xz-bounds (above or below),
  519. /// then @p pos and @p closest will be equal.
  520. ///
  521. /// The height of @p closest will be the polygon boundary. The height detail is not used.
  522. ///
  523. /// @p pos does not have to be within the bounds of the polybon or the navigation mesh.
  524. ///
  525. dtStatus dtNavMeshQuery::closestPointOnPolyBoundary(dtPolyRef ref, const float* pos, float* closest) const
  526. {
  527. dtAssert(m_nav);
  528. const dtMeshTile* tile = 0;
  529. const dtPoly* poly = 0;
  530. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  531. return DT_FAILURE | DT_INVALID_PARAM;
  532. // Collect vertices.
  533. float verts[DT_VERTS_PER_POLYGON*3];
  534. float edged[DT_VERTS_PER_POLYGON];
  535. float edget[DT_VERTS_PER_POLYGON];
  536. int nv = 0;
  537. for (int i = 0; i < (int)poly->vertCount; ++i)
  538. {
  539. dtVcopy(&verts[nv*3], &tile->verts[poly->verts[i]*3]);
  540. nv++;
  541. }
  542. bool inside = dtDistancePtPolyEdgesSqr(pos, verts, nv, edged, edget);
  543. if (inside)
  544. {
  545. // Point is inside the polygon, return the point.
  546. dtVcopy(closest, pos);
  547. }
  548. else
  549. {
  550. // Point is outside the polygon, dtClamp to nearest edge.
  551. float dmin = FLT_MAX;
  552. int imin = -1;
  553. for (int i = 0; i < nv; ++i)
  554. {
  555. if (edged[i] < dmin)
  556. {
  557. dmin = edged[i];
  558. imin = i;
  559. }
  560. }
  561. const float* va = &verts[imin*3];
  562. const float* vb = &verts[((imin+1)%nv)*3];
  563. dtVlerp(closest, va, vb, edget[imin]);
  564. }
  565. return DT_SUCCESS;
  566. }
  567. /// @par
  568. ///
  569. /// Will return #DT_FAILURE if the provided position is outside the xz-bounds
  570. /// of the polygon.
  571. ///
  572. dtStatus dtNavMeshQuery::getPolyHeight(dtPolyRef ref, const float* pos, float* height) const
  573. {
  574. dtAssert(m_nav);
  575. const dtMeshTile* tile = 0;
  576. const dtPoly* poly = 0;
  577. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  578. return DT_FAILURE | DT_INVALID_PARAM;
  579. if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  580. {
  581. const float* v0 = &tile->verts[poly->verts[0]*3];
  582. const float* v1 = &tile->verts[poly->verts[1]*3];
  583. const float d0 = dtVdist2D(pos, v0);
  584. const float d1 = dtVdist2D(pos, v1);
  585. const float u = d0 / (d0+d1);
  586. if (height)
  587. *height = v0[1] + (v1[1] - v0[1]) * u;
  588. return DT_SUCCESS;
  589. }
  590. else
  591. {
  592. const unsigned int ip = (unsigned int)(poly - tile->polys);
  593. const dtPolyDetail* pd = &tile->detailMeshes[ip];
  594. for (int j = 0; j < pd->triCount; ++j)
  595. {
  596. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  597. const float* v[3];
  598. for (int k = 0; k < 3; ++k)
  599. {
  600. if (t[k] < poly->vertCount)
  601. v[k] = &tile->verts[poly->verts[t[k]]*3];
  602. else
  603. v[k] = &tile->detailVerts[(pd->vertBase+(t[k]-poly->vertCount))*3];
  604. }
  605. float h;
  606. if (dtClosestHeightPointTriangle(pos, v[0], v[1], v[2], h))
  607. {
  608. if (height)
  609. *height = h;
  610. return DT_SUCCESS;
  611. }
  612. }
  613. }
  614. return DT_FAILURE | DT_INVALID_PARAM;
  615. }
  616. /// @par
  617. ///
  618. /// @note If the search box does not intersect any polygons the search will
  619. /// return #DT_SUCCESS, but @p nearestRef will be zero. So if in doubt, check
  620. /// @p nearestRef before using @p nearestPt.
  621. ///
  622. /// @warning This function is not suitable for large area searches. If the search
  623. /// extents overlaps more than MAX_SEARCH (128) polygons it may return an invalid result.
  624. ///
  625. dtStatus dtNavMeshQuery::findNearestPoly(const float* center, const float* extents,
  626. const dtQueryFilter* filter,
  627. dtPolyRef* nearestRef, float* nearestPt) const
  628. {
  629. dtAssert(m_nav);
  630. if (!nearestRef)
  631. return DT_FAILURE | DT_INVALID_PARAM;
  632. // Get nearby polygons from proximity grid.
  633. const int MAX_SEARCH = 128;
  634. dtPolyRef polys[MAX_SEARCH];
  635. int polyCount = 0;
  636. if (dtStatusFailed(queryPolygons(center, extents, filter, polys, &polyCount, MAX_SEARCH)))
  637. return DT_FAILURE | DT_INVALID_PARAM;
  638. *nearestRef = 0;
  639. if (polyCount == 0)
  640. return DT_SUCCESS;
  641. // Find nearest polygon amongst the nearby polygons.
  642. dtPolyRef nearest = 0;
  643. float nearestPoint[3];
  644. float nearestDistanceSqr = FLT_MAX;
  645. for (int i = 0; i < polyCount; ++i)
  646. {
  647. dtPolyRef ref = polys[i];
  648. float closestPtPoly[3];
  649. float diff[3];
  650. bool posOverPoly = false;
  651. float d = 0;
  652. closestPointOnPoly(ref, center, closestPtPoly, &posOverPoly);
  653. // If a point is directly over a polygon and closer than
  654. // climb height, favor that instead of straight line nearest point.
  655. dtVsub(diff, center, closestPtPoly);
  656. if (posOverPoly)
  657. {
  658. const dtMeshTile* tile = 0;
  659. const dtPoly* poly = 0;
  660. m_nav->getTileAndPolyByRefUnsafe(polys[i], &tile, &poly);
  661. d = dtAbs(diff[1]) - tile->header->walkableClimb;
  662. d = d > 0 ? d*d : 0;
  663. }
  664. else
  665. {
  666. d = dtVlenSqr(diff);
  667. }
  668. if (d < nearestDistanceSqr)
  669. {
  670. dtVcopy(nearestPoint, closestPtPoly);
  671. nearestDistanceSqr = d;
  672. nearest = ref;
  673. }
  674. }
  675. *nearestRef = nearest;
  676. if (nearestPt)
  677. dtVcopy(nearestPt, nearestPoint);
  678. return DT_SUCCESS;
  679. }
  680. int dtNavMeshQuery::queryPolygonsInTile(const dtMeshTile* tile, const float* qmin, const float* qmax,
  681. const dtQueryFilter* filter,
  682. dtPolyRef* polys, const int maxPolys) const
  683. {
  684. dtAssert(m_nav);
  685. if (tile->bvTree)
  686. {
  687. const dtBVNode* node = &tile->bvTree[0];
  688. const dtBVNode* end = &tile->bvTree[tile->header->bvNodeCount];
  689. const float* tbmin = tile->header->bmin;
  690. const float* tbmax = tile->header->bmax;
  691. const float qfac = tile->header->bvQuantFactor;
  692. // Calculate quantized box
  693. unsigned short bmin[3], bmax[3];
  694. // dtClamp query box to world box.
  695. float minx = dtClamp(qmin[0], tbmin[0], tbmax[0]) - tbmin[0];
  696. float miny = dtClamp(qmin[1], tbmin[1], tbmax[1]) - tbmin[1];
  697. float minz = dtClamp(qmin[2], tbmin[2], tbmax[2]) - tbmin[2];
  698. float maxx = dtClamp(qmax[0], tbmin[0], tbmax[0]) - tbmin[0];
  699. float maxy = dtClamp(qmax[1], tbmin[1], tbmax[1]) - tbmin[1];
  700. float maxz = dtClamp(qmax[2], tbmin[2], tbmax[2]) - tbmin[2];
  701. // Quantize
  702. bmin[0] = (unsigned short)(qfac * minx) & 0xfffe;
  703. bmin[1] = (unsigned short)(qfac * miny) & 0xfffe;
  704. bmin[2] = (unsigned short)(qfac * minz) & 0xfffe;
  705. bmax[0] = (unsigned short)(qfac * maxx + 1) | 1;
  706. bmax[1] = (unsigned short)(qfac * maxy + 1) | 1;
  707. bmax[2] = (unsigned short)(qfac * maxz + 1) | 1;
  708. // Traverse tree
  709. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  710. int n = 0;
  711. while (node < end)
  712. {
  713. const bool overlap = dtOverlapQuantBounds(bmin, bmax, node->bmin, node->bmax);
  714. const bool isLeafNode = node->i >= 0;
  715. if (isLeafNode && overlap)
  716. {
  717. dtPolyRef ref = base | (dtPolyRef)node->i;
  718. if (filter->passFilter(ref, tile, &tile->polys[node->i]))
  719. {
  720. if (n < maxPolys)
  721. polys[n++] = ref;
  722. }
  723. }
  724. if (overlap || isLeafNode)
  725. node++;
  726. else
  727. {
  728. const int escapeIndex = -node->i;
  729. node += escapeIndex;
  730. }
  731. }
  732. return n;
  733. }
  734. else
  735. {
  736. float bmin[3], bmax[3];
  737. int n = 0;
  738. const dtPolyRef base = m_nav->getPolyRefBase(tile);
  739. for (int i = 0; i < tile->header->polyCount; ++i)
  740. {
  741. const dtPoly* p = &tile->polys[i];
  742. // Do not return off-mesh connection polygons.
  743. if (p->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  744. continue;
  745. // Must pass filter
  746. const dtPolyRef ref = base | (dtPolyRef)i;
  747. if (!filter->passFilter(ref, tile, p))
  748. continue;
  749. // Calc polygon bounds.
  750. const float* v = &tile->verts[p->verts[0]*3];
  751. dtVcopy(bmin, v);
  752. dtVcopy(bmax, v);
  753. for (int j = 1; j < p->vertCount; ++j)
  754. {
  755. v = &tile->verts[p->verts[j]*3];
  756. dtVmin(bmin, v);
  757. dtVmax(bmax, v);
  758. }
  759. if (dtOverlapBounds(qmin,qmax, bmin,bmax))
  760. {
  761. if (n < maxPolys)
  762. polys[n++] = ref;
  763. }
  764. }
  765. return n;
  766. }
  767. }
  768. /// @par
  769. ///
  770. /// If no polygons are found, the function will return #DT_SUCCESS with a
  771. /// @p polyCount of zero.
  772. ///
  773. /// If @p polys is too small to hold the entire result set, then the array will
  774. /// be filled to capacity. The method of choosing which polygons from the
  775. /// full set are included in the partial result set is undefined.
  776. ///
  777. dtStatus dtNavMeshQuery::queryPolygons(const float* center, const float* extents,
  778. const dtQueryFilter* filter,
  779. dtPolyRef* polys, int* polyCount, const int maxPolys) const
  780. {
  781. dtAssert(m_nav);
  782. float bmin[3], bmax[3];
  783. dtVsub(bmin, center, extents);
  784. dtVadd(bmax, center, extents);
  785. // Find tiles the query touches.
  786. int minx, miny, maxx, maxy;
  787. m_nav->calcTileLoc(bmin, &minx, &miny);
  788. m_nav->calcTileLoc(bmax, &maxx, &maxy);
  789. static const int MAX_NEIS = 32;
  790. const dtMeshTile* neis[MAX_NEIS];
  791. int n = 0;
  792. for (int y = miny; y <= maxy; ++y)
  793. {
  794. for (int x = minx; x <= maxx; ++x)
  795. {
  796. const int nneis = m_nav->getTilesAt(x,y,neis,MAX_NEIS);
  797. for (int j = 0; j < nneis; ++j)
  798. {
  799. n += queryPolygonsInTile(neis[j], bmin, bmax, filter, polys+n, maxPolys-n);
  800. if (n >= maxPolys)
  801. {
  802. *polyCount = n;
  803. return DT_SUCCESS | DT_BUFFER_TOO_SMALL;
  804. }
  805. }
  806. }
  807. }
  808. *polyCount = n;
  809. return DT_SUCCESS;
  810. }
  811. /// @par
  812. ///
  813. /// If the end polygon cannot be reached through the navigation graph,
  814. /// the last polygon in the path will be the nearest the end polygon.
  815. ///
  816. /// If the path array is to small to hold the full result, it will be filled as
  817. /// far as possible from the start polygon toward the end polygon.
  818. ///
  819. /// The start and end positions are used to calculate traversal costs.
  820. /// (The y-values impact the result.)
  821. ///
  822. dtStatus dtNavMeshQuery::findPath(dtPolyRef startRef, dtPolyRef endRef,
  823. const float* startPos, const float* endPos,
  824. const dtQueryFilter* filter,
  825. dtPolyRef* path, int* pathCount, const int maxPath) const
  826. {
  827. dtAssert(m_nav);
  828. dtAssert(m_nodePool);
  829. dtAssert(m_openList);
  830. *pathCount = 0;
  831. if (!startRef || !endRef)
  832. return DT_FAILURE | DT_INVALID_PARAM;
  833. if (!maxPath)
  834. return DT_FAILURE | DT_INVALID_PARAM;
  835. // Validate input
  836. if (!m_nav->isValidPolyRef(startRef) || !m_nav->isValidPolyRef(endRef))
  837. return DT_FAILURE | DT_INVALID_PARAM;
  838. if (startRef == endRef)
  839. {
  840. path[0] = startRef;
  841. *pathCount = 1;
  842. return DT_SUCCESS;
  843. }
  844. m_nodePool->clear();
  845. m_openList->clear();
  846. dtNode* startNode = m_nodePool->getNode(startRef);
  847. dtVcopy(startNode->pos, startPos);
  848. startNode->pidx = 0;
  849. startNode->cost = 0;
  850. startNode->total = dtVdist(startPos, endPos) * H_SCALE;
  851. startNode->id = startRef;
  852. startNode->flags = DT_NODE_OPEN;
  853. m_openList->push(startNode);
  854. dtNode* lastBestNode = startNode;
  855. float lastBestNodeCost = startNode->total;
  856. dtStatus status = DT_SUCCESS;
  857. while (!m_openList->empty())
  858. {
  859. // Remove node from open list and put it in closed list.
  860. dtNode* bestNode = m_openList->pop();
  861. bestNode->flags &= ~DT_NODE_OPEN;
  862. bestNode->flags |= DT_NODE_CLOSED;
  863. // Reached the goal, stop searching.
  864. if (bestNode->id == endRef)
  865. {
  866. lastBestNode = bestNode;
  867. break;
  868. }
  869. // Get current poly and tile.
  870. // The API input has been cheked already, skip checking internal data.
  871. const dtPolyRef bestRef = bestNode->id;
  872. const dtMeshTile* bestTile = 0;
  873. const dtPoly* bestPoly = 0;
  874. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  875. // Get parent poly and tile.
  876. dtPolyRef parentRef = 0;
  877. const dtMeshTile* parentTile = 0;
  878. const dtPoly* parentPoly = 0;
  879. if (bestNode->pidx)
  880. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  881. if (parentRef)
  882. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  883. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  884. {
  885. dtPolyRef neighbourRef = bestTile->links[i].ref;
  886. // Skip invalid ids and do not expand back to where we came from.
  887. if (!neighbourRef || neighbourRef == parentRef)
  888. continue;
  889. // Get neighbour poly and tile.
  890. // The API input has been cheked already, skip checking internal data.
  891. const dtMeshTile* neighbourTile = 0;
  892. const dtPoly* neighbourPoly = 0;
  893. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  894. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  895. continue;
  896. // deal explicitly with crossing tile boundaries
  897. unsigned char crossSide = 0;
  898. if (bestTile->links[i].side != 0xff)
  899. crossSide = bestTile->links[i].side >> 1;
  900. // get the node
  901. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef, crossSide);
  902. if (!neighbourNode)
  903. {
  904. status |= DT_OUT_OF_NODES;
  905. continue;
  906. }
  907. // If the node is visited the first time, calculate node position.
  908. if (neighbourNode->flags == 0)
  909. {
  910. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  911. neighbourRef, neighbourPoly, neighbourTile,
  912. neighbourNode->pos);
  913. }
  914. // Calculate cost and heuristic.
  915. float cost = 0;
  916. float heuristic = 0;
  917. // Special case for last node.
  918. if (neighbourRef == endRef)
  919. {
  920. // Cost
  921. const float curCost = filter->getCost(bestNode->pos, neighbourNode->pos,
  922. parentRef, parentTile, parentPoly,
  923. bestRef, bestTile, bestPoly,
  924. neighbourRef, neighbourTile, neighbourPoly);
  925. const float endCost = filter->getCost(neighbourNode->pos, endPos,
  926. bestRef, bestTile, bestPoly,
  927. neighbourRef, neighbourTile, neighbourPoly,
  928. 0, 0, 0);
  929. cost = bestNode->cost + curCost + endCost;
  930. heuristic = 0;
  931. }
  932. else
  933. {
  934. // Cost
  935. const float curCost = filter->getCost(bestNode->pos, neighbourNode->pos,
  936. parentRef, parentTile, parentPoly,
  937. bestRef, bestTile, bestPoly,
  938. neighbourRef, neighbourTile, neighbourPoly);
  939. cost = bestNode->cost + curCost;
  940. heuristic = dtVdist(neighbourNode->pos, endPos)*H_SCALE;
  941. }
  942. const float total = cost + heuristic;
  943. // The node is already in open list and the new result is worse, skip.
  944. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  945. continue;
  946. // The node is already visited and process, and the new result is worse, skip.
  947. if ((neighbourNode->flags & DT_NODE_CLOSED) && total >= neighbourNode->total)
  948. continue;
  949. // Add or update the node.
  950. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  951. neighbourNode->id = neighbourRef;
  952. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  953. neighbourNode->cost = cost;
  954. neighbourNode->total = total;
  955. if (neighbourNode->flags & DT_NODE_OPEN)
  956. {
  957. // Already in open, update node location.
  958. m_openList->modify(neighbourNode);
  959. }
  960. else
  961. {
  962. // Put the node in open list.
  963. neighbourNode->flags |= DT_NODE_OPEN;
  964. m_openList->push(neighbourNode);
  965. }
  966. // Update nearest node to target so far.
  967. if (heuristic < lastBestNodeCost)
  968. {
  969. lastBestNodeCost = heuristic;
  970. lastBestNode = neighbourNode;
  971. }
  972. }
  973. }
  974. if (lastBestNode->id != endRef)
  975. status |= DT_PARTIAL_RESULT;
  976. // Reverse the path.
  977. dtNode* prev = 0;
  978. dtNode* node = lastBestNode;
  979. do
  980. {
  981. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  982. node->pidx = m_nodePool->getNodeIdx(prev);
  983. prev = node;
  984. node = next;
  985. }
  986. while (node);
  987. // Store path
  988. node = prev;
  989. int n = 0;
  990. do
  991. {
  992. path[n++] = node->id;
  993. if (n >= maxPath)
  994. {
  995. status |= DT_BUFFER_TOO_SMALL;
  996. break;
  997. }
  998. node = m_nodePool->getNodeAtIdx(node->pidx);
  999. }
  1000. while (node);
  1001. *pathCount = n;
  1002. return status;
  1003. }
  1004. /// @par
  1005. ///
  1006. /// @warning Calling any non-slice methods before calling finalizeSlicedFindPath()
  1007. /// or finalizeSlicedFindPathPartial() may result in corrupted data!
  1008. ///
  1009. /// The @p filter pointer is stored and used for the duration of the sliced
  1010. /// path query.
  1011. ///
  1012. dtStatus dtNavMeshQuery::initSlicedFindPath(dtPolyRef startRef, dtPolyRef endRef,
  1013. const float* startPos, const float* endPos,
  1014. const dtQueryFilter* filter, const unsigned int options)
  1015. {
  1016. dtAssert(m_nav);
  1017. dtAssert(m_nodePool);
  1018. dtAssert(m_openList);
  1019. // Init path state.
  1020. memset(&m_query, 0, sizeof(dtQueryData));
  1021. m_query.status = DT_FAILURE;
  1022. m_query.startRef = startRef;
  1023. m_query.endRef = endRef;
  1024. dtVcopy(m_query.startPos, startPos);
  1025. dtVcopy(m_query.endPos, endPos);
  1026. m_query.filter = filter;
  1027. m_query.options = options;
  1028. m_query.raycastLimitSqr = FLT_MAX;
  1029. if (!startRef || !endRef)
  1030. return DT_FAILURE | DT_INVALID_PARAM;
  1031. // Validate input
  1032. if (!m_nav->isValidPolyRef(startRef) || !m_nav->isValidPolyRef(endRef))
  1033. return DT_FAILURE | DT_INVALID_PARAM;
  1034. // trade quality with performance?
  1035. if (options & DT_FINDPATH_ANY_ANGLE)
  1036. {
  1037. // limiting to several times the character radius yields nice results. It is not sensitive
  1038. // so it is enough to compute it from the first tile.
  1039. const dtMeshTile* tile = m_nav->getTileByRef(startRef);
  1040. float agentRadius = tile->header->walkableRadius;
  1041. m_query.raycastLimitSqr = dtSqr(agentRadius * DT_RAY_CAST_LIMIT_PROPORTIONS);
  1042. }
  1043. if (startRef == endRef)
  1044. {
  1045. m_query.status = DT_SUCCESS;
  1046. return DT_SUCCESS;
  1047. }
  1048. m_nodePool->clear();
  1049. m_openList->clear();
  1050. dtNode* startNode = m_nodePool->getNode(startRef);
  1051. dtVcopy(startNode->pos, startPos);
  1052. startNode->pidx = 0;
  1053. startNode->cost = 0;
  1054. startNode->total = dtVdist(startPos, endPos) * H_SCALE;
  1055. startNode->id = startRef;
  1056. startNode->flags = DT_NODE_OPEN;
  1057. m_openList->push(startNode);
  1058. m_query.status = DT_IN_PROGRESS;
  1059. m_query.lastBestNode = startNode;
  1060. m_query.lastBestNodeCost = startNode->total;
  1061. return m_query.status;
  1062. }
  1063. dtStatus dtNavMeshQuery::updateSlicedFindPath(const int maxIter, int* doneIters)
  1064. {
  1065. if (!dtStatusInProgress(m_query.status))
  1066. return m_query.status;
  1067. // Make sure the request is still valid.
  1068. if (!m_nav->isValidPolyRef(m_query.startRef) || !m_nav->isValidPolyRef(m_query.endRef))
  1069. {
  1070. m_query.status = DT_FAILURE;
  1071. return DT_FAILURE;
  1072. }
  1073. dtRaycastHit rayHit;
  1074. rayHit.maxPath = 0;
  1075. int iter = 0;
  1076. while (iter < maxIter && !m_openList->empty())
  1077. {
  1078. iter++;
  1079. // Remove node from open list and put it in closed list.
  1080. dtNode* bestNode = m_openList->pop();
  1081. bestNode->flags &= ~DT_NODE_OPEN;
  1082. bestNode->flags |= DT_NODE_CLOSED;
  1083. // Reached the goal, stop searching.
  1084. if (bestNode->id == m_query.endRef)
  1085. {
  1086. m_query.lastBestNode = bestNode;
  1087. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1088. m_query.status = DT_SUCCESS | details;
  1089. if (doneIters)
  1090. *doneIters = iter;
  1091. return m_query.status;
  1092. }
  1093. // Get current poly and tile.
  1094. // The API input has been cheked already, skip checking internal data.
  1095. const dtPolyRef bestRef = bestNode->id;
  1096. const dtMeshTile* bestTile = 0;
  1097. const dtPoly* bestPoly = 0;
  1098. if (dtStatusFailed(m_nav->getTileAndPolyByRef(bestRef, &bestTile, &bestPoly)))
  1099. {
  1100. // The polygon has disappeared during the sliced query, fail.
  1101. m_query.status = DT_FAILURE;
  1102. if (doneIters)
  1103. *doneIters = iter;
  1104. return m_query.status;
  1105. }
  1106. // Get parent and grand parent poly and tile.
  1107. dtPolyRef parentRef = 0, grandpaRef = 0;
  1108. const dtMeshTile* parentTile = 0;
  1109. const dtPoly* parentPoly = 0;
  1110. dtNode* parentNode = 0;
  1111. if (bestNode->pidx)
  1112. {
  1113. parentNode = m_nodePool->getNodeAtIdx(bestNode->pidx);
  1114. parentRef = parentNode->id;
  1115. if (parentNode->pidx)
  1116. grandpaRef = m_nodePool->getNodeAtIdx(parentNode->pidx)->id;
  1117. }
  1118. if (parentRef)
  1119. {
  1120. bool invalidParent = dtStatusFailed(m_nav->getTileAndPolyByRef(parentRef, &parentTile, &parentPoly));
  1121. if (invalidParent || (grandpaRef && !m_nav->isValidPolyRef(grandpaRef)) )
  1122. {
  1123. // The polygon has disappeared during the sliced query, fail.
  1124. m_query.status = DT_FAILURE;
  1125. if (doneIters)
  1126. *doneIters = iter;
  1127. return m_query.status;
  1128. }
  1129. }
  1130. // decide whether to test raycast to previous nodes
  1131. bool tryLOS = false;
  1132. if (m_query.options & DT_FINDPATH_ANY_ANGLE)
  1133. {
  1134. if ((parentRef != 0) && (dtVdistSqr(parentNode->pos, bestNode->pos) < m_query.raycastLimitSqr))
  1135. tryLOS = true;
  1136. }
  1137. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  1138. {
  1139. dtPolyRef neighbourRef = bestTile->links[i].ref;
  1140. // Skip invalid ids and do not expand back to where we came from.
  1141. if (!neighbourRef || neighbourRef == parentRef)
  1142. continue;
  1143. // Get neighbour poly and tile.
  1144. // The API input has been cheked already, skip checking internal data.
  1145. const dtMeshTile* neighbourTile = 0;
  1146. const dtPoly* neighbourPoly = 0;
  1147. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  1148. if (!m_query.filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  1149. continue;
  1150. // get the neighbor node
  1151. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef, 0);
  1152. if (!neighbourNode)
  1153. {
  1154. m_query.status |= DT_OUT_OF_NODES;
  1155. continue;
  1156. }
  1157. // do not expand to nodes that were already visited from the same parent
  1158. if (neighbourNode->pidx != 0 && neighbourNode->pidx == bestNode->pidx)
  1159. continue;
  1160. // If the node is visited the first time, calculate node position.
  1161. if (neighbourNode->flags == 0)
  1162. {
  1163. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  1164. neighbourRef, neighbourPoly, neighbourTile,
  1165. neighbourNode->pos);
  1166. }
  1167. // Calculate cost and heuristic.
  1168. float cost = 0;
  1169. float heuristic = 0;
  1170. // raycast parent
  1171. bool foundShortCut = false;
  1172. rayHit.pathCost = rayHit.t = 0;
  1173. if (tryLOS)
  1174. {
  1175. raycast(parentRef, parentNode->pos, neighbourNode->pos, m_query.filter, DT_RAYCAST_USE_COSTS, &rayHit, grandpaRef);
  1176. foundShortCut = rayHit.t >= 1.0f;
  1177. }
  1178. // update move cost
  1179. if (foundShortCut)
  1180. {
  1181. // shortcut found using raycast. Using shorter cost instead
  1182. cost = parentNode->cost + rayHit.pathCost;
  1183. }
  1184. else
  1185. {
  1186. // No shortcut found.
  1187. const float curCost = m_query.filter->getCost(bestNode->pos, neighbourNode->pos,
  1188. parentRef, parentTile, parentPoly,
  1189. bestRef, bestTile, bestPoly,
  1190. neighbourRef, neighbourTile, neighbourPoly);
  1191. cost = bestNode->cost + curCost;
  1192. }
  1193. // Special case for last node.
  1194. if (neighbourRef == m_query.endRef)
  1195. {
  1196. const float endCost = m_query.filter->getCost(neighbourNode->pos, m_query.endPos,
  1197. bestRef, bestTile, bestPoly,
  1198. neighbourRef, neighbourTile, neighbourPoly,
  1199. 0, 0, 0);
  1200. cost = cost + endCost;
  1201. heuristic = 0;
  1202. }
  1203. else
  1204. {
  1205. heuristic = dtVdist(neighbourNode->pos, m_query.endPos)*H_SCALE;
  1206. }
  1207. const float total = cost + heuristic;
  1208. // The node is already in open list and the new result is worse, skip.
  1209. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  1210. continue;
  1211. // The node is already visited and process, and the new result is worse, skip.
  1212. if ((neighbourNode->flags & DT_NODE_CLOSED) && total >= neighbourNode->total)
  1213. continue;
  1214. // Add or update the node.
  1215. neighbourNode->pidx = foundShortCut ? bestNode->pidx : m_nodePool->getNodeIdx(bestNode);
  1216. neighbourNode->id = neighbourRef;
  1217. neighbourNode->flags = (neighbourNode->flags & ~(DT_NODE_CLOSED | DT_NODE_PARENT_DETACHED));
  1218. neighbourNode->cost = cost;
  1219. neighbourNode->total = total;
  1220. if (foundShortCut)
  1221. neighbourNode->flags = (neighbourNode->flags | DT_NODE_PARENT_DETACHED);
  1222. if (neighbourNode->flags & DT_NODE_OPEN)
  1223. {
  1224. // Already in open, update node location.
  1225. m_openList->modify(neighbourNode);
  1226. }
  1227. else
  1228. {
  1229. // Put the node in open list.
  1230. neighbourNode->flags |= DT_NODE_OPEN;
  1231. m_openList->push(neighbourNode);
  1232. }
  1233. // Update nearest node to target so far.
  1234. if (heuristic < m_query.lastBestNodeCost)
  1235. {
  1236. m_query.lastBestNodeCost = heuristic;
  1237. m_query.lastBestNode = neighbourNode;
  1238. }
  1239. }
  1240. }
  1241. // Exhausted all nodes, but could not find path.
  1242. if (m_openList->empty())
  1243. {
  1244. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1245. m_query.status = DT_SUCCESS | details;
  1246. }
  1247. if (doneIters)
  1248. *doneIters = iter;
  1249. return m_query.status;
  1250. }
  1251. dtStatus dtNavMeshQuery::finalizeSlicedFindPath(dtPolyRef* path, int* pathCount, const int maxPath)
  1252. {
  1253. *pathCount = 0;
  1254. if (dtStatusFailed(m_query.status))
  1255. {
  1256. // Reset query.
  1257. memset(&m_query, 0, sizeof(dtQueryData));
  1258. return DT_FAILURE;
  1259. }
  1260. int n = 0;
  1261. if (m_query.startRef == m_query.endRef)
  1262. {
  1263. // Special case: the search starts and ends at same poly.
  1264. path[n++] = m_query.startRef;
  1265. }
  1266. else
  1267. {
  1268. // Reverse the path.
  1269. dtAssert(m_query.lastBestNode);
  1270. if (m_query.lastBestNode->id != m_query.endRef)
  1271. m_query.status |= DT_PARTIAL_RESULT;
  1272. dtNode* prev = 0;
  1273. dtNode* node = m_query.lastBestNode;
  1274. int prevRay = 0;
  1275. do
  1276. {
  1277. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1278. node->pidx = m_nodePool->getNodeIdx(prev);
  1279. prev = node;
  1280. int nextRay = node->flags & DT_NODE_PARENT_DETACHED; // keep track of whether parent is not adjacent (i.e. due to raycast shortcut)
  1281. node->flags = (node->flags & ~DT_NODE_PARENT_DETACHED) | prevRay; // and store it in the reversed path's node
  1282. prevRay = nextRay;
  1283. node = next;
  1284. }
  1285. while (node);
  1286. // Store path
  1287. node = prev;
  1288. do
  1289. {
  1290. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1291. dtStatus status = 0;
  1292. if (node->flags & DT_NODE_PARENT_DETACHED)
  1293. {
  1294. float t, normal[3];
  1295. int m;
  1296. status = raycast(node->id, node->pos, next->pos, m_query.filter, &t, normal, path+n, &m, maxPath-n);
  1297. n += m;
  1298. // raycast ends on poly boundary and the path might include the next poly boundary.
  1299. if (path[n-1] == next->id)
  1300. n--; // remove to avoid duplicates
  1301. }
  1302. else
  1303. {
  1304. path[n++] = node->id;
  1305. if (n >= maxPath)
  1306. status = DT_BUFFER_TOO_SMALL;
  1307. }
  1308. if (status & DT_STATUS_DETAIL_MASK)
  1309. {
  1310. m_query.status |= status & DT_STATUS_DETAIL_MASK;
  1311. break;
  1312. }
  1313. node = next;
  1314. }
  1315. while (node);
  1316. }
  1317. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1318. // Reset query.
  1319. memset(&m_query, 0, sizeof(dtQueryData));
  1320. *pathCount = n;
  1321. return DT_SUCCESS | details;
  1322. }
  1323. dtStatus dtNavMeshQuery::finalizeSlicedFindPathPartial(const dtPolyRef* existing, const int existingSize,
  1324. dtPolyRef* path, int* pathCount, const int maxPath)
  1325. {
  1326. *pathCount = 0;
  1327. if (existingSize == 0)
  1328. {
  1329. return DT_FAILURE;
  1330. }
  1331. if (dtStatusFailed(m_query.status))
  1332. {
  1333. // Reset query.
  1334. memset(&m_query, 0, sizeof(dtQueryData));
  1335. return DT_FAILURE;
  1336. }
  1337. int n = 0;
  1338. if (m_query.startRef == m_query.endRef)
  1339. {
  1340. // Special case: the search starts and ends at same poly.
  1341. path[n++] = m_query.startRef;
  1342. }
  1343. else
  1344. {
  1345. // Find furthest existing node that was visited.
  1346. dtNode* prev = 0;
  1347. dtNode* node = 0;
  1348. for (int i = existingSize-1; i >= 0; --i)
  1349. {
  1350. m_nodePool->findNodes(existing[i], &node, 1);
  1351. if (node)
  1352. break;
  1353. }
  1354. if (!node)
  1355. {
  1356. m_query.status |= DT_PARTIAL_RESULT;
  1357. dtAssert(m_query.lastBestNode);
  1358. node = m_query.lastBestNode;
  1359. }
  1360. // Reverse the path.
  1361. int prevRay = 0;
  1362. do
  1363. {
  1364. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1365. node->pidx = m_nodePool->getNodeIdx(prev);
  1366. prev = node;
  1367. int nextRay = node->flags & DT_NODE_PARENT_DETACHED; // keep track of whether parent is not adjacent (i.e. due to raycast shortcut)
  1368. node->flags = (node->flags & ~DT_NODE_PARENT_DETACHED) | prevRay; // and store it in the reversed path's node
  1369. prevRay = nextRay;
  1370. node = next;
  1371. }
  1372. while (node);
  1373. // Store path
  1374. node = prev;
  1375. do
  1376. {
  1377. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  1378. dtStatus status = 0;
  1379. if (node->flags & DT_NODE_PARENT_DETACHED)
  1380. {
  1381. float t, normal[3];
  1382. int m;
  1383. status = raycast(node->id, node->pos, next->pos, m_query.filter, &t, normal, path+n, &m, maxPath-n);
  1384. n += m;
  1385. // raycast ends on poly boundary and the path might include the next poly boundary.
  1386. if (path[n-1] == next->id)
  1387. n--; // remove to avoid duplicates
  1388. }
  1389. else
  1390. {
  1391. path[n++] = node->id;
  1392. if (n >= maxPath)
  1393. status = DT_BUFFER_TOO_SMALL;
  1394. }
  1395. if (status & DT_STATUS_DETAIL_MASK)
  1396. {
  1397. m_query.status |= status & DT_STATUS_DETAIL_MASK;
  1398. break;
  1399. }
  1400. node = next;
  1401. }
  1402. while (node);
  1403. }
  1404. const dtStatus details = m_query.status & DT_STATUS_DETAIL_MASK;
  1405. // Reset query.
  1406. memset(&m_query, 0, sizeof(dtQueryData));
  1407. *pathCount = n;
  1408. return DT_SUCCESS | details;
  1409. }
  1410. dtStatus dtNavMeshQuery::appendVertex(const float* pos, const unsigned char flags, const dtPolyRef ref,
  1411. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1412. int* straightPathCount, const int maxStraightPath) const
  1413. {
  1414. if ((*straightPathCount) > 0 && dtVequal(&straightPath[((*straightPathCount)-1)*3], pos))
  1415. {
  1416. // The vertices are equal, update flags and poly.
  1417. if (straightPathFlags)
  1418. straightPathFlags[(*straightPathCount)-1] = flags;
  1419. if (straightPathRefs)
  1420. straightPathRefs[(*straightPathCount)-1] = ref;
  1421. }
  1422. else
  1423. {
  1424. // Append new vertex.
  1425. dtVcopy(&straightPath[(*straightPathCount)*3], pos);
  1426. if (straightPathFlags)
  1427. straightPathFlags[(*straightPathCount)] = flags;
  1428. if (straightPathRefs)
  1429. straightPathRefs[(*straightPathCount)] = ref;
  1430. (*straightPathCount)++;
  1431. // If reached end of path or there is no space to append more vertices, return.
  1432. if (flags == DT_STRAIGHTPATH_END || (*straightPathCount) >= maxStraightPath)
  1433. {
  1434. return DT_SUCCESS | (((*straightPathCount) >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1435. }
  1436. }
  1437. return DT_IN_PROGRESS;
  1438. }
  1439. dtStatus dtNavMeshQuery::appendPortals(const int startIdx, const int endIdx, const float* endPos, const dtPolyRef* path,
  1440. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1441. int* straightPathCount, const int maxStraightPath, const int options) const
  1442. {
  1443. const float* startPos = &straightPath[(*straightPathCount-1)*3];
  1444. // Append or update last vertex
  1445. dtStatus stat = 0;
  1446. for (int i = startIdx; i < endIdx; i++)
  1447. {
  1448. // Calculate portal
  1449. const dtPolyRef from = path[i];
  1450. const dtMeshTile* fromTile = 0;
  1451. const dtPoly* fromPoly = 0;
  1452. if (dtStatusFailed(m_nav->getTileAndPolyByRef(from, &fromTile, &fromPoly)))
  1453. return DT_FAILURE | DT_INVALID_PARAM;
  1454. const dtPolyRef to = path[i+1];
  1455. const dtMeshTile* toTile = 0;
  1456. const dtPoly* toPoly = 0;
  1457. if (dtStatusFailed(m_nav->getTileAndPolyByRef(to, &toTile, &toPoly)))
  1458. return DT_FAILURE | DT_INVALID_PARAM;
  1459. float left[3], right[3];
  1460. if (dtStatusFailed(getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right)))
  1461. break;
  1462. if (options & DT_STRAIGHTPATH_AREA_CROSSINGS)
  1463. {
  1464. // Skip intersection if only area crossings are requested.
  1465. if (fromPoly->getArea() == toPoly->getArea())
  1466. continue;
  1467. }
  1468. // Append intersection
  1469. float s,t;
  1470. if (dtIntersectSegSeg2D(startPos, endPos, left, right, s, t))
  1471. {
  1472. float pt[3];
  1473. dtVlerp(pt, left,right, t);
  1474. stat = appendVertex(pt, 0, path[i+1],
  1475. straightPath, straightPathFlags, straightPathRefs,
  1476. straightPathCount, maxStraightPath);
  1477. if (stat != DT_IN_PROGRESS)
  1478. return stat;
  1479. }
  1480. }
  1481. return DT_IN_PROGRESS;
  1482. }
  1483. /// @par
  1484. ///
  1485. /// This method peforms what is often called 'string pulling'.
  1486. ///
  1487. /// The start position is clamped to the first polygon in the path, and the
  1488. /// end position is clamped to the last. So the start and end positions should
  1489. /// normally be within or very near the first and last polygons respectively.
  1490. ///
  1491. /// The returned polygon references represent the reference id of the polygon
  1492. /// that is entered at the associated path position. The reference id associated
  1493. /// with the end point will always be zero. This allows, for example, matching
  1494. /// off-mesh link points to their representative polygons.
  1495. ///
  1496. /// If the provided result buffers are too small for the entire result set,
  1497. /// they will be filled as far as possible from the start toward the end
  1498. /// position.
  1499. ///
  1500. dtStatus dtNavMeshQuery::findStraightPath(const float* startPos, const float* endPos,
  1501. const dtPolyRef* path, const int pathSize,
  1502. float* straightPath, unsigned char* straightPathFlags, dtPolyRef* straightPathRefs,
  1503. int* straightPathCount, const int maxStraightPath, const int options) const
  1504. {
  1505. dtAssert(m_nav);
  1506. *straightPathCount = 0;
  1507. if (!maxStraightPath)
  1508. return DT_FAILURE | DT_INVALID_PARAM;
  1509. if (!path[0])
  1510. return DT_FAILURE | DT_INVALID_PARAM;
  1511. dtStatus stat = 0;
  1512. // TODO: Should this be callers responsibility?
  1513. float closestStartPos[3];
  1514. if (dtStatusFailed(closestPointOnPolyBoundary(path[0], startPos, closestStartPos)))
  1515. return DT_FAILURE | DT_INVALID_PARAM;
  1516. float closestEndPos[3];
  1517. if (dtStatusFailed(closestPointOnPolyBoundary(path[pathSize-1], endPos, closestEndPos)))
  1518. return DT_FAILURE | DT_INVALID_PARAM;
  1519. // Add start point.
  1520. stat = appendVertex(closestStartPos, DT_STRAIGHTPATH_START, path[0],
  1521. straightPath, straightPathFlags, straightPathRefs,
  1522. straightPathCount, maxStraightPath);
  1523. if (stat != DT_IN_PROGRESS)
  1524. return stat;
  1525. if (pathSize > 1)
  1526. {
  1527. float portalApex[3], portalLeft[3], portalRight[3];
  1528. dtVcopy(portalApex, closestStartPos);
  1529. dtVcopy(portalLeft, portalApex);
  1530. dtVcopy(portalRight, portalApex);
  1531. int apexIndex = 0;
  1532. int leftIndex = 0;
  1533. int rightIndex = 0;
  1534. unsigned char leftPolyType = 0;
  1535. unsigned char rightPolyType = 0;
  1536. dtPolyRef leftPolyRef = path[0];
  1537. dtPolyRef rightPolyRef = path[0];
  1538. for (int i = 0; i < pathSize; ++i)
  1539. {
  1540. float left[3], right[3];
  1541. unsigned char fromType, toType;
  1542. if (i+1 < pathSize)
  1543. {
  1544. // Next portal.
  1545. if (dtStatusFailed(getPortalPoints(path[i], path[i+1], left, right, fromType, toType)))
  1546. {
  1547. // Failed to get portal points, in practice this means that path[i+1] is invalid polygon.
  1548. // Clamp the end point to path[i], and return the path so far.
  1549. if (dtStatusFailed(closestPointOnPolyBoundary(path[i], endPos, closestEndPos)))
  1550. {
  1551. // This should only happen when the first polygon is invalid.
  1552. return DT_FAILURE | DT_INVALID_PARAM;
  1553. }
  1554. // Apeend portals along the current straight path segment.
  1555. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1556. {
  1557. stat = appendPortals(apexIndex, i, closestEndPos, path,
  1558. straightPath, straightPathFlags, straightPathRefs,
  1559. straightPathCount, maxStraightPath, options);
  1560. }
  1561. stat = appendVertex(closestEndPos, 0, path[i],
  1562. straightPath, straightPathFlags, straightPathRefs,
  1563. straightPathCount, maxStraightPath);
  1564. return DT_SUCCESS | DT_PARTIAL_RESULT | ((*straightPathCount >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1565. }
  1566. // If starting really close the portal, advance.
  1567. if (i == 0)
  1568. {
  1569. float t;
  1570. if (dtDistancePtSegSqr2D(portalApex, left, right, t) < dtSqr(0.001f))
  1571. continue;
  1572. }
  1573. }
  1574. else
  1575. {
  1576. // End of the path.
  1577. dtVcopy(left, closestEndPos);
  1578. dtVcopy(right, closestEndPos);
  1579. fromType = toType = DT_POLYTYPE_GROUND;
  1580. }
  1581. // Right vertex.
  1582. if (dtTriArea2D(portalApex, portalRight, right) <= 0.0f)
  1583. {
  1584. if (dtVequal(portalApex, portalRight) || dtTriArea2D(portalApex, portalLeft, right) > 0.0f)
  1585. {
  1586. dtVcopy(portalRight, right);
  1587. rightPolyRef = (i+1 < pathSize) ? path[i+1] : 0;
  1588. rightPolyType = toType;
  1589. rightIndex = i;
  1590. }
  1591. else
  1592. {
  1593. // Append portals along the current straight path segment.
  1594. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1595. {
  1596. stat = appendPortals(apexIndex, leftIndex, portalLeft, path,
  1597. straightPath, straightPathFlags, straightPathRefs,
  1598. straightPathCount, maxStraightPath, options);
  1599. if (stat != DT_IN_PROGRESS)
  1600. return stat;
  1601. }
  1602. dtVcopy(portalApex, portalLeft);
  1603. apexIndex = leftIndex;
  1604. unsigned char flags = 0;
  1605. if (!leftPolyRef)
  1606. flags = DT_STRAIGHTPATH_END;
  1607. else if (leftPolyType == DT_POLYTYPE_OFFMESH_CONNECTION)
  1608. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1609. dtPolyRef ref = leftPolyRef;
  1610. // Append or update vertex
  1611. stat = appendVertex(portalApex, flags, ref,
  1612. straightPath, straightPathFlags, straightPathRefs,
  1613. straightPathCount, maxStraightPath);
  1614. if (stat != DT_IN_PROGRESS)
  1615. return stat;
  1616. dtVcopy(portalLeft, portalApex);
  1617. dtVcopy(portalRight, portalApex);
  1618. leftIndex = apexIndex;
  1619. rightIndex = apexIndex;
  1620. // Restart
  1621. i = apexIndex;
  1622. continue;
  1623. }
  1624. }
  1625. // Left vertex.
  1626. if (dtTriArea2D(portalApex, portalLeft, left) >= 0.0f)
  1627. {
  1628. if (dtVequal(portalApex, portalLeft) || dtTriArea2D(portalApex, portalRight, left) < 0.0f)
  1629. {
  1630. dtVcopy(portalLeft, left);
  1631. leftPolyRef = (i+1 < pathSize) ? path[i+1] : 0;
  1632. leftPolyType = toType;
  1633. leftIndex = i;
  1634. }
  1635. else
  1636. {
  1637. // Append portals along the current straight path segment.
  1638. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1639. {
  1640. stat = appendPortals(apexIndex, rightIndex, portalRight, path,
  1641. straightPath, straightPathFlags, straightPathRefs,
  1642. straightPathCount, maxStraightPath, options);
  1643. if (stat != DT_IN_PROGRESS)
  1644. return stat;
  1645. }
  1646. dtVcopy(portalApex, portalRight);
  1647. apexIndex = rightIndex;
  1648. unsigned char flags = 0;
  1649. if (!rightPolyRef)
  1650. flags = DT_STRAIGHTPATH_END;
  1651. else if (rightPolyType == DT_POLYTYPE_OFFMESH_CONNECTION)
  1652. flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
  1653. dtPolyRef ref = rightPolyRef;
  1654. // Append or update vertex
  1655. stat = appendVertex(portalApex, flags, ref,
  1656. straightPath, straightPathFlags, straightPathRefs,
  1657. straightPathCount, maxStraightPath);
  1658. if (stat != DT_IN_PROGRESS)
  1659. return stat;
  1660. dtVcopy(portalLeft, portalApex);
  1661. dtVcopy(portalRight, portalApex);
  1662. leftIndex = apexIndex;
  1663. rightIndex = apexIndex;
  1664. // Restart
  1665. i = apexIndex;
  1666. continue;
  1667. }
  1668. }
  1669. }
  1670. // Append portals along the current straight path segment.
  1671. if (options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS))
  1672. {
  1673. stat = appendPortals(apexIndex, pathSize-1, closestEndPos, path,
  1674. straightPath, straightPathFlags, straightPathRefs,
  1675. straightPathCount, maxStraightPath, options);
  1676. if (stat != DT_IN_PROGRESS)
  1677. return stat;
  1678. }
  1679. }
  1680. stat = appendVertex(closestEndPos, DT_STRAIGHTPATH_END, 0,
  1681. straightPath, straightPathFlags, straightPathRefs,
  1682. straightPathCount, maxStraightPath);
  1683. return DT_SUCCESS | ((*straightPathCount >= maxStraightPath) ? DT_BUFFER_TOO_SMALL : 0);
  1684. }
  1685. /// @par
  1686. ///
  1687. /// This method is optimized for small delta movement and a small number of
  1688. /// polygons. If used for too great a distance, the result set will form an
  1689. /// incomplete path.
  1690. ///
  1691. /// @p resultPos will equal the @p endPos if the end is reached.
  1692. /// Otherwise the closest reachable position will be returned.
  1693. ///
  1694. /// @p resultPos is not projected onto the surface of the navigation
  1695. /// mesh. Use #getPolyHeight if this is needed.
  1696. ///
  1697. /// This method treats the end position in the same manner as
  1698. /// the #raycast method. (As a 2D point.) See that method's documentation
  1699. /// for details.
  1700. ///
  1701. /// If the @p visited array is too small to hold the entire result set, it will
  1702. /// be filled as far as possible from the start position toward the end
  1703. /// position.
  1704. ///
  1705. dtStatus dtNavMeshQuery::moveAlongSurface(dtPolyRef startRef, const float* startPos, const float* endPos,
  1706. const dtQueryFilter* filter,
  1707. float* resultPos, dtPolyRef* visited, int* visitedCount, const int maxVisitedSize) const
  1708. {
  1709. dtAssert(m_nav);
  1710. dtAssert(m_tinyNodePool);
  1711. *visitedCount = 0;
  1712. // Validate input
  1713. if (!startRef)
  1714. return DT_FAILURE | DT_INVALID_PARAM;
  1715. if (!m_nav->isValidPolyRef(startRef))
  1716. return DT_FAILURE | DT_INVALID_PARAM;
  1717. dtStatus status = DT_SUCCESS;
  1718. static const int MAX_STACK = 48;
  1719. dtNode* stack[MAX_STACK];
  1720. int nstack = 0;
  1721. m_tinyNodePool->clear();
  1722. dtNode* startNode = m_tinyNodePool->getNode(startRef);
  1723. startNode->pidx = 0;
  1724. startNode->cost = 0;
  1725. startNode->total = 0;
  1726. startNode->id = startRef;
  1727. startNode->flags = DT_NODE_CLOSED;
  1728. stack[nstack++] = startNode;
  1729. float bestPos[3];
  1730. float bestDist = FLT_MAX;
  1731. dtNode* bestNode = 0;
  1732. dtVcopy(bestPos, startPos);
  1733. // Search constraints
  1734. float searchPos[3], searchRadSqr;
  1735. dtVlerp(searchPos, startPos, endPos, 0.5f);
  1736. searchRadSqr = dtSqr(dtVdist(startPos, endPos)/2.0f + 0.001f);
  1737. float verts[DT_VERTS_PER_POLYGON*3];
  1738. while (nstack)
  1739. {
  1740. // Pop front.
  1741. dtNode* curNode = stack[0];
  1742. for (int i = 0; i < nstack-1; ++i)
  1743. stack[i] = stack[i+1];
  1744. nstack--;
  1745. // Get poly and tile.
  1746. // The API input has been cheked already, skip checking internal data.
  1747. const dtPolyRef curRef = curNode->id;
  1748. const dtMeshTile* curTile = 0;
  1749. const dtPoly* curPoly = 0;
  1750. m_nav->getTileAndPolyByRefUnsafe(curRef, &curTile, &curPoly);
  1751. // Collect vertices.
  1752. const int nverts = curPoly->vertCount;
  1753. for (int i = 0; i < nverts; ++i)
  1754. dtVcopy(&verts[i*3], &curTile->verts[curPoly->verts[i]*3]);
  1755. // If target is inside the poly, stop search.
  1756. if (dtPointInPolygon(endPos, verts, nverts))
  1757. {
  1758. bestNode = curNode;
  1759. dtVcopy(bestPos, endPos);
  1760. break;
  1761. }
  1762. // Find wall edges and find nearest point inside the walls.
  1763. for (int i = 0, j = (int)curPoly->vertCount-1; i < (int)curPoly->vertCount; j = i++)
  1764. {
  1765. // Find links to neighbours.
  1766. static const int MAX_NEIS = 8;
  1767. int nneis = 0;
  1768. dtPolyRef neis[MAX_NEIS];
  1769. if (curPoly->neis[j] & DT_EXT_LINK)
  1770. {
  1771. // Tile border.
  1772. for (unsigned int k = curPoly->firstLink; k != DT_NULL_LINK; k = curTile->links[k].next)
  1773. {
  1774. const dtLink* link = &curTile->links[k];
  1775. if (link->edge == j)
  1776. {
  1777. if (link->ref != 0)
  1778. {
  1779. const dtMeshTile* neiTile = 0;
  1780. const dtPoly* neiPoly = 0;
  1781. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  1782. if (filter->passFilter(link->ref, neiTile, neiPoly))
  1783. {
  1784. if (nneis < MAX_NEIS)
  1785. neis[nneis++] = link->ref;
  1786. }
  1787. }
  1788. }
  1789. }
  1790. }
  1791. else if (curPoly->neis[j])
  1792. {
  1793. const unsigned int idx = (unsigned int)(curPoly->neis[j]-1);
  1794. const dtPolyRef ref = m_nav->getPolyRefBase(curTile) | idx;
  1795. if (filter->passFilter(ref, curTile, &curTile->polys[idx]))
  1796. {
  1797. // Internal edge, encode id.
  1798. neis[nneis++] = ref;
  1799. }
  1800. }
  1801. if (!nneis)
  1802. {
  1803. // Wall edge, calc distance.
  1804. const float* vj = &verts[j*3];
  1805. const float* vi = &verts[i*3];
  1806. float tseg;
  1807. const float distSqr = dtDistancePtSegSqr2D(endPos, vj, vi, tseg);
  1808. if (distSqr < bestDist)
  1809. {
  1810. // Update nearest distance.
  1811. dtVlerp(bestPos, vj,vi, tseg);
  1812. bestDist = distSqr;
  1813. bestNode = curNode;
  1814. }
  1815. }
  1816. else
  1817. {
  1818. for (int k = 0; k < nneis; ++k)
  1819. {
  1820. // Skip if no node can be allocated.
  1821. dtNode* neighbourNode = m_tinyNodePool->getNode(neis[k]);
  1822. if (!neighbourNode)
  1823. continue;
  1824. // Skip if already visited.
  1825. if (neighbourNode->flags & DT_NODE_CLOSED)
  1826. continue;
  1827. // Skip the link if it is too far from search constraint.
  1828. // TODO: Maybe should use getPortalPoints(), but this one is way faster.
  1829. const float* vj = &verts[j*3];
  1830. const float* vi = &verts[i*3];
  1831. float tseg;
  1832. float distSqr = dtDistancePtSegSqr2D(searchPos, vj, vi, tseg);
  1833. if (distSqr > searchRadSqr)
  1834. continue;
  1835. // Mark as the node as visited and push to queue.
  1836. if (nstack < MAX_STACK)
  1837. {
  1838. neighbourNode->pidx = m_tinyNodePool->getNodeIdx(curNode);
  1839. neighbourNode->flags |= DT_NODE_CLOSED;
  1840. stack[nstack++] = neighbourNode;
  1841. }
  1842. }
  1843. }
  1844. }
  1845. }
  1846. int n = 0;
  1847. if (bestNode)
  1848. {
  1849. // Reverse the path.
  1850. dtNode* prev = 0;
  1851. dtNode* node = bestNode;
  1852. do
  1853. {
  1854. dtNode* next = m_tinyNodePool->getNodeAtIdx(node->pidx);
  1855. node->pidx = m_tinyNodePool->getNodeIdx(prev);
  1856. prev = node;
  1857. node = next;
  1858. }
  1859. while (node);
  1860. // Store result
  1861. node = prev;
  1862. do
  1863. {
  1864. visited[n++] = node->id;
  1865. if (n >= maxVisitedSize)
  1866. {
  1867. status |= DT_BUFFER_TOO_SMALL;
  1868. break;
  1869. }
  1870. node = m_tinyNodePool->getNodeAtIdx(node->pidx);
  1871. }
  1872. while (node);
  1873. }
  1874. dtVcopy(resultPos, bestPos);
  1875. *visitedCount = n;
  1876. return status;
  1877. }
  1878. dtStatus dtNavMeshQuery::getPortalPoints(dtPolyRef from, dtPolyRef to, float* left, float* right,
  1879. unsigned char& fromType, unsigned char& toType) const
  1880. {
  1881. dtAssert(m_nav);
  1882. const dtMeshTile* fromTile = 0;
  1883. const dtPoly* fromPoly = 0;
  1884. if (dtStatusFailed(m_nav->getTileAndPolyByRef(from, &fromTile, &fromPoly)))
  1885. return DT_FAILURE | DT_INVALID_PARAM;
  1886. fromType = fromPoly->getType();
  1887. const dtMeshTile* toTile = 0;
  1888. const dtPoly* toPoly = 0;
  1889. if (dtStatusFailed(m_nav->getTileAndPolyByRef(to, &toTile, &toPoly)))
  1890. return DT_FAILURE | DT_INVALID_PARAM;
  1891. toType = toPoly->getType();
  1892. return getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right);
  1893. }
  1894. // Returns portal points between two polygons.
  1895. dtStatus dtNavMeshQuery::getPortalPoints(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  1896. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  1897. float* left, float* right) const
  1898. {
  1899. // Find the link that points to the 'to' polygon.
  1900. const dtLink* link = 0;
  1901. for (unsigned int i = fromPoly->firstLink; i != DT_NULL_LINK; i = fromTile->links[i].next)
  1902. {
  1903. if (fromTile->links[i].ref == to)
  1904. {
  1905. link = &fromTile->links[i];
  1906. break;
  1907. }
  1908. }
  1909. if (!link)
  1910. return DT_FAILURE | DT_INVALID_PARAM;
  1911. // Handle off-mesh connections.
  1912. if (fromPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  1913. {
  1914. // Find link that points to first vertex.
  1915. for (unsigned int i = fromPoly->firstLink; i != DT_NULL_LINK; i = fromTile->links[i].next)
  1916. {
  1917. if (fromTile->links[i].ref == to)
  1918. {
  1919. const int v = fromTile->links[i].edge;
  1920. dtVcopy(left, &fromTile->verts[fromPoly->verts[v]*3]);
  1921. dtVcopy(right, &fromTile->verts[fromPoly->verts[v]*3]);
  1922. return DT_SUCCESS;
  1923. }
  1924. }
  1925. return DT_FAILURE | DT_INVALID_PARAM;
  1926. }
  1927. if (toPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  1928. {
  1929. for (unsigned int i = toPoly->firstLink; i != DT_NULL_LINK; i = toTile->links[i].next)
  1930. {
  1931. if (toTile->links[i].ref == from)
  1932. {
  1933. const int v = toTile->links[i].edge;
  1934. dtVcopy(left, &toTile->verts[toPoly->verts[v]*3]);
  1935. dtVcopy(right, &toTile->verts[toPoly->verts[v]*3]);
  1936. return DT_SUCCESS;
  1937. }
  1938. }
  1939. return DT_FAILURE | DT_INVALID_PARAM;
  1940. }
  1941. // Find portal vertices.
  1942. const int v0 = fromPoly->verts[link->edge];
  1943. const int v1 = fromPoly->verts[(link->edge+1) % (int)fromPoly->vertCount];
  1944. dtVcopy(left, &fromTile->verts[v0*3]);
  1945. dtVcopy(right, &fromTile->verts[v1*3]);
  1946. // If the link is at tile boundary, dtClamp the vertices to
  1947. // the link width.
  1948. if (link->side != 0xff)
  1949. {
  1950. // Unpack portal limits.
  1951. if (link->bmin != 0 || link->bmax != 255)
  1952. {
  1953. const float s = 1.0f/255.0f;
  1954. const float tmin = link->bmin*s;
  1955. const float tmax = link->bmax*s;
  1956. dtVlerp(left, &fromTile->verts[v0*3], &fromTile->verts[v1*3], tmin);
  1957. dtVlerp(right, &fromTile->verts[v0*3], &fromTile->verts[v1*3], tmax);
  1958. }
  1959. }
  1960. return DT_SUCCESS;
  1961. }
  1962. // Returns edge mid point between two polygons.
  1963. dtStatus dtNavMeshQuery::getEdgeMidPoint(dtPolyRef from, dtPolyRef to, float* mid) const
  1964. {
  1965. float left[3], right[3];
  1966. unsigned char fromType, toType;
  1967. if (dtStatusFailed(getPortalPoints(from, to, left,right, fromType, toType)))
  1968. return DT_FAILURE | DT_INVALID_PARAM;
  1969. mid[0] = (left[0]+right[0])*0.5f;
  1970. mid[1] = (left[1]+right[1])*0.5f;
  1971. mid[2] = (left[2]+right[2])*0.5f;
  1972. return DT_SUCCESS;
  1973. }
  1974. dtStatus dtNavMeshQuery::getEdgeMidPoint(dtPolyRef from, const dtPoly* fromPoly, const dtMeshTile* fromTile,
  1975. dtPolyRef to, const dtPoly* toPoly, const dtMeshTile* toTile,
  1976. float* mid) const
  1977. {
  1978. float left[3], right[3];
  1979. if (dtStatusFailed(getPortalPoints(from, fromPoly, fromTile, to, toPoly, toTile, left, right)))
  1980. return DT_FAILURE | DT_INVALID_PARAM;
  1981. mid[0] = (left[0]+right[0])*0.5f;
  1982. mid[1] = (left[1]+right[1])*0.5f;
  1983. mid[2] = (left[2]+right[2])*0.5f;
  1984. return DT_SUCCESS;
  1985. }
  1986. /// @par
  1987. ///
  1988. /// This method is meant to be used for quick, short distance checks.
  1989. ///
  1990. /// If the path array is too small to hold the result, it will be filled as
  1991. /// far as possible from the start postion toward the end position.
  1992. ///
  1993. /// <b>Using the Hit Parameter (t)</b>
  1994. ///
  1995. /// If the hit parameter is a very high value (FLT_MAX), then the ray has hit
  1996. /// the end position. In this case the path represents a valid corridor to the
  1997. /// end position and the value of @p hitNormal is undefined.
  1998. ///
  1999. /// If the hit parameter is zero, then the start position is on the wall that
  2000. /// was hit and the value of @p hitNormal is undefined.
  2001. ///
  2002. /// If 0 < t < 1.0 then the following applies:
  2003. ///
  2004. /// @code
  2005. /// distanceToHitBorder = distanceToEndPosition * t
  2006. /// hitPoint = startPos + (endPos - startPos) * t
  2007. /// @endcode
  2008. ///
  2009. /// <b>Use Case Restriction</b>
  2010. ///
  2011. /// The raycast ignores the y-value of the end position. (2D check.) This
  2012. /// places significant limits on how it can be used. For example:
  2013. ///
  2014. /// Consider a scene where there is a main floor with a second floor balcony
  2015. /// that hangs over the main floor. So the first floor mesh extends below the
  2016. /// balcony mesh. The start position is somewhere on the first floor. The end
  2017. /// position is on the balcony.
  2018. ///
  2019. /// The raycast will search toward the end position along the first floor mesh.
  2020. /// If it reaches the end position's xz-coordinates it will indicate FLT_MAX
  2021. /// (no wall hit), meaning it reached the end position. This is one example of why
  2022. /// this method is meant for short distance checks.
  2023. ///
  2024. dtStatus dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
  2025. const dtQueryFilter* filter,
  2026. float* t, float* hitNormal, dtPolyRef* path, int* pathCount, const int maxPath) const
  2027. {
  2028. dtRaycastHit hit;
  2029. hit.path = path;
  2030. hit.maxPath = maxPath;
  2031. dtStatus status = raycast(startRef, startPos, endPos, filter, 0, &hit);
  2032. *t = hit.t;
  2033. if (hitNormal)
  2034. dtVcopy(hitNormal, hit.hitNormal);
  2035. if (pathCount)
  2036. *pathCount = hit.pathCount;
  2037. return status;
  2038. }
  2039. /// @par
  2040. ///
  2041. /// This method is meant to be used for quick, short distance checks.
  2042. ///
  2043. /// If the path array is too small to hold the result, it will be filled as
  2044. /// far as possible from the start postion toward the end position.
  2045. ///
  2046. /// <b>Using the Hit Parameter t of RaycastHit</b>
  2047. ///
  2048. /// If the hit parameter is a very high value (FLT_MAX), then the ray has hit
  2049. /// the end position. In this case the path represents a valid corridor to the
  2050. /// end position and the value of @p hitNormal is undefined.
  2051. ///
  2052. /// If the hit parameter is zero, then the start position is on the wall that
  2053. /// was hit and the value of @p hitNormal is undefined.
  2054. ///
  2055. /// If 0 < t < 1.0 then the following applies:
  2056. ///
  2057. /// @code
  2058. /// distanceToHitBorder = distanceToEndPosition * t
  2059. /// hitPoint = startPos + (endPos - startPos) * t
  2060. /// @endcode
  2061. ///
  2062. /// <b>Use Case Restriction</b>
  2063. ///
  2064. /// The raycast ignores the y-value of the end position. (2D check.) This
  2065. /// places significant limits on how it can be used. For example:
  2066. ///
  2067. /// Consider a scene where there is a main floor with a second floor balcony
  2068. /// that hangs over the main floor. So the first floor mesh extends below the
  2069. /// balcony mesh. The start position is somewhere on the first floor. The end
  2070. /// position is on the balcony.
  2071. ///
  2072. /// The raycast will search toward the end position along the first floor mesh.
  2073. /// If it reaches the end position's xz-coordinates it will indicate FLT_MAX
  2074. /// (no wall hit), meaning it reached the end position. This is one example of why
  2075. /// this method is meant for short distance checks.
  2076. ///
  2077. dtStatus dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos,
  2078. const dtQueryFilter* filter, const unsigned int options,
  2079. dtRaycastHit* hit, dtPolyRef prevRef) const
  2080. {
  2081. dtAssert(m_nav);
  2082. hit->t = 0;
  2083. hit->pathCount = 0;
  2084. hit->pathCost = 0;
  2085. // Validate input
  2086. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2087. return DT_FAILURE | DT_INVALID_PARAM;
  2088. if (prevRef && !m_nav->isValidPolyRef(prevRef))
  2089. return DT_FAILURE | DT_INVALID_PARAM;
  2090. float dir[3], curPos[3], lastPos[3];
  2091. float verts[DT_VERTS_PER_POLYGON*3+3];
  2092. int n = 0;
  2093. dtVcopy(curPos, startPos);
  2094. dtVsub(dir, endPos, startPos);
  2095. dtVset(hit->hitNormal, 0, 0, 0);
  2096. dtStatus status = DT_SUCCESS;
  2097. const dtMeshTile* prevTile, *tile, *nextTile;
  2098. const dtPoly* prevPoly, *poly, *nextPoly;
  2099. dtPolyRef curRef, nextRef;
  2100. // The API input has been checked already, skip checking internal data.
  2101. nextRef = curRef = startRef;
  2102. tile = 0;
  2103. poly = 0;
  2104. m_nav->getTileAndPolyByRefUnsafe(curRef, &tile, &poly);
  2105. nextTile = prevTile = tile;
  2106. nextPoly = prevPoly = poly;
  2107. if (prevRef)
  2108. m_nav->getTileAndPolyByRefUnsafe(prevRef, &prevTile, &prevPoly);
  2109. while (curRef)
  2110. {
  2111. // Cast ray against current polygon.
  2112. // Collect vertices.
  2113. int nv = 0;
  2114. for (int i = 0; i < (int)poly->vertCount; ++i)
  2115. {
  2116. dtVcopy(&verts[nv*3], &tile->verts[poly->verts[i]*3]);
  2117. nv++;
  2118. }
  2119. float tmin, tmax;
  2120. int segMin, segMax;
  2121. if (!dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax))
  2122. {
  2123. // Could not hit the polygon, keep the old t and report hit.
  2124. hit->pathCount = n;
  2125. return status;
  2126. }
  2127. hit->hitEdgeIndex = segMax;
  2128. // Keep track of furthest t so far.
  2129. if (tmax > hit->t)
  2130. hit->t = tmax;
  2131. // Store visited polygons.
  2132. if (n < hit->maxPath)
  2133. hit->path[n++] = curRef;
  2134. else
  2135. status |= DT_BUFFER_TOO_SMALL;
  2136. // Ray end is completely inside the polygon.
  2137. if (segMax == -1)
  2138. {
  2139. hit->t = FLT_MAX;
  2140. hit->pathCount = n;
  2141. // add the cost
  2142. if (options & DT_RAYCAST_USE_COSTS)
  2143. hit->pathCost += filter->getCost(curPos, endPos, prevRef, prevTile, prevPoly, curRef, tile, poly, curRef, tile, poly);
  2144. return status;
  2145. }
  2146. // Follow neighbours.
  2147. nextRef = 0;
  2148. for (unsigned int i = poly->firstLink; i != DT_NULL_LINK; i = tile->links[i].next)
  2149. {
  2150. const dtLink* link = &tile->links[i];
  2151. // Find link which contains this edge.
  2152. if ((int)link->edge != segMax)
  2153. continue;
  2154. // Get pointer to the next polygon.
  2155. nextTile = 0;
  2156. nextPoly = 0;
  2157. m_nav->getTileAndPolyByRefUnsafe(link->ref, &nextTile, &nextPoly);
  2158. // Skip off-mesh connections.
  2159. if (nextPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  2160. continue;
  2161. // Skip links based on filter.
  2162. if (!filter->passFilter(link->ref, nextTile, nextPoly))
  2163. continue;
  2164. // If the link is internal, just return the ref.
  2165. if (link->side == 0xff)
  2166. {
  2167. nextRef = link->ref;
  2168. break;
  2169. }
  2170. // If the link is at tile boundary,
  2171. // Check if the link spans the whole edge, and accept.
  2172. if (link->bmin == 0 && link->bmax == 255)
  2173. {
  2174. nextRef = link->ref;
  2175. break;
  2176. }
  2177. // Check for partial edge links.
  2178. const int v0 = poly->verts[link->edge];
  2179. const int v1 = poly->verts[(link->edge+1) % poly->vertCount];
  2180. const float* left = &tile->verts[v0*3];
  2181. const float* right = &tile->verts[v1*3];
  2182. // Check that the intersection lies inside the link portal.
  2183. if (link->side == 0 || link->side == 4)
  2184. {
  2185. // Calculate link size.
  2186. const float s = 1.0f/255.0f;
  2187. float lmin = left[2] + (right[2] - left[2])*(link->bmin*s);
  2188. float lmax = left[2] + (right[2] - left[2])*(link->bmax*s);
  2189. if (lmin > lmax) dtSwap(lmin, lmax);
  2190. // Find Z intersection.
  2191. float z = startPos[2] + (endPos[2]-startPos[2])*tmax;
  2192. if (z >= lmin && z <= lmax)
  2193. {
  2194. nextRef = link->ref;
  2195. break;
  2196. }
  2197. }
  2198. else if (link->side == 2 || link->side == 6)
  2199. {
  2200. // Calculate link size.
  2201. const float s = 1.0f/255.0f;
  2202. float lmin = left[0] + (right[0] - left[0])*(link->bmin*s);
  2203. float lmax = left[0] + (right[0] - left[0])*(link->bmax*s);
  2204. if (lmin > lmax) dtSwap(lmin, lmax);
  2205. // Find X intersection.
  2206. float x = startPos[0] + (endPos[0]-startPos[0])*tmax;
  2207. if (x >= lmin && x <= lmax)
  2208. {
  2209. nextRef = link->ref;
  2210. break;
  2211. }
  2212. }
  2213. }
  2214. // add the cost
  2215. if (options & DT_RAYCAST_USE_COSTS)
  2216. {
  2217. // compute the intersection point at the furthest end of the polygon
  2218. // and correct the height (since the raycast moves in 2d)
  2219. dtVcopy(lastPos, curPos);
  2220. dtVmad(curPos, startPos, dir, hit->t);
  2221. float* e1 = &verts[segMax*3];
  2222. float* e2 = &verts[((segMax+1)%nv)*3];
  2223. float eDir[3], diff[3];
  2224. dtVsub(eDir, e2, e1);
  2225. dtVsub(diff, curPos, e1);
  2226. float s = dtSqr(eDir[0]) > dtSqr(eDir[2]) ? diff[0] / eDir[0] : diff[2] / eDir[2];
  2227. curPos[1] = e1[1] + eDir[1] * s;
  2228. hit->pathCost += filter->getCost(lastPos, curPos, prevRef, prevTile, prevPoly, curRef, tile, poly, nextRef, nextTile, nextPoly);
  2229. }
  2230. if (!nextRef)
  2231. {
  2232. // No neighbour, we hit a wall.
  2233. // Calculate hit normal.
  2234. const int a = segMax;
  2235. const int b = segMax+1 < nv ? segMax+1 : 0;
  2236. const float* va = &verts[a*3];
  2237. const float* vb = &verts[b*3];
  2238. const float dx = vb[0] - va[0];
  2239. const float dz = vb[2] - va[2];
  2240. hit->hitNormal[0] = dz;
  2241. hit->hitNormal[1] = 0;
  2242. hit->hitNormal[2] = -dx;
  2243. dtVnormalize(hit->hitNormal);
  2244. hit->pathCount = n;
  2245. return status;
  2246. }
  2247. // No hit, advance to neighbour polygon.
  2248. prevRef = curRef;
  2249. curRef = nextRef;
  2250. prevTile = tile;
  2251. tile = nextTile;
  2252. prevPoly = poly;
  2253. poly = nextPoly;
  2254. }
  2255. hit->pathCount = n;
  2256. return status;
  2257. }
  2258. /// @par
  2259. ///
  2260. /// At least one result array must be provided.
  2261. ///
  2262. /// The order of the result set is from least to highest cost to reach the polygon.
  2263. ///
  2264. /// A common use case for this method is to perform Dijkstra searches.
  2265. /// Candidate polygons are found by searching the graph beginning at the start polygon.
  2266. ///
  2267. /// If a polygon is not found via the graph search, even if it intersects the
  2268. /// search circle, it will not be included in the result set. For example:
  2269. ///
  2270. /// polyA is the start polygon.
  2271. /// polyB shares an edge with polyA. (Is adjacent.)
  2272. /// polyC shares an edge with polyB, but not with polyA
  2273. /// Even if the search circle overlaps polyC, it will not be included in the
  2274. /// result set unless polyB is also in the set.
  2275. ///
  2276. /// The value of the center point is used as the start position for cost
  2277. /// calculations. It is not projected onto the surface of the mesh, so its
  2278. /// y-value will effect the costs.
  2279. ///
  2280. /// Intersection tests occur in 2D. All polygons and the search circle are
  2281. /// projected onto the xz-plane. So the y-value of the center point does not
  2282. /// effect intersection tests.
  2283. ///
  2284. /// If the result arrays are to small to hold the entire result set, they will be
  2285. /// filled to capacity.
  2286. ///
  2287. dtStatus dtNavMeshQuery::findPolysAroundCircle(dtPolyRef startRef, const float* centerPos, const float radius,
  2288. const dtQueryFilter* filter,
  2289. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  2290. int* resultCount, const int maxResult) const
  2291. {
  2292. dtAssert(m_nav);
  2293. dtAssert(m_nodePool);
  2294. dtAssert(m_openList);
  2295. *resultCount = 0;
  2296. // Validate input
  2297. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2298. return DT_FAILURE | DT_INVALID_PARAM;
  2299. m_nodePool->clear();
  2300. m_openList->clear();
  2301. dtNode* startNode = m_nodePool->getNode(startRef);
  2302. dtVcopy(startNode->pos, centerPos);
  2303. startNode->pidx = 0;
  2304. startNode->cost = 0;
  2305. startNode->total = 0;
  2306. startNode->id = startRef;
  2307. startNode->flags = DT_NODE_OPEN;
  2308. m_openList->push(startNode);
  2309. dtStatus status = DT_SUCCESS;
  2310. int n = 0;
  2311. if (n < maxResult)
  2312. {
  2313. if (resultRef)
  2314. resultRef[n] = startNode->id;
  2315. if (resultParent)
  2316. resultParent[n] = 0;
  2317. if (resultCost)
  2318. resultCost[n] = 0;
  2319. ++n;
  2320. }
  2321. else
  2322. {
  2323. status |= DT_BUFFER_TOO_SMALL;
  2324. }
  2325. const float radiusSqr = dtSqr(radius);
  2326. while (!m_openList->empty())
  2327. {
  2328. dtNode* bestNode = m_openList->pop();
  2329. bestNode->flags &= ~DT_NODE_OPEN;
  2330. bestNode->flags |= DT_NODE_CLOSED;
  2331. // Get poly and tile.
  2332. // The API input has been cheked already, skip checking internal data.
  2333. const dtPolyRef bestRef = bestNode->id;
  2334. const dtMeshTile* bestTile = 0;
  2335. const dtPoly* bestPoly = 0;
  2336. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2337. // Get parent poly and tile.
  2338. dtPolyRef parentRef = 0;
  2339. const dtMeshTile* parentTile = 0;
  2340. const dtPoly* parentPoly = 0;
  2341. if (bestNode->pidx)
  2342. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2343. if (parentRef)
  2344. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2345. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2346. {
  2347. const dtLink* link = &bestTile->links[i];
  2348. dtPolyRef neighbourRef = link->ref;
  2349. // Skip invalid neighbours and do not follow back to parent.
  2350. if (!neighbourRef || neighbourRef == parentRef)
  2351. continue;
  2352. // Expand to neighbour
  2353. const dtMeshTile* neighbourTile = 0;
  2354. const dtPoly* neighbourPoly = 0;
  2355. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2356. // Do not advance if the polygon is excluded by the filter.
  2357. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2358. continue;
  2359. // Find edge and calc distance to the edge.
  2360. float va[3], vb[3];
  2361. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2362. continue;
  2363. // If the circle is not touching the next polygon, skip it.
  2364. float tseg;
  2365. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  2366. if (distSqr > radiusSqr)
  2367. continue;
  2368. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  2369. if (!neighbourNode)
  2370. {
  2371. status |= DT_OUT_OF_NODES;
  2372. continue;
  2373. }
  2374. if (neighbourNode->flags & DT_NODE_CLOSED)
  2375. continue;
  2376. // Cost
  2377. if (neighbourNode->flags == 0)
  2378. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  2379. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  2380. // The node is already in open list and the new result is worse, skip.
  2381. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  2382. continue;
  2383. neighbourNode->id = neighbourRef;
  2384. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  2385. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  2386. neighbourNode->total = total;
  2387. if (neighbourNode->flags & DT_NODE_OPEN)
  2388. {
  2389. m_openList->modify(neighbourNode);
  2390. }
  2391. else
  2392. {
  2393. if (n < maxResult)
  2394. {
  2395. if (resultRef)
  2396. resultRef[n] = neighbourNode->id;
  2397. if (resultParent)
  2398. resultParent[n] = m_nodePool->getNodeAtIdx(neighbourNode->pidx)->id;
  2399. if (resultCost)
  2400. resultCost[n] = neighbourNode->total;
  2401. ++n;
  2402. }
  2403. else
  2404. {
  2405. status |= DT_BUFFER_TOO_SMALL;
  2406. }
  2407. neighbourNode->flags = DT_NODE_OPEN;
  2408. m_openList->push(neighbourNode);
  2409. }
  2410. }
  2411. }
  2412. *resultCount = n;
  2413. return status;
  2414. }
  2415. /// @par
  2416. ///
  2417. /// The order of the result set is from least to highest cost.
  2418. ///
  2419. /// At least one result array must be provided.
  2420. ///
  2421. /// A common use case for this method is to perform Dijkstra searches.
  2422. /// Candidate polygons are found by searching the graph beginning at the start
  2423. /// polygon.
  2424. ///
  2425. /// The same intersection test restrictions that apply to findPolysAroundCircle()
  2426. /// method apply to this method.
  2427. ///
  2428. /// The 3D centroid of the search polygon is used as the start position for cost
  2429. /// calculations.
  2430. ///
  2431. /// Intersection tests occur in 2D. All polygons are projected onto the
  2432. /// xz-plane. So the y-values of the vertices do not effect intersection tests.
  2433. ///
  2434. /// If the result arrays are is too small to hold the entire result set, they will
  2435. /// be filled to capacity.
  2436. ///
  2437. dtStatus dtNavMeshQuery::findPolysAroundShape(dtPolyRef startRef, const float* verts, const int nverts,
  2438. const dtQueryFilter* filter,
  2439. dtPolyRef* resultRef, dtPolyRef* resultParent, float* resultCost,
  2440. int* resultCount, const int maxResult) const
  2441. {
  2442. dtAssert(m_nav);
  2443. dtAssert(m_nodePool);
  2444. dtAssert(m_openList);
  2445. *resultCount = 0;
  2446. // Validate input
  2447. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2448. return DT_FAILURE | DT_INVALID_PARAM;
  2449. m_nodePool->clear();
  2450. m_openList->clear();
  2451. float centerPos[3] = {0,0,0};
  2452. for (int i = 0; i < nverts; ++i)
  2453. dtVadd(centerPos,centerPos,&verts[i*3]);
  2454. dtVscale(centerPos,centerPos,1.0f/nverts);
  2455. dtNode* startNode = m_nodePool->getNode(startRef);
  2456. dtVcopy(startNode->pos, centerPos);
  2457. startNode->pidx = 0;
  2458. startNode->cost = 0;
  2459. startNode->total = 0;
  2460. startNode->id = startRef;
  2461. startNode->flags = DT_NODE_OPEN;
  2462. m_openList->push(startNode);
  2463. dtStatus status = DT_SUCCESS;
  2464. int n = 0;
  2465. if (n < maxResult)
  2466. {
  2467. if (resultRef)
  2468. resultRef[n] = startNode->id;
  2469. if (resultParent)
  2470. resultParent[n] = 0;
  2471. if (resultCost)
  2472. resultCost[n] = 0;
  2473. ++n;
  2474. }
  2475. else
  2476. {
  2477. status |= DT_BUFFER_TOO_SMALL;
  2478. }
  2479. while (!m_openList->empty())
  2480. {
  2481. dtNode* bestNode = m_openList->pop();
  2482. bestNode->flags &= ~DT_NODE_OPEN;
  2483. bestNode->flags |= DT_NODE_CLOSED;
  2484. // Get poly and tile.
  2485. // The API input has been cheked already, skip checking internal data.
  2486. const dtPolyRef bestRef = bestNode->id;
  2487. const dtMeshTile* bestTile = 0;
  2488. const dtPoly* bestPoly = 0;
  2489. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2490. // Get parent poly and tile.
  2491. dtPolyRef parentRef = 0;
  2492. const dtMeshTile* parentTile = 0;
  2493. const dtPoly* parentPoly = 0;
  2494. if (bestNode->pidx)
  2495. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2496. if (parentRef)
  2497. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2498. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2499. {
  2500. const dtLink* link = &bestTile->links[i];
  2501. dtPolyRef neighbourRef = link->ref;
  2502. // Skip invalid neighbours and do not follow back to parent.
  2503. if (!neighbourRef || neighbourRef == parentRef)
  2504. continue;
  2505. // Expand to neighbour
  2506. const dtMeshTile* neighbourTile = 0;
  2507. const dtPoly* neighbourPoly = 0;
  2508. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2509. // Do not advance if the polygon is excluded by the filter.
  2510. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2511. continue;
  2512. // Find edge and calc distance to the edge.
  2513. float va[3], vb[3];
  2514. if (!getPortalPoints(bestRef, bestPoly, bestTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2515. continue;
  2516. // If the poly is not touching the edge to the next polygon, skip the connection it.
  2517. float tmin, tmax;
  2518. int segMin, segMax;
  2519. if (!dtIntersectSegmentPoly2D(va, vb, verts, nverts, tmin, tmax, segMin, segMax))
  2520. continue;
  2521. if (tmin > 1.0f || tmax < 0.0f)
  2522. continue;
  2523. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  2524. if (!neighbourNode)
  2525. {
  2526. status |= DT_OUT_OF_NODES;
  2527. continue;
  2528. }
  2529. if (neighbourNode->flags & DT_NODE_CLOSED)
  2530. continue;
  2531. // Cost
  2532. if (neighbourNode->flags == 0)
  2533. dtVlerp(neighbourNode->pos, va, vb, 0.5f);
  2534. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  2535. // The node is already in open list and the new result is worse, skip.
  2536. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  2537. continue;
  2538. neighbourNode->id = neighbourRef;
  2539. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  2540. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  2541. neighbourNode->total = total;
  2542. if (neighbourNode->flags & DT_NODE_OPEN)
  2543. {
  2544. m_openList->modify(neighbourNode);
  2545. }
  2546. else
  2547. {
  2548. if (n < maxResult)
  2549. {
  2550. if (resultRef)
  2551. resultRef[n] = neighbourNode->id;
  2552. if (resultParent)
  2553. resultParent[n] = m_nodePool->getNodeAtIdx(neighbourNode->pidx)->id;
  2554. if (resultCost)
  2555. resultCost[n] = neighbourNode->total;
  2556. ++n;
  2557. }
  2558. else
  2559. {
  2560. status |= DT_BUFFER_TOO_SMALL;
  2561. }
  2562. neighbourNode->flags = DT_NODE_OPEN;
  2563. m_openList->push(neighbourNode);
  2564. }
  2565. }
  2566. }
  2567. *resultCount = n;
  2568. return status;
  2569. }
  2570. /// @par
  2571. ///
  2572. /// This method is optimized for a small search radius and small number of result
  2573. /// polygons.
  2574. ///
  2575. /// Candidate polygons are found by searching the navigation graph beginning at
  2576. /// the start polygon.
  2577. ///
  2578. /// The same intersection test restrictions that apply to the findPolysAroundCircle
  2579. /// mehtod applies to this method.
  2580. ///
  2581. /// The value of the center point is used as the start point for cost calculations.
  2582. /// It is not projected onto the surface of the mesh, so its y-value will effect
  2583. /// the costs.
  2584. ///
  2585. /// Intersection tests occur in 2D. All polygons and the search circle are
  2586. /// projected onto the xz-plane. So the y-value of the center point does not
  2587. /// effect intersection tests.
  2588. ///
  2589. /// If the result arrays are is too small to hold the entire result set, they will
  2590. /// be filled to capacity.
  2591. ///
  2592. dtStatus dtNavMeshQuery::findLocalNeighbourhood(dtPolyRef startRef, const float* centerPos, const float radius,
  2593. const dtQueryFilter* filter,
  2594. dtPolyRef* resultRef, dtPolyRef* resultParent,
  2595. int* resultCount, const int maxResult) const
  2596. {
  2597. dtAssert(m_nav);
  2598. dtAssert(m_tinyNodePool);
  2599. *resultCount = 0;
  2600. // Validate input
  2601. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2602. return DT_FAILURE | DT_INVALID_PARAM;
  2603. static const int MAX_STACK = 48;
  2604. dtNode* stack[MAX_STACK];
  2605. int nstack = 0;
  2606. m_tinyNodePool->clear();
  2607. dtNode* startNode = m_tinyNodePool->getNode(startRef);
  2608. startNode->pidx = 0;
  2609. startNode->id = startRef;
  2610. startNode->flags = DT_NODE_CLOSED;
  2611. stack[nstack++] = startNode;
  2612. const float radiusSqr = dtSqr(radius);
  2613. float pa[DT_VERTS_PER_POLYGON*3];
  2614. float pb[DT_VERTS_PER_POLYGON*3];
  2615. dtStatus status = DT_SUCCESS;
  2616. int n = 0;
  2617. if (n < maxResult)
  2618. {
  2619. resultRef[n] = startNode->id;
  2620. if (resultParent)
  2621. resultParent[n] = 0;
  2622. ++n;
  2623. }
  2624. else
  2625. {
  2626. status |= DT_BUFFER_TOO_SMALL;
  2627. }
  2628. while (nstack)
  2629. {
  2630. // Pop front.
  2631. dtNode* curNode = stack[0];
  2632. for (int i = 0; i < nstack-1; ++i)
  2633. stack[i] = stack[i+1];
  2634. nstack--;
  2635. // Get poly and tile.
  2636. // The API input has been cheked already, skip checking internal data.
  2637. const dtPolyRef curRef = curNode->id;
  2638. const dtMeshTile* curTile = 0;
  2639. const dtPoly* curPoly = 0;
  2640. m_nav->getTileAndPolyByRefUnsafe(curRef, &curTile, &curPoly);
  2641. for (unsigned int i = curPoly->firstLink; i != DT_NULL_LINK; i = curTile->links[i].next)
  2642. {
  2643. const dtLink* link = &curTile->links[i];
  2644. dtPolyRef neighbourRef = link->ref;
  2645. // Skip invalid neighbours.
  2646. if (!neighbourRef)
  2647. continue;
  2648. // Skip if cannot alloca more nodes.
  2649. dtNode* neighbourNode = m_tinyNodePool->getNode(neighbourRef);
  2650. if (!neighbourNode)
  2651. continue;
  2652. // Skip visited.
  2653. if (neighbourNode->flags & DT_NODE_CLOSED)
  2654. continue;
  2655. // Expand to neighbour
  2656. const dtMeshTile* neighbourTile = 0;
  2657. const dtPoly* neighbourPoly = 0;
  2658. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  2659. // Skip off-mesh connections.
  2660. if (neighbourPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  2661. continue;
  2662. // Do not advance if the polygon is excluded by the filter.
  2663. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  2664. continue;
  2665. // Find edge and calc distance to the edge.
  2666. float va[3], vb[3];
  2667. if (!getPortalPoints(curRef, curPoly, curTile, neighbourRef, neighbourPoly, neighbourTile, va, vb))
  2668. continue;
  2669. // If the circle is not touching the next polygon, skip it.
  2670. float tseg;
  2671. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  2672. if (distSqr > radiusSqr)
  2673. continue;
  2674. // Mark node visited, this is done before the overlap test so that
  2675. // we will not visit the poly again if the test fails.
  2676. neighbourNode->flags |= DT_NODE_CLOSED;
  2677. neighbourNode->pidx = m_tinyNodePool->getNodeIdx(curNode);
  2678. // Check that the polygon does not collide with existing polygons.
  2679. // Collect vertices of the neighbour poly.
  2680. const int npa = neighbourPoly->vertCount;
  2681. for (int k = 0; k < npa; ++k)
  2682. dtVcopy(&pa[k*3], &neighbourTile->verts[neighbourPoly->verts[k]*3]);
  2683. bool overlap = false;
  2684. for (int j = 0; j < n; ++j)
  2685. {
  2686. dtPolyRef pastRef = resultRef[j];
  2687. // Connected polys do not overlap.
  2688. bool connected = false;
  2689. for (unsigned int k = curPoly->firstLink; k != DT_NULL_LINK; k = curTile->links[k].next)
  2690. {
  2691. if (curTile->links[k].ref == pastRef)
  2692. {
  2693. connected = true;
  2694. break;
  2695. }
  2696. }
  2697. if (connected)
  2698. continue;
  2699. // Potentially overlapping.
  2700. const dtMeshTile* pastTile = 0;
  2701. const dtPoly* pastPoly = 0;
  2702. m_nav->getTileAndPolyByRefUnsafe(pastRef, &pastTile, &pastPoly);
  2703. // Get vertices and test overlap
  2704. const int npb = pastPoly->vertCount;
  2705. for (int k = 0; k < npb; ++k)
  2706. dtVcopy(&pb[k*3], &pastTile->verts[pastPoly->verts[k]*3]);
  2707. if (dtOverlapPolyPoly2D(pa,npa, pb,npb))
  2708. {
  2709. overlap = true;
  2710. break;
  2711. }
  2712. }
  2713. if (overlap)
  2714. continue;
  2715. // This poly is fine, store and advance to the poly.
  2716. if (n < maxResult)
  2717. {
  2718. resultRef[n] = neighbourRef;
  2719. if (resultParent)
  2720. resultParent[n] = curRef;
  2721. ++n;
  2722. }
  2723. else
  2724. {
  2725. status |= DT_BUFFER_TOO_SMALL;
  2726. }
  2727. if (nstack < MAX_STACK)
  2728. {
  2729. stack[nstack++] = neighbourNode;
  2730. }
  2731. }
  2732. }
  2733. *resultCount = n;
  2734. return status;
  2735. }
  2736. struct dtSegInterval
  2737. {
  2738. dtPolyRef ref;
  2739. short tmin, tmax;
  2740. };
  2741. static void insertInterval(dtSegInterval* ints, int& nints, const int maxInts,
  2742. const short tmin, const short tmax, const dtPolyRef ref)
  2743. {
  2744. if (nints+1 > maxInts) return;
  2745. // Find insertion point.
  2746. int idx = 0;
  2747. while (idx < nints)
  2748. {
  2749. if (tmax <= ints[idx].tmin)
  2750. break;
  2751. idx++;
  2752. }
  2753. // Move current results.
  2754. if (nints-idx)
  2755. memmove(ints+idx+1, ints+idx, sizeof(dtSegInterval)*(nints-idx));
  2756. // Store
  2757. ints[idx].ref = ref;
  2758. ints[idx].tmin = tmin;
  2759. ints[idx].tmax = tmax;
  2760. nints++;
  2761. }
  2762. /// @par
  2763. ///
  2764. /// If the @p segmentRefs parameter is provided, then all polygon segments will be returned.
  2765. /// Otherwise only the wall segments are returned.
  2766. ///
  2767. /// A segment that is normally a portal will be included in the result set as a
  2768. /// wall if the @p filter results in the neighbor polygon becoomming impassable.
  2769. ///
  2770. /// The @p segmentVerts and @p segmentRefs buffers should normally be sized for the
  2771. /// maximum segments per polygon of the source navigation mesh.
  2772. ///
  2773. dtStatus dtNavMeshQuery::getPolyWallSegments(dtPolyRef ref, const dtQueryFilter* filter,
  2774. float* segmentVerts, dtPolyRef* segmentRefs, int* segmentCount,
  2775. const int maxSegments) const
  2776. {
  2777. dtAssert(m_nav);
  2778. *segmentCount = 0;
  2779. const dtMeshTile* tile = 0;
  2780. const dtPoly* poly = 0;
  2781. if (dtStatusFailed(m_nav->getTileAndPolyByRef(ref, &tile, &poly)))
  2782. return DT_FAILURE | DT_INVALID_PARAM;
  2783. int n = 0;
  2784. static const int MAX_INTERVAL = 16;
  2785. dtSegInterval ints[MAX_INTERVAL];
  2786. int nints;
  2787. const bool storePortals = segmentRefs != 0;
  2788. dtStatus status = DT_SUCCESS;
  2789. for (int i = 0, j = (int)poly->vertCount-1; i < (int)poly->vertCount; j = i++)
  2790. {
  2791. // Skip non-solid edges.
  2792. nints = 0;
  2793. if (poly->neis[j] & DT_EXT_LINK)
  2794. {
  2795. // Tile border.
  2796. for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  2797. {
  2798. const dtLink* link = &tile->links[k];
  2799. if (link->edge == j)
  2800. {
  2801. if (link->ref != 0)
  2802. {
  2803. const dtMeshTile* neiTile = 0;
  2804. const dtPoly* neiPoly = 0;
  2805. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  2806. if (filter->passFilter(link->ref, neiTile, neiPoly))
  2807. {
  2808. insertInterval(ints, nints, MAX_INTERVAL, link->bmin, link->bmax, link->ref);
  2809. }
  2810. }
  2811. }
  2812. }
  2813. }
  2814. else
  2815. {
  2816. // Internal edge
  2817. dtPolyRef neiRef = 0;
  2818. if (poly->neis[j])
  2819. {
  2820. const unsigned int idx = (unsigned int)(poly->neis[j]-1);
  2821. neiRef = m_nav->getPolyRefBase(tile) | idx;
  2822. if (!filter->passFilter(neiRef, tile, &tile->polys[idx]))
  2823. neiRef = 0;
  2824. }
  2825. // If the edge leads to another polygon and portals are not stored, skip.
  2826. if (neiRef != 0 && !storePortals)
  2827. continue;
  2828. if (n < maxSegments)
  2829. {
  2830. const float* vj = &tile->verts[poly->verts[j]*3];
  2831. const float* vi = &tile->verts[poly->verts[i]*3];
  2832. float* seg = &segmentVerts[n*6];
  2833. dtVcopy(seg+0, vj);
  2834. dtVcopy(seg+3, vi);
  2835. if (segmentRefs)
  2836. segmentRefs[n] = neiRef;
  2837. n++;
  2838. }
  2839. else
  2840. {
  2841. status |= DT_BUFFER_TOO_SMALL;
  2842. }
  2843. continue;
  2844. }
  2845. // Add sentinels
  2846. insertInterval(ints, nints, MAX_INTERVAL, -1, 0, 0);
  2847. insertInterval(ints, nints, MAX_INTERVAL, 255, 256, 0);
  2848. // Store segments.
  2849. const float* vj = &tile->verts[poly->verts[j]*3];
  2850. const float* vi = &tile->verts[poly->verts[i]*3];
  2851. for (int k = 1; k < nints; ++k)
  2852. {
  2853. // Portal segment.
  2854. if (storePortals && ints[k].ref)
  2855. {
  2856. const float tmin = ints[k].tmin/255.0f;
  2857. const float tmax = ints[k].tmax/255.0f;
  2858. if (n < maxSegments)
  2859. {
  2860. float* seg = &segmentVerts[n*6];
  2861. dtVlerp(seg+0, vj,vi, tmin);
  2862. dtVlerp(seg+3, vj,vi, tmax);
  2863. if (segmentRefs)
  2864. segmentRefs[n] = ints[k].ref;
  2865. n++;
  2866. }
  2867. else
  2868. {
  2869. status |= DT_BUFFER_TOO_SMALL;
  2870. }
  2871. }
  2872. // Wall segment.
  2873. const int imin = ints[k-1].tmax;
  2874. const int imax = ints[k].tmin;
  2875. if (imin != imax)
  2876. {
  2877. const float tmin = imin/255.0f;
  2878. const float tmax = imax/255.0f;
  2879. if (n < maxSegments)
  2880. {
  2881. float* seg = &segmentVerts[n*6];
  2882. dtVlerp(seg+0, vj,vi, tmin);
  2883. dtVlerp(seg+3, vj,vi, tmax);
  2884. if (segmentRefs)
  2885. segmentRefs[n] = 0;
  2886. n++;
  2887. }
  2888. else
  2889. {
  2890. status |= DT_BUFFER_TOO_SMALL;
  2891. }
  2892. }
  2893. }
  2894. }
  2895. *segmentCount = n;
  2896. return status;
  2897. }
  2898. /// @par
  2899. ///
  2900. /// @p hitPos is not adjusted using the height detail data.
  2901. ///
  2902. /// @p hitDist will equal the search radius if there is no wall within the
  2903. /// radius. In this case the values of @p hitPos and @p hitNormal are
  2904. /// undefined.
  2905. ///
  2906. /// The normal will become unpredicable if @p hitDist is a very small number.
  2907. ///
  2908. dtStatus dtNavMeshQuery::findDistanceToWall(dtPolyRef startRef, const float* centerPos, const float maxRadius,
  2909. const dtQueryFilter* filter,
  2910. float* hitDist, float* hitPos, float* hitNormal) const
  2911. {
  2912. dtAssert(m_nav);
  2913. dtAssert(m_nodePool);
  2914. dtAssert(m_openList);
  2915. // Validate input
  2916. if (!startRef || !m_nav->isValidPolyRef(startRef))
  2917. return DT_FAILURE | DT_INVALID_PARAM;
  2918. m_nodePool->clear();
  2919. m_openList->clear();
  2920. dtNode* startNode = m_nodePool->getNode(startRef);
  2921. dtVcopy(startNode->pos, centerPos);
  2922. startNode->pidx = 0;
  2923. startNode->cost = 0;
  2924. startNode->total = 0;
  2925. startNode->id = startRef;
  2926. startNode->flags = DT_NODE_OPEN;
  2927. m_openList->push(startNode);
  2928. float radiusSqr = dtSqr(maxRadius);
  2929. dtStatus status = DT_SUCCESS;
  2930. while (!m_openList->empty())
  2931. {
  2932. dtNode* bestNode = m_openList->pop();
  2933. bestNode->flags &= ~DT_NODE_OPEN;
  2934. bestNode->flags |= DT_NODE_CLOSED;
  2935. // Get poly and tile.
  2936. // The API input has been cheked already, skip checking internal data.
  2937. const dtPolyRef bestRef = bestNode->id;
  2938. const dtMeshTile* bestTile = 0;
  2939. const dtPoly* bestPoly = 0;
  2940. m_nav->getTileAndPolyByRefUnsafe(bestRef, &bestTile, &bestPoly);
  2941. // Get parent poly and tile.
  2942. dtPolyRef parentRef = 0;
  2943. const dtMeshTile* parentTile = 0;
  2944. const dtPoly* parentPoly = 0;
  2945. if (bestNode->pidx)
  2946. parentRef = m_nodePool->getNodeAtIdx(bestNode->pidx)->id;
  2947. if (parentRef)
  2948. m_nav->getTileAndPolyByRefUnsafe(parentRef, &parentTile, &parentPoly);
  2949. // Hit test walls.
  2950. for (int i = 0, j = (int)bestPoly->vertCount-1; i < (int)bestPoly->vertCount; j = i++)
  2951. {
  2952. // Skip non-solid edges.
  2953. if (bestPoly->neis[j] & DT_EXT_LINK)
  2954. {
  2955. // Tile border.
  2956. bool solid = true;
  2957. for (unsigned int k = bestPoly->firstLink; k != DT_NULL_LINK; k = bestTile->links[k].next)
  2958. {
  2959. const dtLink* link = &bestTile->links[k];
  2960. if (link->edge == j)
  2961. {
  2962. if (link->ref != 0)
  2963. {
  2964. const dtMeshTile* neiTile = 0;
  2965. const dtPoly* neiPoly = 0;
  2966. m_nav->getTileAndPolyByRefUnsafe(link->ref, &neiTile, &neiPoly);
  2967. if (filter->passFilter(link->ref, neiTile, neiPoly))
  2968. solid = false;
  2969. }
  2970. break;
  2971. }
  2972. }
  2973. if (!solid) continue;
  2974. }
  2975. else if (bestPoly->neis[j])
  2976. {
  2977. // Internal edge
  2978. const unsigned int idx = (unsigned int)(bestPoly->neis[j]-1);
  2979. const dtPolyRef ref = m_nav->getPolyRefBase(bestTile) | idx;
  2980. if (filter->passFilter(ref, bestTile, &bestTile->polys[idx]))
  2981. continue;
  2982. }
  2983. // Calc distance to the edge.
  2984. const float* vj = &bestTile->verts[bestPoly->verts[j]*3];
  2985. const float* vi = &bestTile->verts[bestPoly->verts[i]*3];
  2986. float tseg;
  2987. float distSqr = dtDistancePtSegSqr2D(centerPos, vj, vi, tseg);
  2988. // Edge is too far, skip.
  2989. if (distSqr > radiusSqr)
  2990. continue;
  2991. // Hit wall, update radius.
  2992. radiusSqr = distSqr;
  2993. // Calculate hit pos.
  2994. hitPos[0] = vj[0] + (vi[0] - vj[0])*tseg;
  2995. hitPos[1] = vj[1] + (vi[1] - vj[1])*tseg;
  2996. hitPos[2] = vj[2] + (vi[2] - vj[2])*tseg;
  2997. }
  2998. for (unsigned int i = bestPoly->firstLink; i != DT_NULL_LINK; i = bestTile->links[i].next)
  2999. {
  3000. const dtLink* link = &bestTile->links[i];
  3001. dtPolyRef neighbourRef = link->ref;
  3002. // Skip invalid neighbours and do not follow back to parent.
  3003. if (!neighbourRef || neighbourRef == parentRef)
  3004. continue;
  3005. // Expand to neighbour.
  3006. const dtMeshTile* neighbourTile = 0;
  3007. const dtPoly* neighbourPoly = 0;
  3008. m_nav->getTileAndPolyByRefUnsafe(neighbourRef, &neighbourTile, &neighbourPoly);
  3009. // Skip off-mesh connections.
  3010. if (neighbourPoly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  3011. continue;
  3012. // Calc distance to the edge.
  3013. const float* va = &bestTile->verts[bestPoly->verts[link->edge]*3];
  3014. const float* vb = &bestTile->verts[bestPoly->verts[(link->edge+1) % bestPoly->vertCount]*3];
  3015. float tseg;
  3016. float distSqr = dtDistancePtSegSqr2D(centerPos, va, vb, tseg);
  3017. // If the circle is not touching the next polygon, skip it.
  3018. if (distSqr > radiusSqr)
  3019. continue;
  3020. if (!filter->passFilter(neighbourRef, neighbourTile, neighbourPoly))
  3021. continue;
  3022. dtNode* neighbourNode = m_nodePool->getNode(neighbourRef);
  3023. if (!neighbourNode)
  3024. {
  3025. status |= DT_OUT_OF_NODES;
  3026. continue;
  3027. }
  3028. if (neighbourNode->flags & DT_NODE_CLOSED)
  3029. continue;
  3030. // Cost
  3031. if (neighbourNode->flags == 0)
  3032. {
  3033. getEdgeMidPoint(bestRef, bestPoly, bestTile,
  3034. neighbourRef, neighbourPoly, neighbourTile, neighbourNode->pos);
  3035. }
  3036. const float total = bestNode->total + dtVdist(bestNode->pos, neighbourNode->pos);
  3037. // The node is already in open list and the new result is worse, skip.
  3038. if ((neighbourNode->flags & DT_NODE_OPEN) && total >= neighbourNode->total)
  3039. continue;
  3040. neighbourNode->id = neighbourRef;
  3041. neighbourNode->flags = (neighbourNode->flags & ~DT_NODE_CLOSED);
  3042. neighbourNode->pidx = m_nodePool->getNodeIdx(bestNode);
  3043. neighbourNode->total = total;
  3044. if (neighbourNode->flags & DT_NODE_OPEN)
  3045. {
  3046. m_openList->modify(neighbourNode);
  3047. }
  3048. else
  3049. {
  3050. neighbourNode->flags |= DT_NODE_OPEN;
  3051. m_openList->push(neighbourNode);
  3052. }
  3053. }
  3054. }
  3055. // Calc hit normal.
  3056. dtVsub(hitNormal, centerPos, hitPos);
  3057. dtVnormalize(hitNormal);
  3058. *hitDist = dtMathSqrtf(radiusSqr);
  3059. return status;
  3060. }
  3061. bool dtNavMeshQuery::isValidPolyRef(dtPolyRef ref, const dtQueryFilter* filter) const
  3062. {
  3063. const dtMeshTile* tile = 0;
  3064. const dtPoly* poly = 0;
  3065. dtStatus status = m_nav->getTileAndPolyByRef(ref, &tile, &poly);
  3066. // If cannot get polygon, assume it does not exists and boundary is invalid.
  3067. if (dtStatusFailed(status))
  3068. return false;
  3069. // If cannot pass filter, assume flags has changed and boundary is invalid.
  3070. if (!filter->passFilter(ref, tile, poly))
  3071. return false;
  3072. return true;
  3073. }
  3074. /// @par
  3075. ///
  3076. /// The closed list is the list of polygons that were fully evaluated during
  3077. /// the last navigation graph search. (A* or Dijkstra)
  3078. ///
  3079. bool dtNavMeshQuery::isInClosedList(dtPolyRef ref) const
  3080. {
  3081. if (!m_nodePool) return false;
  3082. dtNode* nodes[DT_MAX_STATES_PER_NODE];
  3083. int n= m_nodePool->findNodes(ref, nodes, DT_MAX_STATES_PER_NODE);
  3084. for (int i=0; i<n; i++)
  3085. {
  3086. if (nodes[i]->flags & DT_NODE_CLOSED)
  3087. return true;
  3088. }
  3089. return false;
  3090. }