NavMeshTesterTool.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. //
  2. // Copyright (c) 2009-2010 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. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "SDL.h"
  24. #include "SDL_opengl.h"
  25. #ifdef __APPLE__
  26. # include <OpenGL/glu.h>
  27. #else
  28. # include <GL/glu.h>
  29. #endif
  30. #include "imgui.h"
  31. #include "NavMeshTesterTool.h"
  32. #include "Sample.h"
  33. #include "Recast.h"
  34. #include "RecastDebugDraw.h"
  35. #include "DetourNavMesh.h"
  36. #include "DetourNavMeshBuilder.h"
  37. #include "DetourDebugDraw.h"
  38. #include "DetourCommon.h"
  39. #ifdef WIN32
  40. # define snprintf _snprintf
  41. #endif
  42. // Uncomment this to dump all the requests in stdout.
  43. #define DUMP_REQS
  44. // Returns a random number [0..1]
  45. static float frand()
  46. {
  47. // return ((float)(rand() & 0xffff)/(float)0xffff);
  48. return (float)rand()/(float)RAND_MAX;
  49. }
  50. inline bool inRange(const float* v1, const float* v2, const float r, const float h)
  51. {
  52. const float dx = v2[0] - v1[0];
  53. const float dy = v2[1] - v1[1];
  54. const float dz = v2[2] - v1[2];
  55. return (dx*dx + dz*dz) < r*r && fabsf(dy) < h;
  56. }
  57. static int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath,
  58. const dtPolyRef* visited, const int nvisited)
  59. {
  60. int furthestPath = -1;
  61. int furthestVisited = -1;
  62. // Find furthest common polygon.
  63. for (int i = npath-1; i >= 0; --i)
  64. {
  65. bool found = false;
  66. for (int j = nvisited-1; j >= 0; --j)
  67. {
  68. if (path[i] == visited[j])
  69. {
  70. furthestPath = i;
  71. furthestVisited = j;
  72. found = true;
  73. }
  74. }
  75. if (found)
  76. break;
  77. }
  78. // If no intersection found just return current path.
  79. if (furthestPath == -1 || furthestVisited == -1)
  80. return npath;
  81. // Concatenate paths.
  82. // Adjust beginning of the buffer to include the visited.
  83. const int req = nvisited - furthestVisited;
  84. const int orig = rcMin(furthestPath+1, npath);
  85. int size = rcMax(0, npath-orig);
  86. if (req+size > maxPath)
  87. size = maxPath-req;
  88. if (size)
  89. memmove(path+req, path+orig, size*sizeof(dtPolyRef));
  90. // Store visited
  91. for (int i = 0; i < req; ++i)
  92. path[i] = visited[(nvisited-1)-i];
  93. return req+size;
  94. }
  95. // This function checks if the path has a small U-turn, that is,
  96. // a polygon further in the path is adjacent to the first polygon
  97. // in the path. If that happens, a shortcut is taken.
  98. // This can happen if the target (T) location is at tile boundary,
  99. // and we're (S) approaching it parallel to the tile edge.
  100. // The choice at the vertex can be arbitrary,
  101. // +---+---+
  102. // |:::|:::|
  103. // +-S-+-T-+
  104. // |:::| | <-- the step can end up in here, resulting U-turn path.
  105. // +---+---+
  106. static int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery)
  107. {
  108. if (npath < 3)
  109. return npath;
  110. // Get connected polygons
  111. static const int maxNeis = 16;
  112. dtPolyRef neis[maxNeis];
  113. int nneis = 0;
  114. const dtMeshTile* tile = 0;
  115. const dtPoly* poly = 0;
  116. if (dtStatusFailed(navQuery->getAttachedNavMesh()->getTileAndPolyByRef(path[0], &tile, &poly)))
  117. return npath;
  118. for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  119. {
  120. const dtLink* link = &tile->links[k];
  121. if (link->ref != 0)
  122. {
  123. if (nneis < maxNeis)
  124. neis[nneis++] = link->ref;
  125. }
  126. }
  127. // If any of the neighbour polygons is within the next few polygons
  128. // in the path, short cut to that polygon directly.
  129. static const int maxLookAhead = 6;
  130. int cut = 0;
  131. for (int i = dtMin(maxLookAhead, npath) - 1; i > 1 && cut == 0; i--) {
  132. for (int j = 0; j < nneis; j++)
  133. {
  134. if (path[i] == neis[j]) {
  135. cut = i;
  136. break;
  137. }
  138. }
  139. }
  140. if (cut > 1)
  141. {
  142. int offset = cut-1;
  143. npath -= offset;
  144. for (int i = 1; i < npath; i++)
  145. path[i] = path[i+offset];
  146. }
  147. return npath;
  148. }
  149. static bool getSteerTarget(dtNavMeshQuery* navQuery, const float* startPos, const float* endPos,
  150. const float minTargetDist,
  151. const dtPolyRef* path, const int pathSize,
  152. float* steerPos, unsigned char& steerPosFlag, dtPolyRef& steerPosRef,
  153. float* outPoints = 0, int* outPointCount = 0)
  154. {
  155. // Find steer target.
  156. static const int MAX_STEER_POINTS = 3;
  157. float steerPath[MAX_STEER_POINTS*3];
  158. unsigned char steerPathFlags[MAX_STEER_POINTS];
  159. dtPolyRef steerPathPolys[MAX_STEER_POINTS];
  160. int nsteerPath = 0;
  161. navQuery->findStraightPath(startPos, endPos, path, pathSize,
  162. steerPath, steerPathFlags, steerPathPolys, &nsteerPath, MAX_STEER_POINTS);
  163. if (!nsteerPath)
  164. return false;
  165. if (outPoints && outPointCount)
  166. {
  167. *outPointCount = nsteerPath;
  168. for (int i = 0; i < nsteerPath; ++i)
  169. dtVcopy(&outPoints[i*3], &steerPath[i*3]);
  170. }
  171. // Find vertex far enough to steer to.
  172. int ns = 0;
  173. while (ns < nsteerPath)
  174. {
  175. // Stop at Off-Mesh link or when point is further than slop away.
  176. if ((steerPathFlags[ns] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ||
  177. !inRange(&steerPath[ns*3], startPos, minTargetDist, 1000.0f))
  178. break;
  179. ns++;
  180. }
  181. // Failed to find good point to steer to.
  182. if (ns >= nsteerPath)
  183. return false;
  184. dtVcopy(steerPos, &steerPath[ns*3]);
  185. steerPos[1] = startPos[1];
  186. steerPosFlag = steerPathFlags[ns];
  187. steerPosRef = steerPathPolys[ns];
  188. return true;
  189. }
  190. NavMeshTesterTool::NavMeshTesterTool() :
  191. m_sample(0),
  192. m_navMesh(0),
  193. m_navQuery(0),
  194. m_pathFindStatus(DT_FAILURE),
  195. m_toolMode(TOOLMODE_PATHFIND_FOLLOW),
  196. m_straightPathOptions(0),
  197. m_startRef(0),
  198. m_endRef(0),
  199. m_npolys(0),
  200. m_nstraightPath(0),
  201. m_nsmoothPath(0),
  202. m_nrandPoints(0),
  203. m_randPointsInCircle(false),
  204. m_hitResult(false),
  205. m_distanceToWall(0),
  206. m_sposSet(false),
  207. m_eposSet(false),
  208. m_pathIterNum(0),
  209. m_pathIterPolyCount(0),
  210. m_steerPointCount(0)
  211. {
  212. m_filter.setIncludeFlags(SAMPLE_POLYFLAGS_ALL ^ SAMPLE_POLYFLAGS_DISABLED);
  213. m_filter.setExcludeFlags(0);
  214. m_polyPickExt[0] = 2;
  215. m_polyPickExt[1] = 4;
  216. m_polyPickExt[2] = 2;
  217. m_neighbourhoodRadius = 2.5f;
  218. m_randomRadius = 5.0f;
  219. }
  220. void NavMeshTesterTool::init(Sample* sample)
  221. {
  222. m_sample = sample;
  223. m_navMesh = sample->getNavMesh();
  224. m_navQuery = sample->getNavMeshQuery();
  225. recalc();
  226. if (m_navQuery)
  227. {
  228. // Change costs.
  229. m_filter.setAreaCost(SAMPLE_POLYAREA_GROUND, 1.0f);
  230. m_filter.setAreaCost(SAMPLE_POLYAREA_WATER, 10.0f);
  231. m_filter.setAreaCost(SAMPLE_POLYAREA_ROAD, 1.0f);
  232. m_filter.setAreaCost(SAMPLE_POLYAREA_DOOR, 1.0f);
  233. m_filter.setAreaCost(SAMPLE_POLYAREA_GRASS, 2.0f);
  234. m_filter.setAreaCost(SAMPLE_POLYAREA_JUMP, 1.5f);
  235. }
  236. m_neighbourhoodRadius = sample->getAgentRadius() * 20.0f;
  237. m_randomRadius = sample->getAgentRadius() * 30.0f;
  238. }
  239. void NavMeshTesterTool::handleMenu()
  240. {
  241. if (imguiCheck("Pathfind Follow", m_toolMode == TOOLMODE_PATHFIND_FOLLOW))
  242. {
  243. m_toolMode = TOOLMODE_PATHFIND_FOLLOW;
  244. recalc();
  245. }
  246. if (imguiCheck("Pathfind Straight", m_toolMode == TOOLMODE_PATHFIND_STRAIGHT))
  247. {
  248. m_toolMode = TOOLMODE_PATHFIND_STRAIGHT;
  249. recalc();
  250. }
  251. if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT)
  252. {
  253. imguiIndent();
  254. imguiLabel("Vertices at crossings");
  255. if (imguiCheck("None", m_straightPathOptions == 0))
  256. {
  257. m_straightPathOptions = 0;
  258. recalc();
  259. }
  260. if (imguiCheck("Area", m_straightPathOptions == DT_STRAIGHTPATH_AREA_CROSSINGS))
  261. {
  262. m_straightPathOptions = DT_STRAIGHTPATH_AREA_CROSSINGS;
  263. recalc();
  264. }
  265. if (imguiCheck("All", m_straightPathOptions == DT_STRAIGHTPATH_ALL_CROSSINGS))
  266. {
  267. m_straightPathOptions = DT_STRAIGHTPATH_ALL_CROSSINGS;
  268. recalc();
  269. }
  270. imguiUnindent();
  271. }
  272. if (imguiCheck("Pathfind Sliced", m_toolMode == TOOLMODE_PATHFIND_SLICED))
  273. {
  274. m_toolMode = TOOLMODE_PATHFIND_SLICED;
  275. recalc();
  276. }
  277. imguiSeparator();
  278. if (imguiCheck("Distance to Wall", m_toolMode == TOOLMODE_DISTANCE_TO_WALL))
  279. {
  280. m_toolMode = TOOLMODE_DISTANCE_TO_WALL;
  281. recalc();
  282. }
  283. imguiSeparator();
  284. if (imguiCheck("Raycast", m_toolMode == TOOLMODE_RAYCAST))
  285. {
  286. m_toolMode = TOOLMODE_RAYCAST;
  287. recalc();
  288. }
  289. imguiSeparator();
  290. if (imguiCheck("Find Polys in Circle", m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE))
  291. {
  292. m_toolMode = TOOLMODE_FIND_POLYS_IN_CIRCLE;
  293. recalc();
  294. }
  295. if (imguiCheck("Find Polys in Shape", m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE))
  296. {
  297. m_toolMode = TOOLMODE_FIND_POLYS_IN_SHAPE;
  298. recalc();
  299. }
  300. imguiSeparator();
  301. if (imguiCheck("Find Local Neighbourhood", m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD))
  302. {
  303. m_toolMode = TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD;
  304. recalc();
  305. }
  306. imguiSeparator();
  307. if (imguiButton("Set Random Start"))
  308. {
  309. dtStatus status = m_navQuery->findRandomPoint(&m_filter, frand, &m_startRef, m_spos);
  310. if (dtStatusSucceed(status))
  311. {
  312. m_sposSet = true;
  313. recalc();
  314. }
  315. }
  316. if (imguiButton("Set Random End", m_sposSet))
  317. {
  318. if (m_sposSet)
  319. {
  320. dtStatus status = m_navQuery->findRandomPointAroundCircle(m_startRef, m_spos, m_randomRadius, &m_filter, frand, &m_endRef, m_epos);
  321. if (dtStatusSucceed(status))
  322. {
  323. m_eposSet = true;
  324. recalc();
  325. }
  326. }
  327. }
  328. imguiSeparator();
  329. if (imguiButton("Make Random Points"))
  330. {
  331. m_randPointsInCircle = false;
  332. m_nrandPoints = 0;
  333. for (int i = 0; i < MAX_RAND_POINTS; i++)
  334. {
  335. float pt[3];
  336. dtPolyRef ref;
  337. dtStatus status = m_navQuery->findRandomPoint(&m_filter, frand, &ref, pt);
  338. if (dtStatusSucceed(status))
  339. {
  340. dtVcopy(&m_randPoints[m_nrandPoints*3], pt);
  341. m_nrandPoints++;
  342. }
  343. }
  344. }
  345. if (imguiButton("Make Random Points Around", m_sposSet))
  346. {
  347. if (m_sposSet)
  348. {
  349. m_nrandPoints = 0;
  350. m_randPointsInCircle = true;
  351. for (int i = 0; i < MAX_RAND_POINTS; i++)
  352. {
  353. float pt[3];
  354. dtPolyRef ref;
  355. dtStatus status = m_navQuery->findRandomPointAroundCircle(m_startRef, m_spos, m_randomRadius, &m_filter, frand, &ref, pt);
  356. if (dtStatusSucceed(status))
  357. {
  358. dtVcopy(&m_randPoints[m_nrandPoints*3], pt);
  359. m_nrandPoints++;
  360. }
  361. }
  362. }
  363. }
  364. imguiSeparator();
  365. imguiLabel("Include Flags");
  366. imguiIndent();
  367. if (imguiCheck("Walk", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_WALK) != 0))
  368. {
  369. m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_WALK);
  370. recalc();
  371. }
  372. if (imguiCheck("Swim", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_SWIM) != 0))
  373. {
  374. m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_SWIM);
  375. recalc();
  376. }
  377. if (imguiCheck("Door", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_DOOR) != 0))
  378. {
  379. m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_DOOR);
  380. recalc();
  381. }
  382. if (imguiCheck("Jump", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_JUMP) != 0))
  383. {
  384. m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_JUMP);
  385. recalc();
  386. }
  387. imguiUnindent();
  388. imguiSeparator();
  389. imguiLabel("Exclude Flags");
  390. imguiIndent();
  391. if (imguiCheck("Walk", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_WALK) != 0))
  392. {
  393. m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_WALK);
  394. recalc();
  395. }
  396. if (imguiCheck("Swim", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_SWIM) != 0))
  397. {
  398. m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_SWIM);
  399. recalc();
  400. }
  401. if (imguiCheck("Door", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_DOOR) != 0))
  402. {
  403. m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_DOOR);
  404. recalc();
  405. }
  406. if (imguiCheck("Jump", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_JUMP) != 0))
  407. {
  408. m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_JUMP);
  409. recalc();
  410. }
  411. imguiUnindent();
  412. imguiSeparator();
  413. }
  414. void NavMeshTesterTool::handleClick(const float* /*s*/, const float* p, bool shift)
  415. {
  416. if (shift)
  417. {
  418. m_sposSet = true;
  419. dtVcopy(m_spos, p);
  420. }
  421. else
  422. {
  423. m_eposSet = true;
  424. dtVcopy(m_epos, p);
  425. }
  426. recalc();
  427. }
  428. void NavMeshTesterTool::handleStep()
  429. {
  430. }
  431. void NavMeshTesterTool::handleToggle()
  432. {
  433. // TODO: merge separate to a path iterator. Use same code in recalc() too.
  434. if (m_toolMode != TOOLMODE_PATHFIND_FOLLOW)
  435. return;
  436. if (!m_sposSet || !m_eposSet || !m_startRef || !m_endRef)
  437. return;
  438. static const float STEP_SIZE = 0.5f;
  439. static const float SLOP = 0.01f;
  440. if (m_pathIterNum == 0)
  441. {
  442. m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
  443. m_nsmoothPath = 0;
  444. m_pathIterPolyCount = m_npolys;
  445. if (m_pathIterPolyCount)
  446. memcpy(m_pathIterPolys, m_polys, sizeof(dtPolyRef)*m_pathIterPolyCount);
  447. if (m_pathIterPolyCount)
  448. {
  449. // Iterate over the path to find smooth path on the detail mesh surface.
  450. m_navQuery->closestPointOnPoly(m_startRef, m_spos, m_iterPos, 0);
  451. m_navQuery->closestPointOnPoly(m_pathIterPolys[m_pathIterPolyCount-1], m_epos, m_targetPos, 0);
  452. m_nsmoothPath = 0;
  453. dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
  454. m_nsmoothPath++;
  455. }
  456. }
  457. dtVcopy(m_prevIterPos, m_iterPos);
  458. m_pathIterNum++;
  459. if (!m_pathIterPolyCount)
  460. return;
  461. if (m_nsmoothPath >= MAX_SMOOTH)
  462. return;
  463. // Move towards target a small advancement at a time until target reached or
  464. // when ran out of memory to store the path.
  465. // Find location to steer towards.
  466. float steerPos[3];
  467. unsigned char steerPosFlag;
  468. dtPolyRef steerPosRef;
  469. if (!getSteerTarget(m_navQuery, m_iterPos, m_targetPos, SLOP,
  470. m_pathIterPolys, m_pathIterPolyCount, steerPos, steerPosFlag, steerPosRef,
  471. m_steerPoints, &m_steerPointCount))
  472. return;
  473. dtVcopy(m_steerPos, steerPos);
  474. bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
  475. bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
  476. // Find movement delta.
  477. float delta[3], len;
  478. dtVsub(delta, steerPos, m_iterPos);
  479. len = sqrtf(dtVdot(delta,delta));
  480. // If the steer target is end of path or off-mesh link, do not move past the location.
  481. if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
  482. len = 1;
  483. else
  484. len = STEP_SIZE / len;
  485. float moveTgt[3];
  486. dtVmad(moveTgt, m_iterPos, delta, len);
  487. // Move
  488. float result[3];
  489. dtPolyRef visited[16];
  490. int nvisited = 0;
  491. m_navQuery->moveAlongSurface(m_pathIterPolys[0], m_iterPos, moveTgt, &m_filter,
  492. result, visited, &nvisited, 16);
  493. m_pathIterPolyCount = fixupCorridor(m_pathIterPolys, m_pathIterPolyCount, MAX_POLYS, visited, nvisited);
  494. m_pathIterPolyCount = fixupShortcuts(m_pathIterPolys, m_pathIterPolyCount, m_navQuery);
  495. float h = 0;
  496. m_navQuery->getPolyHeight(m_pathIterPolys[0], result, &h);
  497. result[1] = h;
  498. dtVcopy(m_iterPos, result);
  499. // Handle end of path and off-mesh links when close enough.
  500. if (endOfPath && inRange(m_iterPos, steerPos, SLOP, 1.0f))
  501. {
  502. // Reached end of path.
  503. dtVcopy(m_iterPos, m_targetPos);
  504. if (m_nsmoothPath < MAX_SMOOTH)
  505. {
  506. dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
  507. m_nsmoothPath++;
  508. }
  509. return;
  510. }
  511. else if (offMeshConnection && inRange(m_iterPos, steerPos, SLOP, 1.0f))
  512. {
  513. // Reached off-mesh connection.
  514. float startPos[3], endPos[3];
  515. // Advance the path up to and over the off-mesh connection.
  516. dtPolyRef prevRef = 0, polyRef = m_pathIterPolys[0];
  517. int npos = 0;
  518. while (npos < m_pathIterPolyCount && polyRef != steerPosRef)
  519. {
  520. prevRef = polyRef;
  521. polyRef = m_pathIterPolys[npos];
  522. npos++;
  523. }
  524. for (int i = npos; i < m_pathIterPolyCount; ++i)
  525. m_pathIterPolys[i-npos] = m_pathIterPolys[i];
  526. m_pathIterPolyCount -= npos;
  527. // Handle the connection.
  528. dtStatus status = m_navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
  529. if (dtStatusSucceed(status))
  530. {
  531. if (m_nsmoothPath < MAX_SMOOTH)
  532. {
  533. dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
  534. m_nsmoothPath++;
  535. // Hack to make the dotted path not visible during off-mesh connection.
  536. if (m_nsmoothPath & 1)
  537. {
  538. dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
  539. m_nsmoothPath++;
  540. }
  541. }
  542. // Move position at the other side of the off-mesh link.
  543. dtVcopy(m_iterPos, endPos);
  544. float eh = 0.0f;
  545. m_navQuery->getPolyHeight(m_pathIterPolys[0], m_iterPos, &eh);
  546. m_iterPos[1] = eh;
  547. }
  548. }
  549. // Store results.
  550. if (m_nsmoothPath < MAX_SMOOTH)
  551. {
  552. dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
  553. m_nsmoothPath++;
  554. }
  555. }
  556. void NavMeshTesterTool::handleUpdate(const float /*dt*/)
  557. {
  558. if (m_toolMode == TOOLMODE_PATHFIND_SLICED)
  559. {
  560. if (dtStatusInProgress(m_pathFindStatus))
  561. {
  562. m_pathFindStatus = m_navQuery->updateSlicedFindPath(1,0);
  563. }
  564. if (dtStatusSucceed(m_pathFindStatus))
  565. {
  566. m_navQuery->finalizeSlicedFindPath(m_polys, &m_npolys, MAX_POLYS);
  567. m_nstraightPath = 0;
  568. if (m_npolys)
  569. {
  570. // In case of partial path, make sure the end point is clamped to the last polygon.
  571. float epos[3];
  572. dtVcopy(epos, m_epos);
  573. if (m_polys[m_npolys-1] != m_endRef)
  574. m_navQuery->closestPointOnPoly(m_polys[m_npolys-1], m_epos, epos, 0);
  575. m_navQuery->findStraightPath(m_spos, epos, m_polys, m_npolys,
  576. m_straightPath, m_straightPathFlags,
  577. m_straightPathPolys, &m_nstraightPath, MAX_POLYS, DT_STRAIGHTPATH_ALL_CROSSINGS);
  578. }
  579. m_pathFindStatus = DT_FAILURE;
  580. }
  581. }
  582. }
  583. void NavMeshTesterTool::reset()
  584. {
  585. m_startRef = 0;
  586. m_endRef = 0;
  587. m_npolys = 0;
  588. m_nstraightPath = 0;
  589. m_nsmoothPath = 0;
  590. memset(m_hitPos, 0, sizeof(m_hitPos));
  591. memset(m_hitNormal, 0, sizeof(m_hitNormal));
  592. m_distanceToWall = 0;
  593. }
  594. void NavMeshTesterTool::recalc()
  595. {
  596. if (!m_navMesh)
  597. return;
  598. if (m_sposSet)
  599. m_navQuery->findNearestPoly(m_spos, m_polyPickExt, &m_filter, &m_startRef, 0);
  600. else
  601. m_startRef = 0;
  602. if (m_eposSet)
  603. m_navQuery->findNearestPoly(m_epos, m_polyPickExt, &m_filter, &m_endRef, 0);
  604. else
  605. m_endRef = 0;
  606. m_pathFindStatus = DT_FAILURE;
  607. if (m_toolMode == TOOLMODE_PATHFIND_FOLLOW)
  608. {
  609. m_pathIterNum = 0;
  610. if (m_sposSet && m_eposSet && m_startRef && m_endRef)
  611. {
  612. #ifdef DUMP_REQS
  613. printf("pi %f %f %f %f %f %f 0x%x 0x%x\n",
  614. m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
  615. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  616. #endif
  617. m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
  618. m_nsmoothPath = 0;
  619. if (m_npolys)
  620. {
  621. // Iterate over the path to find smooth path on the detail mesh surface.
  622. dtPolyRef polys[MAX_POLYS];
  623. memcpy(polys, m_polys, sizeof(dtPolyRef)*m_npolys);
  624. int npolys = m_npolys;
  625. float iterPos[3], targetPos[3];
  626. m_navQuery->closestPointOnPoly(m_startRef, m_spos, iterPos, 0);
  627. m_navQuery->closestPointOnPoly(polys[npolys-1], m_epos, targetPos, 0);
  628. static const float STEP_SIZE = 0.5f;
  629. static const float SLOP = 0.01f;
  630. m_nsmoothPath = 0;
  631. dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
  632. m_nsmoothPath++;
  633. // Move towards target a small advancement at a time until target reached or
  634. // when ran out of memory to store the path.
  635. while (npolys && m_nsmoothPath < MAX_SMOOTH)
  636. {
  637. // Find location to steer towards.
  638. float steerPos[3];
  639. unsigned char steerPosFlag;
  640. dtPolyRef steerPosRef;
  641. if (!getSteerTarget(m_navQuery, iterPos, targetPos, SLOP,
  642. polys, npolys, steerPos, steerPosFlag, steerPosRef))
  643. break;
  644. bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
  645. bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
  646. // Find movement delta.
  647. float delta[3], len;
  648. dtVsub(delta, steerPos, iterPos);
  649. len = dtMathSqrtf(dtVdot(delta, delta));
  650. // If the steer target is end of path or off-mesh link, do not move past the location.
  651. if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
  652. len = 1;
  653. else
  654. len = STEP_SIZE / len;
  655. float moveTgt[3];
  656. dtVmad(moveTgt, iterPos, delta, len);
  657. // Move
  658. float result[3];
  659. dtPolyRef visited[16];
  660. int nvisited = 0;
  661. m_navQuery->moveAlongSurface(polys[0], iterPos, moveTgt, &m_filter,
  662. result, visited, &nvisited, 16);
  663. npolys = fixupCorridor(polys, npolys, MAX_POLYS, visited, nvisited);
  664. npolys = fixupShortcuts(polys, npolys, m_navQuery);
  665. float h = 0;
  666. m_navQuery->getPolyHeight(polys[0], result, &h);
  667. result[1] = h;
  668. dtVcopy(iterPos, result);
  669. // Handle end of path and off-mesh links when close enough.
  670. if (endOfPath && inRange(iterPos, steerPos, SLOP, 1.0f))
  671. {
  672. // Reached end of path.
  673. dtVcopy(iterPos, targetPos);
  674. if (m_nsmoothPath < MAX_SMOOTH)
  675. {
  676. dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
  677. m_nsmoothPath++;
  678. }
  679. break;
  680. }
  681. else if (offMeshConnection && inRange(iterPos, steerPos, SLOP, 1.0f))
  682. {
  683. // Reached off-mesh connection.
  684. float startPos[3], endPos[3];
  685. // Advance the path up to and over the off-mesh connection.
  686. dtPolyRef prevRef = 0, polyRef = polys[0];
  687. int npos = 0;
  688. while (npos < npolys && polyRef != steerPosRef)
  689. {
  690. prevRef = polyRef;
  691. polyRef = polys[npos];
  692. npos++;
  693. }
  694. for (int i = npos; i < npolys; ++i)
  695. polys[i-npos] = polys[i];
  696. npolys -= npos;
  697. // Handle the connection.
  698. dtStatus status = m_navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
  699. if (dtStatusSucceed(status))
  700. {
  701. if (m_nsmoothPath < MAX_SMOOTH)
  702. {
  703. dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
  704. m_nsmoothPath++;
  705. // Hack to make the dotted path not visible during off-mesh connection.
  706. if (m_nsmoothPath & 1)
  707. {
  708. dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
  709. m_nsmoothPath++;
  710. }
  711. }
  712. // Move position at the other side of the off-mesh link.
  713. dtVcopy(iterPos, endPos);
  714. float eh = 0.0f;
  715. m_navQuery->getPolyHeight(polys[0], iterPos, &eh);
  716. iterPos[1] = eh;
  717. }
  718. }
  719. // Store results.
  720. if (m_nsmoothPath < MAX_SMOOTH)
  721. {
  722. dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
  723. m_nsmoothPath++;
  724. }
  725. }
  726. }
  727. }
  728. else
  729. {
  730. m_npolys = 0;
  731. m_nsmoothPath = 0;
  732. }
  733. }
  734. else if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT)
  735. {
  736. if (m_sposSet && m_eposSet && m_startRef && m_endRef)
  737. {
  738. #ifdef DUMP_REQS
  739. printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
  740. m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
  741. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  742. #endif
  743. m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
  744. m_nstraightPath = 0;
  745. if (m_npolys)
  746. {
  747. // In case of partial path, make sure the end point is clamped to the last polygon.
  748. float epos[3];
  749. dtVcopy(epos, m_epos);
  750. if (m_polys[m_npolys-1] != m_endRef)
  751. m_navQuery->closestPointOnPoly(m_polys[m_npolys-1], m_epos, epos, 0);
  752. m_navQuery->findStraightPath(m_spos, epos, m_polys, m_npolys,
  753. m_straightPath, m_straightPathFlags,
  754. m_straightPathPolys, &m_nstraightPath, MAX_POLYS, m_straightPathOptions);
  755. }
  756. }
  757. else
  758. {
  759. m_npolys = 0;
  760. m_nstraightPath = 0;
  761. }
  762. }
  763. else if (m_toolMode == TOOLMODE_PATHFIND_SLICED)
  764. {
  765. if (m_sposSet && m_eposSet && m_startRef && m_endRef)
  766. {
  767. #ifdef DUMP_REQS
  768. printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
  769. m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
  770. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  771. #endif
  772. m_npolys = 0;
  773. m_nstraightPath = 0;
  774. m_pathFindStatus = m_navQuery->initSlicedFindPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, DT_FINDPATH_ANY_ANGLE);
  775. }
  776. else
  777. {
  778. m_npolys = 0;
  779. m_nstraightPath = 0;
  780. }
  781. }
  782. else if (m_toolMode == TOOLMODE_RAYCAST)
  783. {
  784. m_nstraightPath = 0;
  785. if (m_sposSet && m_eposSet && m_startRef)
  786. {
  787. #ifdef DUMP_REQS
  788. printf("rc %f %f %f %f %f %f 0x%x 0x%x\n",
  789. m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
  790. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  791. #endif
  792. float t = 0;
  793. m_npolys = 0;
  794. m_nstraightPath = 2;
  795. m_straightPath[0] = m_spos[0];
  796. m_straightPath[1] = m_spos[1];
  797. m_straightPath[2] = m_spos[2];
  798. m_navQuery->raycast(m_startRef, m_spos, m_epos, &m_filter, &t, m_hitNormal, m_polys, &m_npolys, MAX_POLYS);
  799. if (t > 1)
  800. {
  801. // No hit
  802. dtVcopy(m_hitPos, m_epos);
  803. m_hitResult = false;
  804. }
  805. else
  806. {
  807. // Hit
  808. dtVlerp(m_hitPos, m_spos, m_epos, t);
  809. m_hitResult = true;
  810. }
  811. // Adjust height.
  812. if (m_npolys > 0)
  813. {
  814. float h = 0;
  815. m_navQuery->getPolyHeight(m_polys[m_npolys-1], m_hitPos, &h);
  816. m_hitPos[1] = h;
  817. }
  818. dtVcopy(&m_straightPath[3], m_hitPos);
  819. }
  820. }
  821. else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
  822. {
  823. m_distanceToWall = 0;
  824. if (m_sposSet && m_startRef)
  825. {
  826. #ifdef DUMP_REQS
  827. printf("dw %f %f %f %f 0x%x 0x%x\n",
  828. m_spos[0],m_spos[1],m_spos[2], 100.0f,
  829. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  830. #endif
  831. m_distanceToWall = 0.0f;
  832. m_navQuery->findDistanceToWall(m_startRef, m_spos, 100.0f, &m_filter, &m_distanceToWall, m_hitPos, m_hitNormal);
  833. }
  834. }
  835. else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE)
  836. {
  837. if (m_sposSet && m_startRef && m_eposSet)
  838. {
  839. const float dx = m_epos[0] - m_spos[0];
  840. const float dz = m_epos[2] - m_spos[2];
  841. float dist = sqrtf(dx*dx + dz*dz);
  842. #ifdef DUMP_REQS
  843. printf("fpc %f %f %f %f 0x%x 0x%x\n",
  844. m_spos[0],m_spos[1],m_spos[2], dist,
  845. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  846. #endif
  847. m_navQuery->findPolysAroundCircle(m_startRef, m_spos, dist, &m_filter,
  848. m_polys, m_parent, 0, &m_npolys, MAX_POLYS);
  849. }
  850. }
  851. else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE)
  852. {
  853. if (m_sposSet && m_startRef && m_eposSet)
  854. {
  855. const float nx = (m_epos[2] - m_spos[2])*0.25f;
  856. const float nz = -(m_epos[0] - m_spos[0])*0.25f;
  857. const float agentHeight = m_sample ? m_sample->getAgentHeight() : 0;
  858. m_queryPoly[0] = m_spos[0] + nx*1.2f;
  859. m_queryPoly[1] = m_spos[1] + agentHeight/2;
  860. m_queryPoly[2] = m_spos[2] + nz*1.2f;
  861. m_queryPoly[3] = m_spos[0] - nx*1.3f;
  862. m_queryPoly[4] = m_spos[1] + agentHeight/2;
  863. m_queryPoly[5] = m_spos[2] - nz*1.3f;
  864. m_queryPoly[6] = m_epos[0] - nx*0.8f;
  865. m_queryPoly[7] = m_epos[1] + agentHeight/2;
  866. m_queryPoly[8] = m_epos[2] - nz*0.8f;
  867. m_queryPoly[9] = m_epos[0] + nx;
  868. m_queryPoly[10] = m_epos[1] + agentHeight/2;
  869. m_queryPoly[11] = m_epos[2] + nz;
  870. #ifdef DUMP_REQS
  871. printf("fpp %f %f %f %f %f %f %f %f %f %f %f %f 0x%x 0x%x\n",
  872. m_queryPoly[0],m_queryPoly[1],m_queryPoly[2],
  873. m_queryPoly[3],m_queryPoly[4],m_queryPoly[5],
  874. m_queryPoly[6],m_queryPoly[7],m_queryPoly[8],
  875. m_queryPoly[9],m_queryPoly[10],m_queryPoly[11],
  876. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  877. #endif
  878. m_navQuery->findPolysAroundShape(m_startRef, m_queryPoly, 4, &m_filter,
  879. m_polys, m_parent, 0, &m_npolys, MAX_POLYS);
  880. }
  881. }
  882. else if (m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD)
  883. {
  884. if (m_sposSet && m_startRef)
  885. {
  886. #ifdef DUMP_REQS
  887. printf("fln %f %f %f %f 0x%x 0x%x\n",
  888. m_spos[0],m_spos[1],m_spos[2], m_neighbourhoodRadius,
  889. m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
  890. #endif
  891. m_navQuery->findLocalNeighbourhood(m_startRef, m_spos, m_neighbourhoodRadius, &m_filter,
  892. m_polys, m_parent, &m_npolys, MAX_POLYS);
  893. }
  894. }
  895. }
  896. static void getPolyCenter(dtNavMesh* navMesh, dtPolyRef ref, float* center)
  897. {
  898. center[0] = 0;
  899. center[1] = 0;
  900. center[2] = 0;
  901. const dtMeshTile* tile = 0;
  902. const dtPoly* poly = 0;
  903. dtStatus status = navMesh->getTileAndPolyByRef(ref, &tile, &poly);
  904. if (dtStatusFailed(status))
  905. return;
  906. for (int i = 0; i < (int)poly->vertCount; ++i)
  907. {
  908. const float* v = &tile->verts[poly->verts[i]*3];
  909. center[0] += v[0];
  910. center[1] += v[1];
  911. center[2] += v[2];
  912. }
  913. const float s = 1.0f / poly->vertCount;
  914. center[0] *= s;
  915. center[1] *= s;
  916. center[2] *= s;
  917. }
  918. void NavMeshTesterTool::handleRender()
  919. {
  920. duDebugDraw& dd = m_sample->getDebugDraw();
  921. static const unsigned int startCol = duRGBA(128,25,0,192);
  922. static const unsigned int endCol = duRGBA(51,102,0,129);
  923. static const unsigned int pathCol = duRGBA(0,0,0,64);
  924. const float agentRadius = m_sample->getAgentRadius();
  925. const float agentHeight = m_sample->getAgentHeight();
  926. const float agentClimb = m_sample->getAgentClimb();
  927. dd.depthMask(false);
  928. if (m_sposSet)
  929. drawAgent(m_spos, agentRadius, agentHeight, agentClimb, startCol);
  930. if (m_eposSet)
  931. drawAgent(m_epos, agentRadius, agentHeight, agentClimb, endCol);
  932. dd.depthMask(true);
  933. if (!m_navMesh)
  934. {
  935. return;
  936. }
  937. if (m_toolMode == TOOLMODE_PATHFIND_FOLLOW)
  938. {
  939. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
  940. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_endRef, endCol);
  941. if (m_npolys)
  942. {
  943. for (int i = 0; i < m_npolys; ++i)
  944. {
  945. if (m_polys[i] == m_startRef || m_polys[i] == m_endRef)
  946. continue;
  947. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  948. }
  949. }
  950. if (m_nsmoothPath)
  951. {
  952. dd.depthMask(false);
  953. const unsigned int spathCol = duRGBA(0,0,0,220);
  954. dd.begin(DU_DRAW_LINES, 3.0f);
  955. for (int i = 0; i < m_nsmoothPath; ++i)
  956. dd.vertex(m_smoothPath[i*3], m_smoothPath[i*3+1]+0.1f, m_smoothPath[i*3+2], spathCol);
  957. dd.end();
  958. dd.depthMask(true);
  959. }
  960. if (m_pathIterNum)
  961. {
  962. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_pathIterPolys[0], duRGBA(255,255,255,128));
  963. dd.depthMask(false);
  964. dd.begin(DU_DRAW_LINES, 1.0f);
  965. const unsigned int prevCol = duRGBA(255,192,0,220);
  966. const unsigned int curCol = duRGBA(255,255,255,220);
  967. const unsigned int steerCol = duRGBA(0,192,255,220);
  968. dd.vertex(m_prevIterPos[0],m_prevIterPos[1]-0.3f,m_prevIterPos[2], prevCol);
  969. dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], prevCol);
  970. dd.vertex(m_iterPos[0],m_iterPos[1]-0.3f,m_iterPos[2], curCol);
  971. dd.vertex(m_iterPos[0],m_iterPos[1]+0.3f,m_iterPos[2], curCol);
  972. dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], prevCol);
  973. dd.vertex(m_iterPos[0],m_iterPos[1]+0.3f,m_iterPos[2], prevCol);
  974. dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], steerCol);
  975. dd.vertex(m_steerPos[0],m_steerPos[1]+0.3f,m_steerPos[2], steerCol);
  976. for (int i = 0; i < m_steerPointCount-1; ++i)
  977. {
  978. dd.vertex(m_steerPoints[i*3+0],m_steerPoints[i*3+1]+0.2f,m_steerPoints[i*3+2], duDarkenCol(steerCol));
  979. dd.vertex(m_steerPoints[(i+1)*3+0],m_steerPoints[(i+1)*3+1]+0.2f,m_steerPoints[(i+1)*3+2], duDarkenCol(steerCol));
  980. }
  981. dd.end();
  982. dd.depthMask(true);
  983. }
  984. }
  985. else if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT ||
  986. m_toolMode == TOOLMODE_PATHFIND_SLICED)
  987. {
  988. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
  989. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_endRef, endCol);
  990. if (m_npolys)
  991. {
  992. for (int i = 0; i < m_npolys; ++i)
  993. {
  994. if (m_polys[i] == m_startRef || m_polys[i] == m_endRef)
  995. continue;
  996. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  997. }
  998. }
  999. if (m_nstraightPath)
  1000. {
  1001. dd.depthMask(false);
  1002. const unsigned int spathCol = duRGBA(64,16,0,220);
  1003. const unsigned int offMeshCol = duRGBA(128,96,0,220);
  1004. dd.begin(DU_DRAW_LINES, 2.0f);
  1005. for (int i = 0; i < m_nstraightPath-1; ++i)
  1006. {
  1007. unsigned int col;
  1008. if (m_straightPathFlags[i] & DT_STRAIGHTPATH_OFFMESH_CONNECTION)
  1009. col = offMeshCol;
  1010. else
  1011. col = spathCol;
  1012. dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], col);
  1013. dd.vertex(m_straightPath[(i+1)*3], m_straightPath[(i+1)*3+1]+0.4f, m_straightPath[(i+1)*3+2], col);
  1014. }
  1015. dd.end();
  1016. dd.begin(DU_DRAW_POINTS, 6.0f);
  1017. for (int i = 0; i < m_nstraightPath; ++i)
  1018. {
  1019. unsigned int col;
  1020. if (m_straightPathFlags[i] & DT_STRAIGHTPATH_START)
  1021. col = startCol;
  1022. else if (m_straightPathFlags[i] & DT_STRAIGHTPATH_END)
  1023. col = endCol;
  1024. else if (m_straightPathFlags[i] & DT_STRAIGHTPATH_OFFMESH_CONNECTION)
  1025. col = offMeshCol;
  1026. else
  1027. col = spathCol;
  1028. dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], col);
  1029. }
  1030. dd.end();
  1031. dd.depthMask(true);
  1032. }
  1033. }
  1034. else if (m_toolMode == TOOLMODE_RAYCAST)
  1035. {
  1036. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
  1037. if (m_nstraightPath)
  1038. {
  1039. for (int i = 1; i < m_npolys; ++i)
  1040. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  1041. dd.depthMask(false);
  1042. const unsigned int spathCol = m_hitResult ? duRGBA(64,16,0,220) : duRGBA(240,240,240,220);
  1043. dd.begin(DU_DRAW_LINES, 2.0f);
  1044. for (int i = 0; i < m_nstraightPath-1; ++i)
  1045. {
  1046. dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], spathCol);
  1047. dd.vertex(m_straightPath[(i+1)*3], m_straightPath[(i+1)*3+1]+0.4f, m_straightPath[(i+1)*3+2], spathCol);
  1048. }
  1049. dd.end();
  1050. dd.begin(DU_DRAW_POINTS, 4.0f);
  1051. for (int i = 0; i < m_nstraightPath; ++i)
  1052. dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], spathCol);
  1053. dd.end();
  1054. if (m_hitResult)
  1055. {
  1056. const unsigned int hitCol = duRGBA(0,0,0,128);
  1057. dd.begin(DU_DRAW_LINES, 2.0f);
  1058. dd.vertex(m_hitPos[0], m_hitPos[1] + 0.4f, m_hitPos[2], hitCol);
  1059. dd.vertex(m_hitPos[0] + m_hitNormal[0]*agentRadius,
  1060. m_hitPos[1] + 0.4f + m_hitNormal[1]*agentRadius,
  1061. m_hitPos[2] + m_hitNormal[2]*agentRadius, hitCol);
  1062. dd.end();
  1063. }
  1064. dd.depthMask(true);
  1065. }
  1066. }
  1067. else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
  1068. {
  1069. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
  1070. dd.depthMask(false);
  1071. duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_distanceToWall, duRGBA(64,16,0,220), 2.0f);
  1072. dd.begin(DU_DRAW_LINES, 3.0f);
  1073. dd.vertex(m_hitPos[0], m_hitPos[1] + 0.02f, m_hitPos[2], duRGBA(0,0,0,192));
  1074. dd.vertex(m_hitPos[0], m_hitPos[1] + agentHeight, m_hitPos[2], duRGBA(0,0,0,192));
  1075. dd.end();
  1076. dd.depthMask(true);
  1077. }
  1078. else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE)
  1079. {
  1080. for (int i = 0; i < m_npolys; ++i)
  1081. {
  1082. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  1083. dd.depthMask(false);
  1084. if (m_parent[i])
  1085. {
  1086. float p0[3], p1[3];
  1087. dd.depthMask(false);
  1088. getPolyCenter(m_navMesh, m_parent[i], p0);
  1089. getPolyCenter(m_navMesh, m_polys[i], p1);
  1090. duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
  1091. dd.depthMask(true);
  1092. }
  1093. dd.depthMask(true);
  1094. }
  1095. if (m_sposSet && m_eposSet)
  1096. {
  1097. dd.depthMask(false);
  1098. const float dx = m_epos[0] - m_spos[0];
  1099. const float dz = m_epos[2] - m_spos[2];
  1100. const float dist = sqrtf(dx*dx + dz*dz);
  1101. duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], dist, duRGBA(64,16,0,220), 2.0f);
  1102. dd.depthMask(true);
  1103. }
  1104. }
  1105. else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE)
  1106. {
  1107. for (int i = 0; i < m_npolys; ++i)
  1108. {
  1109. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  1110. dd.depthMask(false);
  1111. if (m_parent[i])
  1112. {
  1113. float p0[3], p1[3];
  1114. dd.depthMask(false);
  1115. getPolyCenter(m_navMesh, m_parent[i], p0);
  1116. getPolyCenter(m_navMesh, m_polys[i], p1);
  1117. duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
  1118. dd.depthMask(true);
  1119. }
  1120. dd.depthMask(true);
  1121. }
  1122. if (m_sposSet && m_eposSet)
  1123. {
  1124. dd.depthMask(false);
  1125. const unsigned int col = duRGBA(64,16,0,220);
  1126. dd.begin(DU_DRAW_LINES, 2.0f);
  1127. for (int i = 0, j = 3; i < 4; j=i++)
  1128. {
  1129. const float* p0 = &m_queryPoly[j*3];
  1130. const float* p1 = &m_queryPoly[i*3];
  1131. dd.vertex(p0, col);
  1132. dd.vertex(p1, col);
  1133. }
  1134. dd.end();
  1135. dd.depthMask(true);
  1136. }
  1137. }
  1138. else if (m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD)
  1139. {
  1140. for (int i = 0; i < m_npolys; ++i)
  1141. {
  1142. duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
  1143. dd.depthMask(false);
  1144. if (m_parent[i])
  1145. {
  1146. float p0[3], p1[3];
  1147. dd.depthMask(false);
  1148. getPolyCenter(m_navMesh, m_parent[i], p0);
  1149. getPolyCenter(m_navMesh, m_polys[i], p1);
  1150. duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
  1151. dd.depthMask(true);
  1152. }
  1153. static const int MAX_SEGS = DT_VERTS_PER_POLYGON*4;
  1154. float segs[MAX_SEGS*6];
  1155. dtPolyRef refs[MAX_SEGS];
  1156. memset(refs, 0, sizeof(dtPolyRef)*MAX_SEGS);
  1157. int nsegs = 0;
  1158. m_navQuery->getPolyWallSegments(m_polys[i], &m_filter, segs, refs, &nsegs, MAX_SEGS);
  1159. dd.begin(DU_DRAW_LINES, 2.0f);
  1160. for (int j = 0; j < nsegs; ++j)
  1161. {
  1162. const float* s = &segs[j*6];
  1163. // Skip too distant segments.
  1164. float tseg;
  1165. float distSqr = dtDistancePtSegSqr2D(m_spos, s, s+3, tseg);
  1166. if (distSqr > dtSqr(m_neighbourhoodRadius))
  1167. continue;
  1168. float delta[3], norm[3], p0[3], p1[3];
  1169. dtVsub(delta, s+3,s);
  1170. dtVmad(p0, s, delta, 0.5f);
  1171. norm[0] = delta[2];
  1172. norm[1] = 0;
  1173. norm[2] = -delta[0];
  1174. dtVnormalize(norm);
  1175. dtVmad(p1, p0, norm, agentRadius*0.5f);
  1176. // Skip backfacing segments.
  1177. if (refs[j])
  1178. {
  1179. unsigned int col = duRGBA(255,255,255,32);
  1180. dd.vertex(s[0],s[1]+agentClimb,s[2],col);
  1181. dd.vertex(s[3],s[4]+agentClimb,s[5],col);
  1182. }
  1183. else
  1184. {
  1185. unsigned int col = duRGBA(192,32,16,192);
  1186. if (dtTriArea2D(m_spos, s, s+3) < 0.0f)
  1187. col = duRGBA(96,32,16,192);
  1188. dd.vertex(p0[0],p0[1]+agentClimb,p0[2],col);
  1189. dd.vertex(p1[0],p1[1]+agentClimb,p1[2],col);
  1190. dd.vertex(s[0],s[1]+agentClimb,s[2],col);
  1191. dd.vertex(s[3],s[4]+agentClimb,s[5],col);
  1192. }
  1193. }
  1194. dd.end();
  1195. dd.depthMask(true);
  1196. }
  1197. if (m_sposSet)
  1198. {
  1199. dd.depthMask(false);
  1200. duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_neighbourhoodRadius, duRGBA(64,16,0,220), 2.0f);
  1201. dd.depthMask(true);
  1202. }
  1203. }
  1204. if (m_nrandPoints > 0)
  1205. {
  1206. dd.begin(DU_DRAW_POINTS, 6.0f);
  1207. for (int i = 0; i < m_nrandPoints; i++)
  1208. {
  1209. const float* p = &m_randPoints[i*3];
  1210. dd.vertex(p[0],p[1]+0.1f,p[2], duRGBA(220,32,16,192));
  1211. }
  1212. dd.end();
  1213. if (m_randPointsInCircle && m_sposSet)
  1214. {
  1215. duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_randomRadius, duRGBA(64,16,0,220), 2.0f);
  1216. }
  1217. }
  1218. }
  1219. void NavMeshTesterTool::handleRenderOverlay(double* proj, double* model, int* view)
  1220. {
  1221. GLdouble x, y, z;
  1222. // Draw start and end point labels
  1223. if (m_sposSet && gluProject((GLdouble)m_spos[0], (GLdouble)m_spos[1], (GLdouble)m_spos[2],
  1224. model, proj, view, &x, &y, &z))
  1225. {
  1226. imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "Start", imguiRGBA(0,0,0,220));
  1227. }
  1228. if (m_eposSet && gluProject((GLdouble)m_epos[0], (GLdouble)m_epos[1], (GLdouble)m_epos[2],
  1229. model, proj, view, &x, &y, &z))
  1230. {
  1231. imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "End", imguiRGBA(0,0,0,220));
  1232. }
  1233. // Tool help
  1234. const int h = view[3];
  1235. imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB+SHIFT: Set start location LMB: Set end location", imguiRGBA(255,255,255,192));
  1236. }
  1237. void NavMeshTesterTool::drawAgent(const float* pos, float r, float h, float c, const unsigned int col)
  1238. {
  1239. duDebugDraw& dd = m_sample->getDebugDraw();
  1240. dd.depthMask(false);
  1241. // Agent dimensions.
  1242. duDebugDrawCylinderWire(&dd, pos[0]-r, pos[1]+0.02f, pos[2]-r, pos[0]+r, pos[1]+h, pos[2]+r, col, 2.0f);
  1243. duDebugDrawCircle(&dd, pos[0],pos[1]+c,pos[2],r,duRGBA(0,0,0,64),1.0f);
  1244. unsigned int colb = duRGBA(0,0,0,196);
  1245. dd.begin(DU_DRAW_LINES);
  1246. dd.vertex(pos[0], pos[1]-c, pos[2], colb);
  1247. dd.vertex(pos[0], pos[1]+c, pos[2], colb);
  1248. dd.vertex(pos[0]-r/2, pos[1]+0.02f, pos[2], colb);
  1249. dd.vertex(pos[0]+r/2, pos[1]+0.02f, pos[2], colb);
  1250. dd.vertex(pos[0], pos[1]+0.02f, pos[2]-r/2, colb);
  1251. dd.vertex(pos[0], pos[1]+0.02f, pos[2]+r/2, colb);
  1252. dd.end();
  1253. dd.depthMask(true);
  1254. }