CCNavMesh.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "navmesh/CCNavMesh.h"
  22. #if CC_USE_NAVMESH
  23. #include "platform/CCFileUtils.h"
  24. #include "renderer/CCRenderer.h"
  25. #include "recast/Detour/DetourCommon.h"
  26. #include "recast/DebugUtils/DetourDebugDraw.h"
  27. #include <sstream>
  28. NS_CC_BEGIN
  29. #pragma pack(push,1)
  30. struct TileCacheSetHeader
  31. {
  32. int32_t magic;
  33. int32_t version;
  34. int32_t numTiles;
  35. dtNavMeshParams meshParams;
  36. dtTileCacheParams cacheParams;
  37. };
  38. struct TileCacheTileHeader
  39. {
  40. dtCompressedTileRef tileRef;
  41. int32_t dataSize;
  42. };
  43. #pragma pack(pop)
  44. static unsigned char* parseRow(unsigned char* buf, unsigned char* bufEnd, char* row, int len)
  45. {
  46. bool start = true;
  47. bool done = false;
  48. int n = 0;
  49. while (!done && buf < bufEnd)
  50. {
  51. char c = *buf;
  52. buf++;
  53. // multirow
  54. switch (c)
  55. {
  56. case '\n':
  57. if (start) break;
  58. done = true;
  59. break;
  60. case '\r':
  61. break;
  62. case '\t':
  63. case ' ':
  64. if (start) break;
  65. default:
  66. start = false;
  67. row[n++] = c;
  68. if (n >= len - 1)
  69. done = true;
  70. break;
  71. }
  72. }
  73. row[n] = '\0';
  74. return buf;
  75. }
  76. static const int TILECACHESET_MAGIC = 'T' << 24 | 'S' << 16 | 'E' << 8 | 'T'; //'TSET';
  77. static const int TILECACHESET_VERSION = 1;
  78. static const int MAX_AGENTS = 128;
  79. NavMesh* NavMesh::create(const std::string &navFilePath, const std::string &geomFilePath)
  80. {
  81. auto ref = new (std::nothrow) NavMesh();
  82. if (ref && ref->initWithFilePath(navFilePath, geomFilePath))
  83. {
  84. ref->autorelease();
  85. return ref;
  86. }
  87. CC_SAFE_DELETE(ref);
  88. return nullptr;
  89. }
  90. NavMesh::NavMesh()
  91. : _navMesh(nullptr)
  92. , _navMeshQuery(nullptr)
  93. , _crowed(nullptr)
  94. , _tileCache(nullptr)
  95. , _allocator(nullptr)
  96. , _compressor(nullptr)
  97. , _meshProcess(nullptr)
  98. , _geomData(nullptr)
  99. , _isDebugDrawEnabled(false)
  100. {
  101. }
  102. NavMesh::~NavMesh()
  103. {
  104. dtFreeTileCache(_tileCache);
  105. dtFreeCrowd(_crowed);
  106. dtFreeNavMesh(_navMesh);
  107. dtFreeNavMeshQuery(_navMeshQuery);
  108. CC_SAFE_DELETE(_allocator);
  109. CC_SAFE_DELETE(_compressor);
  110. CC_SAFE_DELETE(_meshProcess);
  111. CC_SAFE_DELETE(_geomData);
  112. for (auto iter : _agentList){
  113. CC_SAFE_RELEASE(iter);
  114. }
  115. _agentList.clear();
  116. for (auto iter : _obstacleList){
  117. CC_SAFE_RELEASE(iter);
  118. }
  119. _obstacleList.clear();
  120. }
  121. bool NavMesh::initWithFilePath(const std::string &navFilePath, const std::string &geomFilePath)
  122. {
  123. _navFilePath = navFilePath;
  124. _geomFilePath = geomFilePath;
  125. if (!read()) return false;
  126. return true;
  127. }
  128. bool NavMesh::read()
  129. {
  130. if (!loadGeomFile()) return false;
  131. if (!loadNavMeshFile()) return false;
  132. return true;
  133. }
  134. bool NavMesh::loadNavMeshFile()
  135. {
  136. auto data = FileUtils::getInstance()->getDataFromFile(_navFilePath);
  137. if (data.isNull()) return false;
  138. // Read header.
  139. unsigned int offset = 0;
  140. TileCacheSetHeader header = *((TileCacheSetHeader*)(data.getBytes() + offset));
  141. offset += sizeof(TileCacheSetHeader);
  142. if (header.magic != TILECACHESET_MAGIC)
  143. {
  144. return false;
  145. }
  146. if (header.version != TILECACHESET_VERSION)
  147. {
  148. return false;
  149. }
  150. _navMesh = dtAllocNavMesh();
  151. if (!_navMesh)
  152. {
  153. return false;
  154. }
  155. dtStatus status = _navMesh->init(&header.meshParams);
  156. if (dtStatusFailed(status))
  157. {
  158. return false;
  159. }
  160. _tileCache = dtAllocTileCache();
  161. if (!_tileCache)
  162. {
  163. return false;
  164. }
  165. _allocator = new (std::nothrow) LinearAllocator(32000);
  166. _compressor = new (std::nothrow) FastLZCompressor;
  167. _meshProcess = new (std::nothrow) MeshProcess(_geomData);
  168. status = _tileCache->init(&header.cacheParams, _allocator, _compressor, _meshProcess);
  169. if (dtStatusFailed(status))
  170. {
  171. return false;
  172. }
  173. // Read tiles.
  174. for (int i = 0; i < header.numTiles; ++i)
  175. {
  176. TileCacheTileHeader tileHeader = *((TileCacheTileHeader*)(data.getBytes() + offset));
  177. offset += sizeof(TileCacheTileHeader);
  178. if (!tileHeader.tileRef || !tileHeader.dataSize)
  179. break;
  180. unsigned char* tileData = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
  181. if (!tileData) break;
  182. memcpy(tileData, (data.getBytes() + offset), tileHeader.dataSize);
  183. offset += tileHeader.dataSize;
  184. dtCompressedTileRef tile = 0;
  185. _tileCache->addTile(tileData, tileHeader.dataSize, DT_COMPRESSEDTILE_FREE_DATA, &tile);
  186. if (tile)
  187. _tileCache->buildNavMeshTile(tile, _navMesh);
  188. }
  189. //create crowed
  190. _crowed = dtAllocCrowd();
  191. _crowed->init(MAX_AGENTS, header.cacheParams.walkableRadius, _navMesh);
  192. //create NavMeshQuery
  193. _navMeshQuery = dtAllocNavMeshQuery();
  194. _navMeshQuery->init(_navMesh, 2048);
  195. _agentList.assign(MAX_AGENTS, nullptr);
  196. _obstacleList.assign(header.cacheParams.maxObstacles, nullptr);
  197. //duDebugDrawNavMesh(&_debugDraw, *_navMesh, DU_DRAWNAVMESH_OFFMESHCONS);
  198. return true;
  199. }
  200. bool NavMesh::loadGeomFile()
  201. {
  202. unsigned char* buf = nullptr;
  203. auto data = FileUtils::getInstance()->getDataFromFile(_geomFilePath);
  204. if (data.isNull()) return false;
  205. buf = data.getBytes();
  206. _geomData = new (std::nothrow) GeomData;
  207. _geomData->offMeshConCount = 0;
  208. unsigned char* src = buf;
  209. unsigned char* srcEnd = buf + data.getSize();
  210. char row[512];
  211. while (src < srcEnd)
  212. {
  213. // Parse one row
  214. row[0] = '\0';
  215. src = parseRow(src, srcEnd, row, sizeof(row) / sizeof(char));
  216. if (row[0] == 'c')
  217. {
  218. // Off-mesh connection
  219. if (_geomData->offMeshConCount < GeomData::MAX_OFFMESH_CONNECTIONS)
  220. {
  221. float* v = &_geomData->offMeshConVerts[_geomData->offMeshConCount * 3 * 2];
  222. int bidir, area = 0, flags = 0;
  223. float rad;
  224. sscanf(row + 1, "%f %f %f %f %f %f %f %d %d %d",
  225. &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &rad, &bidir, &area, &flags);
  226. _geomData->offMeshConRads[_geomData->offMeshConCount] = rad;
  227. _geomData->offMeshConDirs[_geomData->offMeshConCount] = (unsigned char)bidir;
  228. _geomData->offMeshConAreas[_geomData->offMeshConCount] = (unsigned char)area;
  229. _geomData->offMeshConFlags[_geomData->offMeshConCount] = (unsigned short)flags;
  230. _geomData->offMeshConCount++;
  231. }
  232. }
  233. }
  234. return true;
  235. }
  236. void NavMesh::dtDraw()
  237. {
  238. drawObstacles();
  239. _debugDraw.depthMask(false);
  240. duDebugDrawNavMeshWithClosedList(&_debugDraw, *_navMesh, *_navMeshQuery, DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST/* | DU_DRAWNAVMESH_COLOR_TILES*/);
  241. drawAgents();
  242. drawOffMeshConnections();
  243. _debugDraw.depthMask(true);
  244. }
  245. void cocos2d::NavMesh::drawOffMeshConnections()
  246. {
  247. unsigned int conColor = duRGBA(192, 0, 128, 192);
  248. unsigned int baseColor = duRGBA(0, 0, 0, 64);
  249. _debugDraw.begin(DU_DRAW_LINES, 2.0f);
  250. for (int i = 0; i < _geomData->offMeshConCount; ++i)
  251. {
  252. float* v = &_geomData->offMeshConVerts[i * 3 * 2];
  253. _debugDraw.vertex(v[0], v[1], v[2], baseColor);
  254. _debugDraw.vertex(v[0], v[1] + 0.2f, v[2], baseColor);
  255. _debugDraw.vertex(v[3], v[4], v[5], baseColor);
  256. _debugDraw.vertex(v[3], v[4] + 0.2f, v[5], baseColor);
  257. duAppendCircle(&_debugDraw, v[0], v[1] + 0.1f, v[2], _geomData->offMeshConRads[i], baseColor);
  258. duAppendCircle(&_debugDraw, v[3], v[4] + 0.1f, v[5], _geomData->offMeshConRads[i], baseColor);
  259. if (/*hilight*/true)
  260. {
  261. duAppendArc(&_debugDraw, v[0], v[1], v[2], v[3], v[4], v[5], 0.25f,
  262. (_geomData->offMeshConDirs[i] & 1) ? 0.6f : 0.0f, 0.6f, conColor);
  263. }
  264. }
  265. _debugDraw.end();
  266. }
  267. void cocos2d::NavMesh::drawObstacles()
  268. {
  269. // Draw obstacles
  270. for (auto iter : _obstacleList)
  271. {
  272. if (iter){
  273. const dtTileCacheObstacle* ob = _tileCache->getObstacleByRef(iter->_obstacleID);
  274. if (ob->state == DT_OBSTACLE_EMPTY) continue;
  275. float bmin[3], bmax[3];
  276. _tileCache->getObstacleBounds(ob, bmin, bmax);
  277. unsigned int col = 0;
  278. if (ob->state == DT_OBSTACLE_PROCESSING)
  279. col = duRGBA(255, 255, 0, 128);
  280. else if (ob->state == DT_OBSTACLE_PROCESSED)
  281. col = duRGBA(255, 192, 0, 192);
  282. else if (ob->state == DT_OBSTACLE_REMOVING)
  283. col = duRGBA(220, 0, 0, 128);
  284. duDebugDrawCylinder(&_debugDraw, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2], col);
  285. duDebugDrawCylinderWire(&_debugDraw, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2], duDarkenCol(col), 2);
  286. }
  287. }
  288. }
  289. void cocos2d::NavMesh::drawAgents()
  290. {
  291. for (auto iter : _agentList)
  292. {
  293. if (iter){
  294. auto agent = _crowed->getAgent(iter->_agentID);
  295. float r = iter->getRadius();
  296. float h = iter->getHeight();
  297. unsigned int col = duRGBA(0, 0, 0, 32);
  298. duDebugDrawCircle(&_debugDraw, agent->npos[0], agent->npos[1], agent->npos[2], r, col, 2.0f);
  299. col = duRGBA(220, 220, 220, 128);
  300. if (agent->targetState == DT_CROWDAGENT_TARGET_REQUESTING || agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE)
  301. col = duLerpCol(col, duRGBA(128, 0, 255, 128), 32);
  302. else if (agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_PATH)
  303. col = duLerpCol(col, duRGBA(128, 0, 255, 128), 128);
  304. else if (agent->targetState == DT_CROWDAGENT_TARGET_FAILED)
  305. col = duRGBA(255, 32, 16, 128);
  306. else if (agent->targetState == DT_CROWDAGENT_TARGET_VELOCITY)
  307. col = duLerpCol(col, duRGBA(64, 255, 0, 128), 128);
  308. duDebugDrawCylinder(&_debugDraw, agent->npos[0] - r, agent->npos[1] + r*0.1f, agent->npos[2] - r,
  309. agent->npos[0] + r, agent->npos[1] + h, agent->npos[2] + r, col);
  310. }
  311. }
  312. // Velocity stuff.
  313. for (auto iter : _agentList)
  314. {
  315. if (iter){
  316. auto agent = _crowed->getAgent(iter->_agentID);
  317. const float radius = agent->params.radius;
  318. const float height = agent->params.height;
  319. const float* pos = agent->npos;
  320. const float* vel = agent->vel;
  321. // const float* dvel = agent->dvel;
  322. unsigned int col = duRGBA(220, 220, 220, 192);
  323. if (agent->targetState == DT_CROWDAGENT_TARGET_REQUESTING || agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE)
  324. col = duLerpCol(col, duRGBA(128, 0, 255, 192), 32);
  325. else if (agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_PATH)
  326. col = duLerpCol(col, duRGBA(128, 0, 255, 192), 128);
  327. else if (agent->targetState == DT_CROWDAGENT_TARGET_FAILED)
  328. col = duRGBA(255, 32, 16, 192);
  329. else if (agent->targetState == DT_CROWDAGENT_TARGET_VELOCITY)
  330. col = duLerpCol(col, duRGBA(64, 255, 0, 192), 128);
  331. duDebugDrawCircle(&_debugDraw, pos[0], pos[1] + height, pos[2], radius, col, 2.0f);
  332. //duDebugDrawArrow(&_debugDraw, pos[0], pos[1] + height, pos[2],
  333. // pos[0] + dvel[0], pos[1] + height + dvel[1], pos[2] + dvel[2],
  334. // 0.0f, 0.4f, duRGBA(0, 192, 255, 192), 2.0f);
  335. duDebugDrawArrow(&_debugDraw, pos[0], pos[1] + height, pos[2],
  336. pos[0] + vel[0], pos[1] + height + vel[1], pos[2] + vel[2],
  337. 0.0f, 0.4f, duRGBA(0, 0, 0, 160), 2.0f);
  338. }
  339. }
  340. }
  341. void NavMesh::removeNavMeshObstacle(NavMeshObstacle *obstacle)
  342. {
  343. auto iter = std::find(_obstacleList.begin(), _obstacleList.end(), obstacle);
  344. if (iter != _obstacleList.end()){
  345. obstacle->removeFrom(_tileCache);
  346. obstacle->release();
  347. _obstacleList[iter - _obstacleList.begin()] = nullptr;
  348. }
  349. }
  350. void NavMesh::addNavMeshObstacle(NavMeshObstacle *obstacle)
  351. {
  352. auto iter = std::find(_obstacleList.begin(), _obstacleList.end(), nullptr);
  353. if (iter != _obstacleList.end()){
  354. obstacle->addTo(_tileCache);
  355. obstacle->retain();
  356. _obstacleList[iter - _obstacleList.begin()] = obstacle;
  357. }
  358. }
  359. void NavMesh::removeNavMeshAgent(NavMeshAgent *agent)
  360. {
  361. auto iter = std::find(_agentList.begin(), _agentList.end(), agent);
  362. if (iter != _agentList.end()){
  363. agent->removeFrom(_crowed);
  364. agent->setNavMeshQuery(nullptr);
  365. agent->release();
  366. _agentList[iter - _agentList.begin()] = nullptr;
  367. }
  368. }
  369. void NavMesh::addNavMeshAgent(NavMeshAgent *agent)
  370. {
  371. auto iter = std::find(_agentList.begin(), _agentList.end(), nullptr);
  372. if (iter != _agentList.end()){
  373. agent->addTo(_crowed);
  374. agent->setNavMeshQuery(_navMeshQuery);
  375. agent->retain();
  376. _agentList[iter - _agentList.begin()] = agent;
  377. }
  378. }
  379. bool NavMesh::isDebugDrawEnabled() const
  380. {
  381. return _isDebugDrawEnabled;
  382. }
  383. void NavMesh::setDebugDrawEnable(bool enable)
  384. {
  385. _isDebugDrawEnabled = enable;
  386. }
  387. void NavMesh::debugDraw(Renderer* renderer)
  388. {
  389. if (_isDebugDrawEnabled){
  390. _debugDraw.clear();
  391. dtDraw();
  392. _debugDraw.draw(renderer);
  393. }
  394. }
  395. void NavMesh::update(float dt)
  396. {
  397. for (auto iter : _agentList){
  398. if (iter)
  399. iter->preUpdate(dt);
  400. }
  401. for (auto iter : _obstacleList){
  402. if (iter)
  403. iter->preUpdate(dt);
  404. }
  405. if (_crowed)
  406. _crowed->update(dt, nullptr);
  407. if (_tileCache)
  408. _tileCache->update(dt, _navMesh);
  409. for (auto iter : _agentList){
  410. if (iter)
  411. iter->postUpdate(dt);
  412. }
  413. for (auto iter : _obstacleList){
  414. if (iter)
  415. iter->postUpdate(dt);
  416. }
  417. }
  418. void cocos2d::NavMesh::findPath(const Vec3 &start, const Vec3 &end, std::vector<Vec3> &pathPoints)
  419. {
  420. static const int MAX_POLYS = 256;
  421. static const int MAX_SMOOTH = 2048;
  422. float ext[3];
  423. ext[0] = 2; ext[1] = 4; ext[2] = 2;
  424. dtQueryFilter filter;
  425. dtPolyRef startRef, endRef;
  426. dtPolyRef polys[MAX_POLYS];
  427. int npolys = 0;
  428. _navMeshQuery->findNearestPoly(&start.x, ext, &filter, &startRef, 0);
  429. _navMeshQuery->findNearestPoly(&end.x, ext, &filter, &endRef, 0);
  430. _navMeshQuery->findPath(startRef, endRef, &start.x, &end.x, &filter, polys, &npolys, MAX_POLYS);
  431. if (npolys)
  432. {
  433. //// Iterate over the path to find smooth path on the detail mesh surface.
  434. //dtPolyRef polys[MAX_POLYS];
  435. //memcpy(polys, polys, sizeof(dtPolyRef)*npolys);
  436. //int npolys = npolys;
  437. float iterPos[3], targetPos[3];
  438. _navMeshQuery->closestPointOnPoly(startRef, &start.x, iterPos, 0);
  439. _navMeshQuery->closestPointOnPoly(polys[npolys - 1], &end.x, targetPos, 0);
  440. static const float STEP_SIZE = 0.5f;
  441. static const float SLOP = 0.01f;
  442. int nsmoothPath = 0;
  443. //dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
  444. //m_nsmoothPath++;
  445. pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
  446. nsmoothPath++;
  447. // Move towards target a small advancement at a time until target reached or
  448. // when ran out of memory to store the path.
  449. while (npolys && nsmoothPath < MAX_SMOOTH)
  450. {
  451. // Find location to steer towards.
  452. float steerPos[3];
  453. unsigned char steerPosFlag;
  454. dtPolyRef steerPosRef;
  455. if (!getSteerTarget(_navMeshQuery, iterPos, targetPos, SLOP,
  456. polys, npolys, steerPos, steerPosFlag, steerPosRef))
  457. break;
  458. bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
  459. bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
  460. // Find movement delta.
  461. float delta[3], len;
  462. dtVsub(delta, steerPos, iterPos);
  463. len = dtMathSqrtf(dtVdot(delta, delta));
  464. // If the steer target is end of path or off-mesh link, do not move past the location.
  465. if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
  466. len = 1;
  467. else
  468. len = STEP_SIZE / len;
  469. float moveTgt[3];
  470. dtVmad(moveTgt, iterPos, delta, len);
  471. // Move
  472. float result[3];
  473. dtPolyRef visited[16];
  474. int nvisited = 0;
  475. _navMeshQuery->moveAlongSurface(polys[0], iterPos, moveTgt, &filter,
  476. result, visited, &nvisited, 16);
  477. npolys = fixupCorridor(polys, npolys, MAX_POLYS, visited, nvisited);
  478. npolys = fixupShortcuts(polys, npolys, _navMeshQuery);
  479. float h = 0;
  480. _navMeshQuery->getPolyHeight(polys[0], result, &h);
  481. result[1] = h;
  482. dtVcopy(iterPos, result);
  483. // Handle end of path and off-mesh links when close enough.
  484. if (endOfPath && inRange(iterPos, steerPos, SLOP, 1.0f))
  485. {
  486. // Reached end of path.
  487. dtVcopy(iterPos, targetPos);
  488. if (nsmoothPath < MAX_SMOOTH)
  489. {
  490. //dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
  491. //m_nsmoothPath++;
  492. pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
  493. nsmoothPath++;
  494. }
  495. break;
  496. }
  497. else if (offMeshConnection && inRange(iterPos, steerPos, SLOP, 1.0f))
  498. {
  499. // Reached off-mesh connection.
  500. float startPos[3], endPos[3];
  501. // Advance the path up to and over the off-mesh connection.
  502. dtPolyRef prevRef = 0, polyRef = polys[0];
  503. int npos = 0;
  504. while (npos < npolys && polyRef != steerPosRef)
  505. {
  506. prevRef = polyRef;
  507. polyRef = polys[npos];
  508. npos++;
  509. }
  510. for (int i = npos; i < npolys; ++i)
  511. polys[i - npos] = polys[i];
  512. npolys -= npos;
  513. // Handle the connection.
  514. dtStatus status = _navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
  515. if (dtStatusSucceed(status))
  516. {
  517. if (nsmoothPath < MAX_SMOOTH)
  518. {
  519. //dtVcopy(&m_smoothPath[m_nsmoothPath * 3], startPos);
  520. //m_nsmoothPath++;
  521. pathPoints.push_back(Vec3(startPos[0], startPos[1], startPos[2]));
  522. nsmoothPath++;
  523. // Hack to make the dotted path not visible during off-mesh connection.
  524. if (nsmoothPath & 1)
  525. {
  526. //dtVcopy(&m_smoothPath[m_nsmoothPath * 3], startPos);
  527. //m_nsmoothPath++;
  528. pathPoints.push_back(Vec3(startPos[0], startPos[1], startPos[2]));
  529. nsmoothPath++;
  530. }
  531. }
  532. // Move position at the other side of the off-mesh link.
  533. dtVcopy(iterPos, endPos);
  534. float eh = 0.0f;
  535. _navMeshQuery->getPolyHeight(polys[0], iterPos, &eh);
  536. iterPos[1] = eh;
  537. }
  538. }
  539. // Store results.
  540. if (nsmoothPath < MAX_SMOOTH)
  541. {
  542. //dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
  543. //m_nsmoothPath++;
  544. pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
  545. nsmoothPath++;
  546. }
  547. }
  548. }
  549. }
  550. NS_CC_END
  551. #endif //CC_USE_NAVMESH