DetourTileNavMesh.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. //
  2. // Copyright (c) 2009 Mikko Mononen memon@inside.org
  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 <math.h>
  19. #include <float.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include "DetourTileNavMesh.h"
  23. #include "DetourNode.h"
  24. #include "DetourCommon.h"
  25. inline int opposite(int side) { return (side+2) & 0x3; }
  26. inline bool overlapBoxes(const float* amin, const float* amax,
  27. const float* bmin, const float* bmax)
  28. {
  29. bool overlap = true;
  30. overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
  31. overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
  32. overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
  33. return overlap;
  34. }
  35. inline bool overlapRects(const float* amin, const float* amax,
  36. const float* bmin, const float* bmax)
  37. {
  38. bool overlap = true;
  39. overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
  40. overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
  41. return overlap;
  42. }
  43. static void calcRect(const float* va, const float* vb,
  44. float* bmin, float* bmax,
  45. int side, float padx, float pady)
  46. {
  47. if ((side&1) == 0)
  48. {
  49. bmin[0] = min(va[2],vb[2]) + padx;
  50. bmin[1] = min(va[1],vb[1]) - pady;
  51. bmax[0] = max(va[2],vb[2]) - padx;
  52. bmax[1] = max(va[1],vb[1]) + pady;
  53. }
  54. else
  55. {
  56. bmin[0] = min(va[0],vb[0]) + padx;
  57. bmin[1] = min(va[1],vb[1]) - pady;
  58. bmax[0] = max(va[0],vb[0]) - padx;
  59. bmax[1] = max(va[1],vb[1]) + pady;
  60. }
  61. }
  62. inline int computeTileHash(int x, int y)
  63. {
  64. const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
  65. const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
  66. unsigned int n = h1 * x + h2 * y;
  67. return (int)(n & (DT_TILE_LOOKUP_SIZE-1));
  68. }
  69. //////////////////////////////////////////////////////////////////////////////////////////
  70. dtTiledNavMesh::dtTiledNavMesh() :
  71. m_tileSize(0),
  72. m_portalHeight(0),
  73. m_nextFree(0),
  74. m_tmpLinks(0),
  75. m_ntmpLinks(0),
  76. m_nodePool(0),
  77. m_openList(0)
  78. {
  79. }
  80. dtTiledNavMesh::~dtTiledNavMesh()
  81. {
  82. for (int i = 0; i < DT_MAX_TILES; ++i)
  83. {
  84. if (m_tiles[i].data && m_tiles[i].dataSize < 0)
  85. {
  86. delete [] m_tiles[i].data;
  87. m_tiles[i].data = 0;
  88. m_tiles[i].dataSize = 0;
  89. }
  90. }
  91. delete [] m_tmpLinks;
  92. delete m_nodePool;
  93. delete m_openList;
  94. }
  95. bool dtTiledNavMesh::init(const float* orig, float tileSize, float portalHeight)
  96. {
  97. vcopy(m_orig, orig);
  98. m_tileSize = tileSize;
  99. m_portalHeight = portalHeight;
  100. // Init tiles
  101. memset(m_tiles, 0, sizeof(dtTile)*DT_MAX_TILES);
  102. memset(m_posLookup, 0, sizeof(dtTile*)*DT_TILE_LOOKUP_SIZE);
  103. m_nextFree = 0;
  104. for (int i = DT_MAX_TILES-1; i >= 0; --i)
  105. {
  106. m_tiles[i].next = m_nextFree;
  107. m_nextFree = &m_tiles[i];
  108. }
  109. if (!m_nodePool)
  110. {
  111. m_nodePool = new dtNodePool(2048, 256);
  112. if (!m_nodePool)
  113. return false;
  114. }
  115. if (!m_openList)
  116. {
  117. m_openList = new dtNodeQueue(2048);
  118. if (!m_openList)
  119. return false;
  120. }
  121. return true;
  122. }
  123. //////////////////////////////////////////////////////////////////////////////////////////
  124. int dtTiledNavMesh::findConnectingPolys(const float* va, const float* vb,
  125. dtTile* tile, int side,
  126. dtTilePolyRef* con, float* conarea, int maxcon)
  127. {
  128. if (!tile) return 0;
  129. dtTileHeader* h = tile->header;
  130. float amin[2], amax[2];
  131. calcRect(va,vb, amin,amax, side, 0.01f, m_portalHeight);
  132. // Remove links pointing to 'side' and compact the links array.
  133. float bmin[2], bmax[2];
  134. unsigned short m = 0x8000 | (unsigned short)side;
  135. int n = 0;
  136. dtTilePolyRef base = getTileId(tile);
  137. for (int i = 0; i < h->npolys; ++i)
  138. {
  139. dtTilePoly* poly = &h->polys[i];
  140. for (int j = 0; j < poly->nv; ++j)
  141. {
  142. // Skip edges which do not point to the right side.
  143. if (poly->n[j] != m) continue;
  144. // Check if the segments touch.
  145. const float* vc = &h->verts[poly->v[j]*3];
  146. const float* vd = &h->verts[poly->v[(j+1) % (int)poly->nv]*3];
  147. calcRect(vc,vd, bmin,bmax, side, 0.01f, m_portalHeight);
  148. if (!overlapRects(amin,amax, bmin,bmax)) continue;
  149. // Add return value.
  150. if (n < maxcon)
  151. {
  152. conarea[n*2+0] = max(amin[0], bmin[0]);
  153. conarea[n*2+1] = min(amax[0], bmax[0]);
  154. con[n] = base | (unsigned int)i;
  155. n++;
  156. }
  157. break;
  158. }
  159. }
  160. return n;
  161. }
  162. void dtTiledNavMesh::removeExtLinks(dtTile* tile, int side)
  163. {
  164. if (!tile) return;
  165. dtTileHeader* h = tile->header;
  166. // Remove links pointing to 'side' and compact the links array.
  167. dtTileLink* pool = m_tmpLinks;
  168. int nlinks = 0;
  169. for (int i = 0; i < h->npolys; ++i)
  170. {
  171. dtTilePoly* poly = &h->polys[i];
  172. int plinks = nlinks;
  173. int nplinks = 0;
  174. for (int j = 0; j < poly->nlinks; ++j)
  175. {
  176. dtTileLink* link = &h->links[poly->links+j];
  177. if ((int)link->side != side)
  178. {
  179. if (nlinks < h->maxlinks)
  180. {
  181. dtTileLink* dst = &pool[nlinks++];
  182. memcpy(dst, link, sizeof(dtTileLink));
  183. nplinks++;
  184. }
  185. }
  186. }
  187. poly->links = plinks;
  188. poly->nlinks = nplinks;
  189. }
  190. h->nlinks = nlinks;
  191. if (h->nlinks)
  192. memcpy(h->links, m_tmpLinks, sizeof(dtTileLink)*nlinks);
  193. }
  194. void dtTiledNavMesh::buildExtLinks(dtTile* tile, dtTile* target, int side)
  195. {
  196. if (!tile) return;
  197. dtTileHeader* h = tile->header;
  198. // Remove links pointing to 'side' and compact the links array.
  199. dtTileLink* pool = m_tmpLinks;
  200. int nlinks = 0;
  201. for (int i = 0; i < h->npolys; ++i)
  202. {
  203. dtTilePoly* poly = &h->polys[i];
  204. int plinks = nlinks;
  205. int nplinks = 0;
  206. // Copy internal and other external links.
  207. for (int j = 0; j < poly->nlinks; ++j)
  208. {
  209. dtTileLink* link = &h->links[poly->links+j];
  210. if ((int)link->side != side)
  211. {
  212. if (nlinks < h->maxlinks)
  213. {
  214. dtTileLink* dst = &pool[nlinks++];
  215. memcpy(dst, link, sizeof(dtTileLink));
  216. nplinks++;
  217. }
  218. }
  219. }
  220. // Create new links.
  221. unsigned short m = 0x8000 | (unsigned short)side;
  222. for (int j = 0; j < poly->nv; ++j)
  223. {
  224. // Skip edges which do not point to the right side.
  225. if (poly->n[j] != m) continue;
  226. // Create new links
  227. const float* va = &h->verts[poly->v[j]*3];
  228. const float* vb = &h->verts[poly->v[(j+1)%(int)poly->nv]*3];
  229. dtTilePolyRef nei[4];
  230. float neia[4*2];
  231. int nnei = findConnectingPolys(va,vb, target, opposite(side), nei,neia,4);
  232. for (int k = 0; k < nnei; ++k)
  233. {
  234. if (nlinks < h->maxlinks)
  235. {
  236. dtTileLink* link = &pool[nlinks++];
  237. link->ref = nei[k];
  238. link->p = (unsigned short)i;
  239. link->e = (unsigned char)j;
  240. link->side = (unsigned char)side;
  241. // Compress portal limits to a byte value.
  242. if (side == 0 || side == 2)
  243. {
  244. const float lmin = min(va[2], vb[2]);
  245. const float lmax = max(va[2], vb[2]);
  246. link->bmin = (unsigned char)(clamp((neia[k*2+0]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
  247. link->bmax = (unsigned char)(clamp((neia[k*2+1]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
  248. }
  249. else
  250. {
  251. const float lmin = min(va[0], vb[0]);
  252. const float lmax = max(va[0], vb[0]);
  253. link->bmin = (unsigned char)(clamp((neia[k*2+0]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
  254. link->bmax = (unsigned char)(clamp((neia[k*2+1]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
  255. }
  256. nplinks++;
  257. }
  258. }
  259. }
  260. poly->links = plinks;
  261. poly->nlinks = nplinks;
  262. }
  263. h->nlinks = nlinks;
  264. if (h->nlinks)
  265. memcpy(h->links, m_tmpLinks, sizeof(dtTileLink)*nlinks);
  266. }
  267. void dtTiledNavMesh::buildIntLinks(dtTile* tile)
  268. {
  269. if (!tile) return;
  270. dtTileHeader* h = tile->header;
  271. dtTilePolyRef base = getTileId(tile);
  272. dtTileLink* pool = h->links;
  273. int nlinks = 0;
  274. for (int i = 0; i < h->npolys; ++i)
  275. {
  276. dtTilePoly* poly = &h->polys[i];
  277. poly->links = nlinks;
  278. poly->nlinks = 0;
  279. for (int j = 0; j < poly->nv; ++j)
  280. {
  281. // Skip hard and non-internal edges.
  282. if (poly->n[j] == 0 || (poly->n[j] & 0x8000)) continue;
  283. if (nlinks < h->maxlinks)
  284. {
  285. dtTileLink* link = &pool[nlinks++];
  286. link->ref = base | (unsigned int)(poly->n[j]-1);
  287. link->p = (unsigned short)i;
  288. link->e = (unsigned char)j;
  289. link->side = 0xff;
  290. link->bmin = link->bmax = 0;
  291. poly->nlinks++;
  292. }
  293. }
  294. }
  295. h->nlinks = nlinks;
  296. }
  297. bool dtTiledNavMesh::addTileAt(int x, int y, unsigned char* data, int dataSize, bool ownsData)
  298. {
  299. if (getTileAt(x,y))
  300. return false;
  301. // Make sure there is enough space for new tile.
  302. if (!m_nextFree)
  303. return false;
  304. // Make sure the data is in right format.
  305. dtTileHeader* header = (dtTileHeader*)data;
  306. if (header->magic != DT_TILE_NAVMESH_MAGIC)
  307. return false;
  308. if (header->version != DT_TILE_NAVMESH_VERSION)
  309. return false;
  310. // Make sure the tmp link array is large enough.
  311. if (header->maxlinks > m_ntmpLinks)
  312. {
  313. m_ntmpLinks = header->maxlinks;
  314. delete [] m_tmpLinks;
  315. m_tmpLinks = 0;
  316. m_tmpLinks = new dtTileLink[m_ntmpLinks];
  317. }
  318. if (!m_tmpLinks)
  319. return false;
  320. // Allocate a tile.
  321. dtTile* tile = m_nextFree;
  322. m_nextFree = tile->next;
  323. tile->next = 0;
  324. // Insert tile into the position lut.
  325. int h = computeTileHash(x,y);
  326. tile->next = m_posLookup[h];
  327. m_posLookup[h] = tile;
  328. // Patch header pointers.
  329. const int headerSize = sizeof(dtTileHeader);
  330. const int vertsSize = sizeof(float)*3*header->nverts;
  331. const int polysSize = sizeof(dtTilePoly)*header->npolys;
  332. const int linksSize = sizeof(dtTileLink)*(header->maxlinks);
  333. const int detailMeshesSize = sizeof(dtTilePolyDetail)*header->ndmeshes;
  334. const int detailVertsSize = sizeof(float)*3*header->ndverts;
  335. const int detailTrisSize = sizeof(unsigned char)*4*header->ndtris;
  336. unsigned char* d = data + headerSize;
  337. header->verts = (float*)d; d += vertsSize;
  338. header->polys = (dtTilePoly*)d; d += polysSize;
  339. header->links = (dtTileLink*)d; d += linksSize;
  340. header->dmeshes = (dtTilePolyDetail*)d; d += detailMeshesSize;
  341. header->dverts = (float*)d; d += detailVertsSize;
  342. header->dtris = (unsigned char*)d; d += detailTrisSize;
  343. // Init tile.
  344. tile->header = header;
  345. tile->x = x;
  346. tile->y = y;
  347. tile->data = data;
  348. tile->dataSize = dataSize;
  349. tile->ownsData = ownsData;
  350. buildIntLinks(tile);
  351. // Create connections connections.
  352. for (int i = 0; i < 4; ++i)
  353. {
  354. dtTile* nei = getNeighbourTileAt(x,y,i);
  355. if (nei)
  356. {
  357. buildExtLinks(tile, nei, i);
  358. buildExtLinks(nei, tile, opposite(i));
  359. }
  360. }
  361. return true;
  362. }
  363. dtTile* dtTiledNavMesh::getTileAt(int x, int y)
  364. {
  365. // Find tile based on hash.
  366. int h = computeTileHash(x,y);
  367. dtTile* tile = m_posLookup[h];
  368. while (tile)
  369. {
  370. if (tile->x == x && tile->y == y)
  371. return tile;
  372. tile = tile->next;
  373. }
  374. return 0;
  375. }
  376. dtTile* dtTiledNavMesh::getTile(int i)
  377. {
  378. return &m_tiles[i];
  379. }
  380. const dtTile* dtTiledNavMesh::getTile(int i) const
  381. {
  382. return &m_tiles[i];
  383. }
  384. dtTile* dtTiledNavMesh::getNeighbourTileAt(int x, int y, int side)
  385. {
  386. switch (side)
  387. {
  388. case 0: x++; break;
  389. case 1: y++; break;
  390. case 2: x--; break;
  391. case 3: y--; break;
  392. };
  393. return getTileAt(x,y);
  394. }
  395. bool dtTiledNavMesh::removeTileAt(int x, int y, unsigned char** data, int* dataSize)
  396. {
  397. // Remove tile from hash lookup.
  398. int h = computeTileHash(x,y);
  399. dtTile* prev = 0;
  400. dtTile* tile = m_posLookup[h];
  401. while (tile)
  402. {
  403. if (tile->x == x && tile->y == y)
  404. {
  405. if (prev)
  406. prev->next = tile->next;
  407. else
  408. m_posLookup[h] = tile->next;
  409. break;
  410. }
  411. prev = tile;
  412. tile = tile->next;
  413. }
  414. if (!tile)
  415. return false;
  416. // Remove connections to neighbour tiles.
  417. for (int i = 0; i < 4; ++i)
  418. {
  419. dtTile* nei = getNeighbourTileAt(x,y,i);
  420. if (!nei) continue;
  421. removeExtLinks(nei, opposite(i));
  422. }
  423. // Reset tile.
  424. if (tile->ownsData)
  425. {
  426. // Owns data
  427. delete [] tile->data;
  428. tile->data = 0;
  429. tile->dataSize = 0;
  430. if (data) *data = 0;
  431. if (dataSize) *dataSize = 0;
  432. }
  433. else
  434. {
  435. if (data) *data = tile->data;
  436. if (dataSize) *dataSize = tile->dataSize;
  437. }
  438. tile->header = 0;
  439. tile->x = tile->y = 0;
  440. tile->salt++;
  441. // Add to free list.
  442. tile->next = m_nextFree;
  443. m_nextFree = tile;
  444. return true;
  445. }
  446. bool dtTiledNavMesh::closestPointToPoly(dtTilePolyRef ref, const float* pos, float* closest) const
  447. {
  448. unsigned int salt, it, ip;
  449. dtDecodeTileId(ref, salt, it, ip);
  450. if (it >= DT_MAX_TILES) return false;
  451. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
  452. const dtTileHeader* header = m_tiles[it].header;
  453. if (ip >= (unsigned int)header->npolys) return false;
  454. const dtTilePoly* poly = &header->polys[ip];
  455. float closestDistSqr = FLT_MAX;
  456. const dtTilePolyDetail* pd = &header->dmeshes[ip];
  457. for (int j = 0; j < pd->ntris; ++j)
  458. {
  459. const unsigned char* t = &header->dtris[(pd->tbase+j)*4];
  460. const float* v[3];
  461. for (int k = 0; k < 3; ++k)
  462. {
  463. if (t[k] < poly->nv)
  464. v[k] = &header->verts[poly->v[t[k]]*3];
  465. else
  466. v[k] = &header->dverts[(pd->vbase+(t[k]-poly->nv))*3];
  467. }
  468. float pt[3];
  469. closestPtPointTriangle(pt, pos, v[0], v[1], v[2]);
  470. float d = vdistSqr(pos, pt);
  471. if (d < closestDistSqr)
  472. {
  473. vcopy(closest, pt);
  474. closestDistSqr = d;
  475. }
  476. }
  477. return true;
  478. }
  479. bool dtTiledNavMesh::getPolyHeight(dtTilePolyRef ref, const float* pos, float* height) const
  480. {
  481. unsigned int salt, it, ip;
  482. dtDecodeTileId(ref, salt, it, ip);
  483. if (it >= DT_MAX_TILES) return false;
  484. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
  485. const dtTileHeader* header = m_tiles[it].header;
  486. if (ip >= (unsigned int)header->npolys) return false;
  487. const dtTilePoly* poly = &header->polys[ip];
  488. const dtTilePolyDetail* pd = &header->dmeshes[ip];
  489. for (int j = 0; j < pd->ntris; ++j)
  490. {
  491. const unsigned char* t = &header->dtris[(pd->tbase+j)*4];
  492. const float* v[3];
  493. for (int k = 0; k < 3; ++k)
  494. {
  495. if (t[k] < poly->nv)
  496. v[k] = &header->verts[poly->v[t[k]]*3];
  497. else
  498. v[k] = &header->dverts[(pd->vbase+(t[k]-poly->nv))*3];
  499. }
  500. float h;
  501. if (closestHeightPointTriangle(pos, v[0], v[1], v[2], h))
  502. {
  503. if (height)
  504. *height = h;
  505. return true;
  506. }
  507. }
  508. return false;
  509. }
  510. dtTilePolyRef dtTiledNavMesh::findNearestPoly(const float* center, const float* extents)
  511. {
  512. // Get nearby polygons from proximity grid.
  513. dtTilePolyRef polys[128];
  514. int npolys = queryPolygons(center, extents, polys, 128);
  515. // Find nearest polygon amongst the nearby polygons.
  516. dtTilePolyRef nearest = 0;
  517. float nearestDistanceSqr = FLT_MAX;
  518. for (int i = 0; i < npolys; ++i)
  519. {
  520. dtTilePolyRef ref = polys[i];
  521. float closest[3];
  522. if (!closestPointToPoly(ref, center, closest))
  523. continue;
  524. float d = vdistSqr(center, closest);
  525. if (d < nearestDistanceSqr)
  526. {
  527. nearestDistanceSqr = d;
  528. nearest = ref;
  529. }
  530. }
  531. return nearest;
  532. }
  533. dtTilePolyRef dtTiledNavMesh::getTileId(dtTile* tile)
  534. {
  535. if (!tile) return 0;
  536. const unsigned int it = tile - m_tiles;
  537. return dtEncodeTileId(tile->salt, it, 0);
  538. }
  539. int dtTiledNavMesh::queryTilePolygons(dtTile* tile,
  540. const float* qmin, const float* qmax,
  541. dtTilePolyRef* polys, const int maxPolys)
  542. {
  543. float bmin[3], bmax[3];
  544. const dtTileHeader* header = tile->header;
  545. int n = 0;
  546. dtTilePolyRef base = getTileId(tile);
  547. for (int i = 0; i < header->npolys; ++i)
  548. {
  549. // Calc polygon bounds.
  550. dtTilePoly* p = &header->polys[i];
  551. const float* v = &header->verts[p->v[0]*3];
  552. vcopy(bmin, v);
  553. vcopy(bmax, v);
  554. for (int j = 1; j < p->nv; ++j)
  555. {
  556. v = &header->verts[p->v[j]*3];
  557. vmin(bmin, v);
  558. vmax(bmax, v);
  559. }
  560. if (overlapBoxes(qmin,qmax, bmin,bmax))
  561. {
  562. if (n < maxPolys)
  563. polys[n++] = base | (dtTilePolyRef)i;
  564. }
  565. }
  566. return n;
  567. }
  568. int dtTiledNavMesh::queryPolygons(const float* center, const float* extents,
  569. dtTilePolyRef* polys, const int maxPolys)
  570. {
  571. float bmin[3], bmax[3];
  572. bmin[0] = center[0] - extents[0];
  573. bmin[1] = center[1] - extents[1];
  574. bmin[2] = center[2] - extents[2];
  575. bmax[0] = center[0] + extents[0];
  576. bmax[1] = center[1] + extents[1];
  577. bmax[2] = center[2] + extents[2];
  578. // Find tiles the query touches.
  579. const int minx = (int)floorf((bmin[0]-m_orig[0]) / m_tileSize);
  580. const int maxx = (int)ceilf((bmax[0]-m_orig[0]) / m_tileSize);
  581. const int miny = (int)floorf((bmin[2]-m_orig[2]) / m_tileSize);
  582. const int maxy = (int)ceilf((bmax[2]-m_orig[2]) / m_tileSize);
  583. int n = 0;
  584. for (int y = miny; y < maxy; ++y)
  585. {
  586. for (int x = minx; x < maxx; ++x)
  587. {
  588. dtTile* tile = getTileAt(x,y);
  589. if (!tile) continue;
  590. n += queryTilePolygons(tile, bmin, bmax, polys+n, maxPolys-n);
  591. if (n >= maxPolys) return n;
  592. }
  593. }
  594. return n;
  595. }
  596. int dtTiledNavMesh::findPath(dtTilePolyRef startRef, dtTilePolyRef endRef,
  597. const float* startPos, const float* endPos,
  598. dtTilePolyRef* path, const int maxPathSize)
  599. {
  600. if (!startRef || !endRef)
  601. return 0;
  602. if (!maxPathSize)
  603. return 0;
  604. if (!getPolyByRef(startRef) || !getPolyByRef(endRef))
  605. return 0;
  606. if (startRef == endRef)
  607. {
  608. path[0] = startRef;
  609. return 1;
  610. }
  611. if (!m_nodePool || !m_openList)
  612. return 0;
  613. m_nodePool->clear();
  614. m_openList->clear();
  615. static const float H_SCALE = 1.1f; // Heuristic scale.
  616. dtNode* startNode = m_nodePool->getNode(startRef);
  617. startNode->pidx = 0;
  618. startNode->cost = 0;
  619. startNode->total = vdist(startPos, endPos) * H_SCALE;
  620. startNode->id = startRef;
  621. startNode->flags = DT_NODE_OPEN;
  622. m_openList->push(startNode);
  623. dtNode* lastBestNode = startNode;
  624. float lastBestNodeCost = startNode->total;
  625. while (!m_openList->empty())
  626. {
  627. dtNode* bestNode = m_openList->pop();
  628. if (bestNode->id == endRef)
  629. {
  630. lastBestNode = bestNode;
  631. break;
  632. }
  633. // Get poly and tile.
  634. unsigned int salt, it, ip;
  635. dtDecodeTileId(bestNode->id, salt, it, ip);
  636. // The API input has been cheked already, skip checking internal data.
  637. const dtTileHeader* header = m_tiles[it].header;
  638. const dtTilePoly* poly = &header->polys[ip];
  639. for (int i = 0; i < poly->nlinks; ++i)
  640. {
  641. dtTilePolyRef neighbour = header->links[poly->links+i].ref;
  642. if (neighbour)
  643. {
  644. // Skip parent node.
  645. if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
  646. continue;
  647. dtNode* parent = bestNode;
  648. dtNode newNode;
  649. newNode.pidx = m_nodePool->getNodeIdx(parent);
  650. newNode.id = neighbour;
  651. // Calculate cost.
  652. float p0[3], p1[3];
  653. if (!parent->pidx)
  654. vcopy(p0, startPos);
  655. else
  656. getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
  657. getEdgeMidPoint(parent->id, newNode.id, p1);
  658. newNode.cost = parent->cost + vdist(p0,p1);
  659. // Special case for last node.
  660. if (newNode.id == endRef)
  661. newNode.cost += vdist(p1, endPos);
  662. // Heuristic
  663. const float h = vdist(p1,endPos)*H_SCALE;
  664. newNode.total = newNode.cost + h;
  665. dtNode* actualNode = m_nodePool->getNode(newNode.id);
  666. if (!actualNode)
  667. continue;
  668. if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
  669. !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
  670. {
  671. actualNode->flags &= DT_NODE_CLOSED;
  672. actualNode->pidx = newNode.pidx;
  673. actualNode->cost = newNode.cost;
  674. actualNode->total = newNode.total;
  675. if (h < lastBestNodeCost)
  676. {
  677. lastBestNodeCost = h;
  678. lastBestNode = actualNode;
  679. }
  680. if (actualNode->flags & DT_NODE_OPEN)
  681. {
  682. m_openList->modify(actualNode);
  683. }
  684. else
  685. {
  686. actualNode->flags |= DT_NODE_OPEN;
  687. m_openList->push(actualNode);
  688. }
  689. }
  690. }
  691. }
  692. bestNode->flags |= DT_NODE_CLOSED;
  693. }
  694. // Reverse the path.
  695. dtNode* prev = 0;
  696. dtNode* node = lastBestNode;
  697. do
  698. {
  699. dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
  700. node->pidx = m_nodePool->getNodeIdx(prev);
  701. prev = node;
  702. node = next;
  703. }
  704. while (node);
  705. // Store path
  706. node = prev;
  707. int n = 0;
  708. do
  709. {
  710. path[n++] = node->id;
  711. node = m_nodePool->getNodeAtIdx(node->pidx);
  712. }
  713. while (node && n < maxPathSize);
  714. return n;
  715. }
  716. int dtTiledNavMesh::findStraightPath(const float* startPos, const float* endPos,
  717. const dtTilePolyRef* path, const int pathSize,
  718. float* straightPath, const int maxStraightPathSize)
  719. {
  720. if (!maxStraightPathSize)
  721. return 0;
  722. if (!path[0])
  723. return 0;
  724. int straightPathSize = 0;
  725. float closestStartPos[3];
  726. if (!closestPointToPoly(path[0], startPos, closestStartPos))
  727. return 0;
  728. // Add start point.
  729. vcopy(&straightPath[straightPathSize*3], closestStartPos);
  730. straightPathSize++;
  731. if (straightPathSize >= maxStraightPathSize)
  732. return straightPathSize;
  733. float closestEndPos[3];
  734. if (!closestPointToPoly(path[pathSize-1], endPos, closestEndPos))
  735. return 0;
  736. float portalApex[3], portalLeft[3], portalRight[3];
  737. if (pathSize > 1)
  738. {
  739. vcopy(portalApex, closestStartPos);
  740. vcopy(portalLeft, portalApex);
  741. vcopy(portalRight, portalApex);
  742. int apexIndex = 0;
  743. int leftIndex = 0;
  744. int rightIndex = 0;
  745. for (int i = 0; i < pathSize; ++i)
  746. {
  747. float left[3], right[3];
  748. if (i < pathSize-1)
  749. {
  750. // Next portal.
  751. if (!getPortalPoints(path[i], path[i+1], left, right))
  752. {
  753. if (!closestPointToPoly(path[i], endPos, closestEndPos))
  754. return 0;
  755. vcopy(&straightPath[straightPathSize*3], closestEndPos);
  756. straightPathSize++;
  757. return straightPathSize;
  758. }
  759. }
  760. else
  761. {
  762. // End of the path.
  763. vcopy(left, closestEndPos);
  764. vcopy(right, closestEndPos);
  765. }
  766. // Right vertex.
  767. if (vequal(portalApex, portalRight))
  768. {
  769. vcopy(portalRight, right);
  770. rightIndex = i;
  771. }
  772. else
  773. {
  774. if (triArea2D(portalApex, portalRight, right) <= 0.0f)
  775. {
  776. if (triArea2D(portalApex, portalLeft, right) > 0.0f)
  777. {
  778. vcopy(portalRight, right);
  779. rightIndex = i;
  780. }
  781. else
  782. {
  783. vcopy(portalApex, portalLeft);
  784. apexIndex = leftIndex;
  785. if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
  786. {
  787. vcopy(&straightPath[straightPathSize*3], portalApex);
  788. straightPathSize++;
  789. if (straightPathSize >= maxStraightPathSize)
  790. return straightPathSize;
  791. }
  792. vcopy(portalLeft, portalApex);
  793. vcopy(portalRight, portalApex);
  794. leftIndex = apexIndex;
  795. rightIndex = apexIndex;
  796. // Restart
  797. i = apexIndex;
  798. continue;
  799. }
  800. }
  801. }
  802. // Left vertex.
  803. if (vequal(portalApex, portalLeft))
  804. {
  805. vcopy(portalLeft, left);
  806. leftIndex = i;
  807. }
  808. else
  809. {
  810. if (triArea2D(portalApex, portalLeft, left) >= 0.0f)
  811. {
  812. if (triArea2D(portalApex, portalRight, left) < 0.0f)
  813. {
  814. vcopy(portalLeft, left);
  815. leftIndex = i;
  816. }
  817. else
  818. {
  819. vcopy(portalApex, portalRight);
  820. apexIndex = rightIndex;
  821. if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
  822. {
  823. vcopy(&straightPath[straightPathSize*3], portalApex);
  824. straightPathSize++;
  825. if (straightPathSize >= maxStraightPathSize)
  826. return straightPathSize;
  827. }
  828. vcopy(portalLeft, portalApex);
  829. vcopy(portalRight, portalApex);
  830. leftIndex = apexIndex;
  831. rightIndex = apexIndex;
  832. // Restart
  833. i = apexIndex;
  834. continue;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. // Add end point.
  841. vcopy(&straightPath[straightPathSize*3], closestEndPos);
  842. straightPathSize++;
  843. return straightPathSize;
  844. }
  845. // Returns portal points between two polygons.
  846. bool dtTiledNavMesh::getPortalPoints(dtTilePolyRef from, dtTilePolyRef to, float* left, float* right) const
  847. {
  848. unsigned int salt, it, ip;
  849. dtDecodeTileId(from, salt, it, ip);
  850. if (it >= DT_MAX_TILES) return false;
  851. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
  852. if (ip >= (unsigned int)m_tiles[it].header->npolys) return false;
  853. const dtTileHeader* fromHeader = m_tiles[it].header;
  854. const dtTilePoly* fromPoly = &fromHeader->polys[ip];
  855. for (int i = 0; i < fromPoly->nlinks; ++i)
  856. {
  857. const dtTileLink* link = &fromHeader->links[fromPoly->links+i];
  858. if (link->ref == to)
  859. {
  860. // Find portal vertices.
  861. const int v0 = fromPoly->v[link->e];
  862. const int v1 = fromPoly->v[(link->e+1) % fromPoly->nv];
  863. vcopy(left, &fromHeader->verts[v0*3]);
  864. vcopy(right, &fromHeader->verts[v1*3]);
  865. // If the link is at tile boundary, clamp the vertices to
  866. // the link width.
  867. if (link->side == 0 || link->side == 2)
  868. {
  869. // Unpack portal limits.
  870. const float smin = min(left[2],right[2]);
  871. const float smax = max(left[2],right[2]);
  872. const float s = (smax-smin) / 255.0f;
  873. const float lmin = smin + link->bmin*s;
  874. const float lmax = smin + link->bmax*s;
  875. left[2] = max(left[2],lmin);
  876. left[2] = min(left[2],lmax);
  877. right[2] = max(right[2],lmin);
  878. right[2] = min(right[2],lmax);
  879. }
  880. else if (link->side == 1 || link->side == 3)
  881. {
  882. // Unpack portal limits.
  883. const float smin = min(left[0],right[0]);
  884. const float smax = max(left[0],right[0]);
  885. const float s = (smax-smin) / 255.0f;
  886. const float lmin = smin + link->bmin*s;
  887. const float lmax = smin + link->bmax*s;
  888. left[0] = max(left[0],lmin);
  889. left[0] = min(left[0],lmax);
  890. right[0] = max(right[0],lmin);
  891. right[0] = min(right[0],lmax);
  892. }
  893. return true;
  894. }
  895. }
  896. return false;
  897. }
  898. // Returns edge mid point between two polygons.
  899. bool dtTiledNavMesh::getEdgeMidPoint(dtTilePolyRef from, dtTilePolyRef to, float* mid) const
  900. {
  901. float left[3], right[3];
  902. if (!getPortalPoints(from, to, left,right)) return false;
  903. mid[0] = (left[0]+right[0])*0.5f;
  904. mid[1] = (left[1]+right[1])*0.5f;
  905. mid[2] = (left[2]+right[2])*0.5f;
  906. return true;
  907. }
  908. int dtTiledNavMesh::raycast(dtTilePolyRef centerRef, const float* startPos, const float* endPos,
  909. float& t, dtTilePolyRef* path, const int pathSize)
  910. {
  911. t = 0;
  912. if (!centerRef || !getPolyByRef(centerRef))
  913. return 0;
  914. dtTilePolyRef curRef = centerRef;
  915. float verts[DT_TILE_VERTS_PER_POLYGON*3];
  916. int n = 0;
  917. while (curRef)
  918. {
  919. // Cast ray against current polygon.
  920. // The API input has been cheked already, skip checking internal data.
  921. unsigned int salt, it, ip;
  922. dtDecodeTileId(curRef, salt, it, ip);
  923. const dtTileHeader* header = m_tiles[it].header;
  924. const dtTilePoly* poly = &header->polys[ip];
  925. // Collect vertices.
  926. int nv = 0;
  927. for (int i = 0; i < (int)poly->nv; ++i)
  928. {
  929. vcopy(&verts[nv*3], &header->verts[poly->v[i]*3]);
  930. nv++;
  931. }
  932. if (nv < 3)
  933. {
  934. // Hit bad polygon, report hit.
  935. return n;
  936. }
  937. float tmin, tmax;
  938. int segMin, segMax;
  939. if (!intersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax))
  940. {
  941. // Could not hit the polygon, keep the old t and report hit.
  942. return n;
  943. }
  944. // Keep track of furthest t so far.
  945. if (tmax > t)
  946. t = tmax;
  947. if (n < pathSize)
  948. path[n++] = curRef;
  949. // Follow neighbours.
  950. dtTilePolyRef nextRef = 0;
  951. for (int i = 0; i < poly->nlinks; ++i)
  952. {
  953. const dtTileLink* link = &header->links[poly->links+i];
  954. if ((int)link->e == segMax)
  955. {
  956. // If the link is internal, just return the ref.
  957. if (link->side == 0xff)
  958. {
  959. nextRef = link->ref;
  960. break;
  961. }
  962. // If the link is at tile boundary,
  963. const int v0 = poly->v[link->e];
  964. const int v1 = poly->v[(link->e+1) % poly->nv];
  965. const float* left = &header->verts[v0*3];
  966. const float* right = &header->verts[v1*3];
  967. // Check that the intersection lies inside the link portal.
  968. if (link->side == 0 || link->side == 2)
  969. {
  970. // Calculate link size.
  971. const float smin = min(left[2],right[2]);
  972. const float smax = max(left[2],right[2]);
  973. const float s = (smax-smin) / 255.0f;
  974. const float lmin = smin + link->bmin*s;
  975. const float lmax = smin + link->bmax*s;
  976. // Find Z intersection.
  977. float z = startPos[2] + (endPos[2]-startPos[2])*tmax;
  978. if (z >= lmin && z <= lmax)
  979. {
  980. nextRef = link->ref;
  981. break;
  982. }
  983. }
  984. else if (link->side == 1 || link->side == 3)
  985. {
  986. // Calculate link size.
  987. const float smin = min(left[0],right[0]);
  988. const float smax = max(left[0],right[0]);
  989. const float s = (smax-smin) / 255.0f;
  990. const float lmin = smin + link->bmin*s;
  991. const float lmax = smin + link->bmax*s;
  992. // Find X intersection.
  993. float x = startPos[0] + (endPos[0]-startPos[0])*tmax;
  994. if (x >= lmin && x <= lmax)
  995. {
  996. nextRef = link->ref;
  997. break;
  998. }
  999. }
  1000. }
  1001. }
  1002. if (!nextRef)
  1003. {
  1004. // No neighbour, we hit a wall.
  1005. return n;
  1006. }
  1007. // No hit, advance to neighbour polygon.
  1008. curRef = nextRef;
  1009. }
  1010. return n;
  1011. }
  1012. int dtTiledNavMesh::findPolysAround(dtTilePolyRef centerRef, const float* centerPos, float radius,
  1013. dtTilePolyRef* resultRef, dtTilePolyRef* resultParent, float* resultCost,
  1014. const int maxResult)
  1015. {
  1016. if (!centerRef) return 0;
  1017. if (!getPolyByRef(centerRef)) return 0;
  1018. if (!m_nodePool || !m_openList) return 0;
  1019. m_nodePool->clear();
  1020. m_openList->clear();
  1021. dtNode* startNode = m_nodePool->getNode(centerRef);
  1022. startNode->pidx = 0;
  1023. startNode->cost = 0;
  1024. startNode->total = 0;
  1025. startNode->id = centerRef;
  1026. startNode->flags = DT_NODE_OPEN;
  1027. m_openList->push(startNode);
  1028. int n = 0;
  1029. if (n < maxResult)
  1030. {
  1031. if (resultRef)
  1032. resultRef[n] = startNode->id;
  1033. if (resultParent)
  1034. resultParent[n] = 0;
  1035. if (resultCost)
  1036. resultCost[n] = 0;
  1037. ++n;
  1038. }
  1039. const float radiusSqr = sqr(radius);
  1040. while (!m_openList->empty())
  1041. {
  1042. dtNode* bestNode = m_openList->pop();
  1043. // Get poly and tile.
  1044. unsigned int salt, it, ip;
  1045. dtDecodeTileId(bestNode->id, salt, it, ip);
  1046. // The API input has been cheked already, skip checking internal data.
  1047. const dtTileHeader* header = m_tiles[it].header;
  1048. const dtTilePoly* poly = &header->polys[ip];
  1049. for (int i = 0; i < poly->nlinks; ++i)
  1050. {
  1051. const dtTileLink* link = &header->links[poly->links+i];
  1052. dtTilePolyRef neighbour = link->ref;
  1053. if (neighbour)
  1054. {
  1055. // Skip parent node.
  1056. if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
  1057. continue;
  1058. // Calc distance to the edge.
  1059. const float* va = &header->verts[poly->v[link->e]*3];
  1060. const float* vb = &header->verts[poly->v[(link->e+1)%poly->nv]*3];
  1061. float tseg;
  1062. float distSqr = distancePtSegSqr2D(centerPos, va, vb, tseg);
  1063. // If the circle is not touching the next polygon, skip it.
  1064. if (distSqr > radiusSqr)
  1065. continue;
  1066. dtNode* parent = bestNode;
  1067. dtNode newNode;
  1068. newNode.pidx = m_nodePool->getNodeIdx(parent);
  1069. newNode.id = neighbour;
  1070. // Cost
  1071. float p0[3], p1[3];
  1072. if (!parent->pidx)
  1073. vcopy(p0, centerPos);
  1074. else
  1075. getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
  1076. getEdgeMidPoint(parent->id, newNode.id, p1);
  1077. newNode.total = parent->total + vdist(p0,p1);
  1078. dtNode* actualNode = m_nodePool->getNode(newNode.id);
  1079. if (!actualNode)
  1080. continue;
  1081. if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
  1082. !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
  1083. {
  1084. actualNode->flags &= ~DT_NODE_CLOSED;
  1085. actualNode->pidx = newNode.pidx;
  1086. actualNode->total = newNode.total;
  1087. if (actualNode->flags & DT_NODE_OPEN)
  1088. {
  1089. m_openList->modify(actualNode);
  1090. }
  1091. else
  1092. {
  1093. if (n < maxResult)
  1094. {
  1095. if (resultRef)
  1096. resultRef[n] = actualNode->id;
  1097. if (resultParent)
  1098. resultParent[n] = m_nodePool->getNodeAtIdx(actualNode->pidx)->id;
  1099. if (resultCost)
  1100. resultCost[n] = actualNode->total;
  1101. ++n;
  1102. }
  1103. actualNode->flags = DT_NODE_OPEN;
  1104. m_openList->push(actualNode);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. return n;
  1111. }
  1112. float dtTiledNavMesh::findDistanceToWall(dtTilePolyRef centerRef, const float* centerPos, float maxRadius,
  1113. float* hitPos, float* hitNormal)
  1114. {
  1115. if (!centerRef) return 0;
  1116. if (!getPolyByRef(centerRef)) return 0;
  1117. if (!m_nodePool || !m_openList) return 0;
  1118. m_nodePool->clear();
  1119. m_openList->clear();
  1120. dtNode* startNode = m_nodePool->getNode(centerRef);
  1121. startNode->pidx = 0;
  1122. startNode->cost = 0;
  1123. startNode->total = 0;
  1124. startNode->id = centerRef;
  1125. startNode->flags = DT_NODE_OPEN;
  1126. m_openList->push(startNode);
  1127. float radiusSqr = sqr(maxRadius);
  1128. while (!m_openList->empty())
  1129. {
  1130. dtNode* bestNode = m_openList->pop();
  1131. // Get poly and tile.
  1132. unsigned int salt, it, ip;
  1133. dtDecodeTileId(bestNode->id, salt, it, ip);
  1134. // The API input has been cheked already, skip checking internal data.
  1135. const dtTileHeader* header = m_tiles[it].header;
  1136. const dtTilePoly* poly = &header->polys[ip];
  1137. // Hit test walls.
  1138. for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
  1139. {
  1140. // Skip non-solid edges.
  1141. if (poly->n[j] & 0x8000)
  1142. {
  1143. // Tile border.
  1144. bool solid = true;
  1145. for (int i = 0; i < poly->nlinks; ++i)
  1146. {
  1147. const dtTileLink* link = &header->links[poly->links+i];
  1148. if (link->e == j && link->ref != 0)
  1149. {
  1150. solid = false;
  1151. break;
  1152. }
  1153. }
  1154. if (!solid) continue;
  1155. }
  1156. else if (poly->n[j])
  1157. {
  1158. // Internal edge
  1159. continue;
  1160. }
  1161. // Calc distance to the edge.
  1162. const float* vj = &header->verts[poly->v[j]*3];
  1163. const float* vi = &header->verts[poly->v[i]*3];
  1164. float tseg;
  1165. float distSqr = distancePtSegSqr2D(centerPos, vj, vi, tseg);
  1166. // Edge is too far, skip.
  1167. if (distSqr > radiusSqr)
  1168. continue;
  1169. // Hit wall, update radius.
  1170. radiusSqr = distSqr;
  1171. // Calculate hit pos.
  1172. hitPos[0] = vj[0] + (vi[0] - vj[0])*tseg;
  1173. hitPos[1] = vj[1] + (vi[1] - vj[1])*tseg;
  1174. hitPos[2] = vj[2] + (vi[2] - vj[2])*tseg;
  1175. }
  1176. for (int i = 0; i < poly->nlinks; ++i)
  1177. {
  1178. const dtTileLink* link = &header->links[poly->links+i];
  1179. dtTilePolyRef neighbour = link->ref;
  1180. if (neighbour)
  1181. {
  1182. // Skip parent node.
  1183. if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
  1184. continue;
  1185. // Calc distance to the edge.
  1186. const float* va = &header->verts[poly->v[link->e]*3];
  1187. const float* vb = &header->verts[poly->v[(link->e+1)%poly->nv]*3];
  1188. float tseg;
  1189. float distSqr = distancePtSegSqr2D(centerPos, va, vb, tseg);
  1190. // If the circle is not touching the next polygon, skip it.
  1191. if (distSqr > radiusSqr)
  1192. continue;
  1193. dtNode* parent = bestNode;
  1194. dtNode newNode;
  1195. newNode.pidx = m_nodePool->getNodeIdx(parent);
  1196. newNode.id = neighbour;
  1197. float p0[3], p1[3];
  1198. if (!parent->pidx)
  1199. vcopy(p0, centerPos);
  1200. else
  1201. getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
  1202. getEdgeMidPoint(parent->id, newNode.id, p1);
  1203. newNode.total = parent->total + vdist(p0,p1);
  1204. dtNode* actualNode = m_nodePool->getNode(newNode.id);
  1205. if (!actualNode)
  1206. continue;
  1207. if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
  1208. !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
  1209. {
  1210. actualNode->flags &= ~DT_NODE_CLOSED;
  1211. actualNode->pidx = newNode.pidx;
  1212. actualNode->total = newNode.total;
  1213. if (actualNode->flags & DT_NODE_OPEN)
  1214. {
  1215. m_openList->modify(actualNode);
  1216. }
  1217. else
  1218. {
  1219. actualNode->flags = DT_NODE_OPEN;
  1220. m_openList->push(actualNode);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. }
  1226. // Calc hit normal.
  1227. vsub(hitNormal, centerPos, hitPos);
  1228. vnormalize(hitNormal);
  1229. return sqrtf(radiusSqr);
  1230. }
  1231. const dtTilePoly* dtTiledNavMesh::getPolyByRef(dtTilePolyRef ref) const
  1232. {
  1233. unsigned int salt, it, ip;
  1234. dtDecodeTileId(ref, salt, it, ip);
  1235. if (it >= DT_MAX_TILES) return 0;
  1236. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
  1237. if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
  1238. return &m_tiles[it].header->polys[ip];
  1239. }
  1240. const float* dtTiledNavMesh::getPolyVertsByRef(dtTilePolyRef ref) const
  1241. {
  1242. unsigned int salt, it, ip;
  1243. dtDecodeTileId(ref, salt, it, ip);
  1244. if (it >= DT_MAX_TILES) return 0;
  1245. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
  1246. if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
  1247. return m_tiles[it].header->verts;
  1248. }
  1249. const dtTileLink* dtTiledNavMesh::getPolyLinksByRef(dtTilePolyRef ref) const
  1250. {
  1251. unsigned int salt, it, ip;
  1252. dtDecodeTileId(ref, salt, it, ip);
  1253. if (it >= DT_MAX_TILES) return 0;
  1254. if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
  1255. if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
  1256. return m_tiles[it].header->links;
  1257. }