axmol/core/navmesh/CCNavMesh.cpp

666 lines
21 KiB
C++
Raw Normal View History

2015-05-29 16:45:33 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-10-11 12:15:41 +08:00
Copyright (c) 2021 Bytedance Inc.
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-05-29 16:45:33 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "navmesh/CCNavMesh.h"
2022-07-15 19:17:01 +08:00
#if AX_USE_NAVMESH
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
# include "platform/CCFileUtils.h"
# include "renderer/CCRenderer.h"
# include "recast/DetourCommon.h"
# include "recast/DetourDebugDraw.h"
# include <sstream>
2015-05-29 16:45:33 +08:00
NS_AX_BEGIN
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
# pragma pack(push, 1)
2015-05-29 16:45:33 +08:00
struct TileCacheSetHeader
{
int32_t magic;
int32_t version;
int32_t numTiles;
2015-05-29 16:45:33 +08:00
dtNavMeshParams meshParams;
dtTileCacheParams cacheParams;
};
struct TileCacheTileHeader
{
dtCompressedTileRef tileRef;
int32_t dataSize;
2015-05-29 16:45:33 +08:00
};
2021-12-25 10:04:45 +08:00
# pragma pack(pop)
2015-05-29 16:45:33 +08:00
static unsigned char* parseRow(unsigned char* buf, unsigned char* bufEnd, char* row, int len)
{
bool start = true;
2021-12-25 10:04:45 +08:00
bool done = false;
int n = 0;
2015-05-29 16:45:33 +08:00
while (!done && buf < bufEnd)
{
char c = *buf;
buf++;
// multirow
switch (c)
{
case '\n':
2021-12-25 10:04:45 +08:00
if (start)
break;
2015-05-29 16:45:33 +08:00
done = true;
break;
case '\r':
break;
case '\t':
case ' ':
2021-12-25 10:04:45 +08:00
if (start)
break;
2015-05-29 16:45:33 +08:00
default:
2021-12-25 10:04:45 +08:00
start = false;
2015-05-29 16:45:33 +08:00
row[n++] = c;
if (n >= len - 1)
done = true;
break;
}
}
row[n] = '\0';
return buf;
}
2021-12-25 10:04:45 +08:00
static const int TILECACHESET_MAGIC = 'T' << 24 | 'S' << 16 | 'E' << 8 | 'T'; //'TSET';
static const int TILECACHESET_VERSION = 2; // 1: fastlz, 2: lz4
static const int MAX_AGENTS = 128;
2015-05-29 16:45:33 +08:00
2021-12-26 23:26:34 +08:00
NavMesh* NavMesh::create(std::string_view navFilePath, std::string_view geomFilePath)
2015-05-29 16:45:33 +08:00
{
2021-12-08 00:11:53 +08:00
auto ref = new NavMesh();
if (ref->initWithFilePath(navFilePath, geomFilePath))
2015-05-29 16:45:33 +08:00
{
ref->autorelease();
return ref;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ref);
2015-05-29 16:45:33 +08:00
return nullptr;
}
NavMesh::NavMesh()
: _navMesh(nullptr)
, _navMeshQuery(nullptr)
, _crowed(nullptr)
, _tileCache(nullptr)
, _allocator(nullptr)
, _compressor(nullptr)
, _meshProcess(nullptr)
, _geomData(nullptr)
, _isDebugDrawEnabled(false)
2021-12-25 10:04:45 +08:00
{}
2015-05-29 16:45:33 +08:00
NavMesh::~NavMesh()
{
dtFreeTileCache(_tileCache);
dtFreeCrowd(_crowed);
dtFreeNavMesh(_navMesh);
dtFreeNavMeshQuery(_navMeshQuery);
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(_allocator);
AX_SAFE_DELETE(_compressor);
AX_SAFE_DELETE(_meshProcess);
AX_SAFE_DELETE(_geomData);
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
for (auto iter : _agentList)
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(iter);
2015-05-29 16:45:33 +08:00
}
_agentList.clear();
2021-12-25 10:04:45 +08:00
for (auto iter : _obstacleList)
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(iter);
2015-05-29 16:45:33 +08:00
}
_obstacleList.clear();
}
2021-12-26 23:26:34 +08:00
bool NavMesh::initWithFilePath(std::string_view navFilePath, std::string_view geomFilePath)
2015-05-29 16:45:33 +08:00
{
2021-12-25 10:04:45 +08:00
_navFilePath = navFilePath;
2015-05-29 16:45:33 +08:00
_geomFilePath = geomFilePath;
2021-12-25 10:04:45 +08:00
if (!read())
return false;
2015-05-29 16:45:33 +08:00
return true;
}
bool NavMesh::read()
{
2021-12-25 10:04:45 +08:00
if (!loadGeomFile())
return false;
if (!loadNavMeshFile())
return false;
2015-05-29 16:45:33 +08:00
return true;
}
bool NavMesh::loadNavMeshFile()
{
auto data = FileUtils::getInstance()->getDataFromFile(_navFilePath);
2021-12-25 10:04:45 +08:00
if (data.isNull())
return false;
2015-05-29 16:45:33 +08:00
// Read header.
2021-12-25 10:04:45 +08:00
unsigned int offset = 0;
2021-10-11 12:15:41 +08:00
TileCacheSetHeader header = *(reinterpret_cast<TileCacheSetHeader*>(data.getBytes() + offset));
2015-05-29 16:45:33 +08:00
offset += sizeof(TileCacheSetHeader);
if (header.magic != TILECACHESET_MAGIC)
{
return false;
}
if (header.version != TILECACHESET_VERSION)
{
return false;
}
_navMesh = dtAllocNavMesh();
if (!_navMesh)
{
return false;
}
dtStatus status = _navMesh->init(&header.meshParams);
if (dtStatusFailed(status))
{
return false;
}
_tileCache = dtAllocTileCache();
if (!_tileCache)
{
return false;
}
2021-12-25 10:04:45 +08:00
_allocator = new LinearAllocator(32000);
_compressor = new LZ4Compressor();
2021-12-08 00:11:53 +08:00
_meshProcess = new MeshProcess(_geomData);
2021-12-25 10:04:45 +08:00
status = _tileCache->init(&header.cacheParams, _allocator, _compressor, _meshProcess);
2015-05-29 16:45:33 +08:00
if (dtStatusFailed(status))
{
return false;
}
// Read tiles.
for (int i = 0; i < header.numTiles; ++i)
{
TileCacheTileHeader tileHeader = *((TileCacheTileHeader*)(data.getBytes() + offset));
offset += sizeof(TileCacheTileHeader);
if (!tileHeader.tileRef || !tileHeader.dataSize)
break;
unsigned char* tileData = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
2021-12-25 10:04:45 +08:00
if (!tileData)
break;
2015-05-29 16:45:33 +08:00
memcpy(tileData, (data.getBytes() + offset), tileHeader.dataSize);
offset += tileHeader.dataSize;
dtCompressedTileRef tile = 0;
_tileCache->addTile(tileData, tileHeader.dataSize, DT_COMPRESSEDTILE_FREE_DATA, &tile);
if (tile)
_tileCache->buildNavMeshTile(tile, _navMesh);
}
2021-12-25 10:04:45 +08:00
// create crowed
2015-05-29 16:45:33 +08:00
_crowed = dtAllocCrowd();
_crowed->init(MAX_AGENTS, header.cacheParams.walkableRadius, _navMesh);
2021-12-25 10:04:45 +08:00
// create NavMeshQuery
2015-05-29 16:45:33 +08:00
_navMeshQuery = dtAllocNavMeshQuery();
_navMeshQuery->init(_navMesh, 2048);
_agentList.assign(MAX_AGENTS, nullptr);
_obstacleList.assign(header.cacheParams.maxObstacles, nullptr);
2021-12-25 10:04:45 +08:00
// duDebugDrawNavMesh(&_debugDraw, *_navMesh, DU_DRAWNAVMESH_OFFMESHCONS);
2015-05-29 16:45:33 +08:00
return true;
}
bool NavMesh::loadGeomFile()
{
2016-05-25 02:09:11 +08:00
unsigned char* buf = nullptr;
2021-12-25 10:04:45 +08:00
auto data = FileUtils::getInstance()->getDataFromFile(_geomFilePath);
if (data.isNull())
return false;
buf = data.getBytes();
_geomData = new GeomData;
2015-05-29 16:45:33 +08:00
_geomData->offMeshConCount = 0;
2021-12-25 10:04:45 +08:00
unsigned char* src = buf;
2015-05-29 16:45:33 +08:00
unsigned char* srcEnd = buf + data.getSize();
char row[512];
while (src < srcEnd)
{
// Parse one row
row[0] = '\0';
2021-12-25 10:04:45 +08:00
src = parseRow(src, srcEnd, row, sizeof(row) / sizeof(char));
2015-05-29 16:45:33 +08:00
if (row[0] == 'c')
{
// Off-mesh connection
if (_geomData->offMeshConCount < GeomData::MAX_OFFMESH_CONNECTIONS)
{
float* v = &_geomData->offMeshConVerts[_geomData->offMeshConCount * 3 * 2];
int bidir, area = 0, flags = 0;
float rad;
2021-12-25 10:04:45 +08:00
sscanf(row + 1, "%f %f %f %f %f %f %f %d %d %d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &rad,
&bidir, &area, &flags);
_geomData->offMeshConRads[_geomData->offMeshConCount] = rad;
_geomData->offMeshConDirs[_geomData->offMeshConCount] = (unsigned char)bidir;
2015-05-29 16:45:33 +08:00
_geomData->offMeshConAreas[_geomData->offMeshConCount] = (unsigned char)area;
_geomData->offMeshConFlags[_geomData->offMeshConCount] = (unsigned short)flags;
_geomData->offMeshConCount++;
}
}
}
return true;
}
void NavMesh::dtDraw()
{
drawObstacles();
_debugDraw.depthMask(false);
2021-12-25 10:04:45 +08:00
duDebugDrawNavMeshWithClosedList(
&_debugDraw, *_navMesh, *_navMeshQuery,
DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST /* | DU_DRAWNAVMESH_COLOR_TILES*/);
2015-05-29 16:45:33 +08:00
drawAgents();
drawOffMeshConnections();
_debugDraw.depthMask(true);
}
void axis::NavMesh::drawOffMeshConnections()
2015-05-29 16:45:33 +08:00
{
2021-12-25 10:04:45 +08:00
unsigned int conColor = duRGBA(192, 0, 128, 192);
2015-05-29 16:45:33 +08:00
unsigned int baseColor = duRGBA(0, 0, 0, 64);
_debugDraw.begin(DU_DRAW_LINES, 2.0f);
for (int i = 0; i < _geomData->offMeshConCount; ++i)
{
float* v = &_geomData->offMeshConVerts[i * 3 * 2];
_debugDraw.vertex(v[0], v[1], v[2], baseColor);
_debugDraw.vertex(v[0], v[1] + 0.2f, v[2], baseColor);
_debugDraw.vertex(v[3], v[4], v[5], baseColor);
_debugDraw.vertex(v[3], v[4] + 0.2f, v[5], baseColor);
duAppendCircle(&_debugDraw, v[0], v[1] + 0.1f, v[2], _geomData->offMeshConRads[i], baseColor);
duAppendCircle(&_debugDraw, v[3], v[4] + 0.1f, v[5], _geomData->offMeshConRads[i], baseColor);
2021-12-25 10:04:45 +08:00
if (/*hilight*/ true)
2015-05-29 16:45:33 +08:00
{
duAppendArc(&_debugDraw, v[0], v[1], v[2], v[3], v[4], v[5], 0.25f,
2021-12-25 10:04:45 +08:00
(_geomData->offMeshConDirs[i] & 1) ? 0.6f : 0.0f, 0.6f, conColor);
2015-05-29 16:45:33 +08:00
}
}
_debugDraw.end();
}
void axis::NavMesh::drawObstacles()
2015-05-29 16:45:33 +08:00
{
// Draw obstacles
for (auto iter : _obstacleList)
{
2021-12-25 10:04:45 +08:00
if (iter)
{
2015-05-29 16:45:33 +08:00
const dtTileCacheObstacle* ob = _tileCache->getObstacleByRef(iter->_obstacleID);
2021-12-25 10:04:45 +08:00
if (ob->state == DT_OBSTACLE_EMPTY)
continue;
2015-05-29 16:45:33 +08:00
float bmin[3], bmax[3];
_tileCache->getObstacleBounds(ob, bmin, bmax);
unsigned int col = 0;
if (ob->state == DT_OBSTACLE_PROCESSING)
col = duRGBA(255, 255, 0, 128);
else if (ob->state == DT_OBSTACLE_PROCESSED)
col = duRGBA(255, 192, 0, 192);
else if (ob->state == DT_OBSTACLE_REMOVING)
col = duRGBA(220, 0, 0, 128);
duDebugDrawCylinder(&_debugDraw, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2], col);
2021-12-25 10:04:45 +08:00
duDebugDrawCylinderWire(&_debugDraw, bmin[0], bmin[1], bmin[2], bmax[0], bmax[1], bmax[2], duDarkenCol(col),
2);
2015-05-29 16:45:33 +08:00
}
}
}
void axis::NavMesh::drawAgents()
2015-05-29 16:45:33 +08:00
{
for (auto iter : _agentList)
{
2021-12-25 10:04:45 +08:00
if (iter)
{
2015-05-29 16:45:33 +08:00
auto agent = _crowed->getAgent(iter->_agentID);
2021-12-25 10:04:45 +08:00
float r = iter->getRadius();
float h = iter->getHeight();
2015-05-29 16:45:33 +08:00
unsigned int col = duRGBA(0, 0, 0, 32);
duDebugDrawCircle(&_debugDraw, agent->npos[0], agent->npos[1], agent->npos[2], r, col, 2.0f);
col = duRGBA(220, 220, 220, 128);
2021-12-25 10:04:45 +08:00
if (agent->targetState == DT_CROWDAGENT_TARGET_REQUESTING ||
agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE)
2015-05-29 16:45:33 +08:00
col = duLerpCol(col, duRGBA(128, 0, 255, 128), 32);
else if (agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_PATH)
col = duLerpCol(col, duRGBA(128, 0, 255, 128), 128);
else if (agent->targetState == DT_CROWDAGENT_TARGET_FAILED)
col = duRGBA(255, 32, 16, 128);
else if (agent->targetState == DT_CROWDAGENT_TARGET_VELOCITY)
col = duLerpCol(col, duRGBA(64, 255, 0, 128), 128);
2021-12-25 10:04:45 +08:00
duDebugDrawCylinder(&_debugDraw, agent->npos[0] - r, agent->npos[1] + r * 0.1f, agent->npos[2] - r,
agent->npos[0] + r, agent->npos[1] + h, agent->npos[2] + r, col);
2015-05-29 16:45:33 +08:00
}
}
// Velocity stuff.
for (auto iter : _agentList)
{
2021-12-25 10:04:45 +08:00
if (iter)
{
2015-05-29 16:45:33 +08:00
auto agent = _crowed->getAgent(iter->_agentID);
const float radius = agent->params.radius;
const float height = agent->params.height;
2021-12-25 10:04:45 +08:00
const float* pos = agent->npos;
const float* vel = agent->vel;
// const float* dvel = agent->dvel;
2015-05-29 16:45:33 +08:00
unsigned int col = duRGBA(220, 220, 220, 192);
2021-12-25 10:04:45 +08:00
if (agent->targetState == DT_CROWDAGENT_TARGET_REQUESTING ||
agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE)
2015-05-29 16:45:33 +08:00
col = duLerpCol(col, duRGBA(128, 0, 255, 192), 32);
else if (agent->targetState == DT_CROWDAGENT_TARGET_WAITING_FOR_PATH)
col = duLerpCol(col, duRGBA(128, 0, 255, 192), 128);
else if (agent->targetState == DT_CROWDAGENT_TARGET_FAILED)
col = duRGBA(255, 32, 16, 192);
else if (agent->targetState == DT_CROWDAGENT_TARGET_VELOCITY)
col = duLerpCol(col, duRGBA(64, 255, 0, 192), 128);
duDebugDrawCircle(&_debugDraw, pos[0], pos[1] + height, pos[2], radius, col, 2.0f);
2021-12-25 10:04:45 +08:00
// duDebugDrawArrow(&_debugDraw, pos[0], pos[1] + height, pos[2],
// pos[0] + dvel[0], pos[1] + height + dvel[1], pos[2] + dvel[2],
// 0.0f, 0.4f, duRGBA(0, 192, 255, 192), 2.0f);
2015-05-29 16:45:33 +08:00
2021-12-25 10:04:45 +08:00
duDebugDrawArrow(&_debugDraw, pos[0], pos[1] + height, pos[2], pos[0] + vel[0], pos[1] + height + vel[1],
pos[2] + vel[2], 0.0f, 0.4f, duRGBA(0, 0, 0, 160), 2.0f);
2015-05-29 16:45:33 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
void NavMesh::removeNavMeshObstacle(NavMeshObstacle* obstacle)
2015-05-29 16:45:33 +08:00
{
auto iter = std::find(_obstacleList.begin(), _obstacleList.end(), obstacle);
2021-12-25 10:04:45 +08:00
if (iter != _obstacleList.end())
{
2015-05-29 16:45:33 +08:00
obstacle->removeFrom(_tileCache);
obstacle->release();
_obstacleList[iter - _obstacleList.begin()] = nullptr;
}
}
2021-12-25 10:04:45 +08:00
void NavMesh::addNavMeshObstacle(NavMeshObstacle* obstacle)
2015-05-29 16:45:33 +08:00
{
auto iter = std::find(_obstacleList.begin(), _obstacleList.end(), nullptr);
2021-12-25 10:04:45 +08:00
if (iter != _obstacleList.end())
{
2015-05-29 16:45:33 +08:00
obstacle->addTo(_tileCache);
obstacle->retain();
_obstacleList[iter - _obstacleList.begin()] = obstacle;
}
}
2021-12-25 10:04:45 +08:00
void NavMesh::removeNavMeshAgent(NavMeshAgent* agent)
2015-05-29 16:45:33 +08:00
{
auto iter = std::find(_agentList.begin(), _agentList.end(), agent);
2021-12-25 10:04:45 +08:00
if (iter != _agentList.end())
{
2015-05-29 16:45:33 +08:00
agent->removeFrom(_crowed);
agent->setNavMeshQuery(nullptr);
agent->release();
_agentList[iter - _agentList.begin()] = nullptr;
}
}
2021-12-25 10:04:45 +08:00
void NavMesh::addNavMeshAgent(NavMeshAgent* agent)
2015-05-29 16:45:33 +08:00
{
auto iter = std::find(_agentList.begin(), _agentList.end(), nullptr);
2021-12-25 10:04:45 +08:00
if (iter != _agentList.end())
{
2015-05-29 16:45:33 +08:00
agent->addTo(_crowed);
agent->setNavMeshQuery(_navMeshQuery);
agent->retain();
_agentList[iter - _agentList.begin()] = agent;
}
}
bool NavMesh::isDebugDrawEnabled() const
{
return _isDebugDrawEnabled;
}
void NavMesh::setDebugDrawEnable(bool enable)
{
_isDebugDrawEnabled = enable;
}
void NavMesh::debugDraw(Renderer* renderer)
{
2021-12-25 10:04:45 +08:00
if (_isDebugDrawEnabled)
{
2015-05-29 16:45:33 +08:00
_debugDraw.clear();
dtDraw();
_debugDraw.draw(renderer);
}
}
void NavMesh::update(float dt)
{
2021-12-25 10:04:45 +08:00
for (auto iter : _agentList)
{
2015-05-29 16:45:33 +08:00
if (iter)
iter->preUpdate(dt);
}
2021-12-25 10:04:45 +08:00
for (auto iter : _obstacleList)
{
2015-05-29 16:45:33 +08:00
if (iter)
iter->preUpdate(dt);
}
if (_crowed)
_crowed->update(dt, nullptr);
if (_tileCache)
_tileCache->update(dt, _navMesh);
2021-12-25 10:04:45 +08:00
for (auto iter : _agentList)
{
2015-05-29 16:45:33 +08:00
if (iter)
iter->postUpdate(dt);
}
2021-12-25 10:04:45 +08:00
for (auto iter : _obstacleList)
{
2015-05-29 16:45:33 +08:00
if (iter)
iter->postUpdate(dt);
}
}
void axis::NavMesh::findPath(const Vec3& start, const Vec3& end, std::vector<Vec3>& pathPoints)
2015-05-29 16:45:33 +08:00
{
2021-12-25 10:04:45 +08:00
static const int MAX_POLYS = 256;
2015-05-29 16:45:33 +08:00
static const int MAX_SMOOTH = 2048;
float ext[3];
2021-12-25 10:04:45 +08:00
ext[0] = 2;
ext[1] = 4;
ext[2] = 2;
2015-05-29 16:45:33 +08:00
dtQueryFilter filter;
dtPolyRef startRef, endRef;
dtPolyRef polys[MAX_POLYS];
int npolys = 0;
_navMeshQuery->findNearestPoly(&start.x, ext, &filter, &startRef, 0);
_navMeshQuery->findNearestPoly(&end.x, ext, &filter, &endRef, 0);
_navMeshQuery->findPath(startRef, endRef, &start.x, &end.x, &filter, polys, &npolys, MAX_POLYS);
if (npolys)
{
//// Iterate over the path to find smooth path on the detail mesh surface.
2021-12-25 10:04:45 +08:00
// dtPolyRef polys[MAX_POLYS];
// memcpy(polys, polys, sizeof(dtPolyRef)*npolys);
// int npolys = npolys;
2015-05-29 16:45:33 +08:00
float iterPos[3], targetPos[3];
_navMeshQuery->closestPointOnPoly(startRef, &start.x, iterPos, 0);
_navMeshQuery->closestPointOnPoly(polys[npolys - 1], &end.x, targetPos, 0);
static const float STEP_SIZE = 0.5f;
2021-12-25 10:04:45 +08:00
static const float SLOP = 0.01f;
2015-05-29 16:45:33 +08:00
int nsmoothPath = 0;
2021-12-25 10:04:45 +08:00
// dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
// m_nsmoothPath++;
2015-05-29 16:45:33 +08:00
pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
nsmoothPath++;
// Move towards target a small advancement at a time until target reached or
// when ran out of memory to store the path.
while (npolys && nsmoothPath < MAX_SMOOTH)
{
// Find location to steer towards.
float steerPos[3];
unsigned char steerPosFlag;
dtPolyRef steerPosRef;
2021-12-25 10:04:45 +08:00
if (!getSteerTarget(_navMeshQuery, iterPos, targetPos, SLOP, polys, npolys, steerPos, steerPosFlag,
steerPosRef))
2015-05-29 16:45:33 +08:00
break;
2021-12-25 10:04:45 +08:00
bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
2015-05-29 16:45:33 +08:00
bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
// Find movement delta.
float delta[3], len;
dtVsub(delta, steerPos, iterPos);
len = dtMathSqrtf(dtVdot(delta, delta));
// If the steer target is end of path or off-mesh link, do not move past the location.
if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
len = 1;
else
len = STEP_SIZE / len;
float moveTgt[3];
dtVmad(moveTgt, iterPos, delta, len);
// Move
float result[3];
dtPolyRef visited[16];
int nvisited = 0;
2021-12-25 10:04:45 +08:00
_navMeshQuery->moveAlongSurface(polys[0], iterPos, moveTgt, &filter, result, visited, &nvisited, 16);
2015-05-29 16:45:33 +08:00
npolys = fixupCorridor(polys, npolys, MAX_POLYS, visited, nvisited);
npolys = fixupShortcuts(polys, npolys, _navMeshQuery);
float h = 0;
_navMeshQuery->getPolyHeight(polys[0], result, &h);
result[1] = h;
dtVcopy(iterPos, result);
// Handle end of path and off-mesh links when close enough.
if (endOfPath && inRange(iterPos, steerPos, SLOP, 1.0f))
{
// Reached end of path.
dtVcopy(iterPos, targetPos);
if (nsmoothPath < MAX_SMOOTH)
{
2021-12-25 10:04:45 +08:00
// dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
// m_nsmoothPath++;
2015-05-29 16:45:33 +08:00
pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
nsmoothPath++;
}
break;
}
else if (offMeshConnection && inRange(iterPos, steerPos, SLOP, 1.0f))
{
// Reached off-mesh connection.
float startPos[3], endPos[3];
// Advance the path up to and over the off-mesh connection.
dtPolyRef prevRef = 0, polyRef = polys[0];
int npos = 0;
while (npos < npolys && polyRef != steerPosRef)
{
prevRef = polyRef;
polyRef = polys[npos];
npos++;
}
for (int i = npos; i < npolys; ++i)
polys[i - npos] = polys[i];
npolys -= npos;
// Handle the connection.
dtStatus status = _navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
if (dtStatusSucceed(status))
{
if (nsmoothPath < MAX_SMOOTH)
{
2021-12-25 10:04:45 +08:00
// dtVcopy(&m_smoothPath[m_nsmoothPath * 3], startPos);
// m_nsmoothPath++;
2015-05-29 16:45:33 +08:00
pathPoints.push_back(Vec3(startPos[0], startPos[1], startPos[2]));
nsmoothPath++;
// Hack to make the dotted path not visible during off-mesh connection.
if (nsmoothPath & 1)
{
2021-12-25 10:04:45 +08:00
// dtVcopy(&m_smoothPath[m_nsmoothPath * 3], startPos);
// m_nsmoothPath++;
2015-05-29 16:45:33 +08:00
pathPoints.push_back(Vec3(startPos[0], startPos[1], startPos[2]));
nsmoothPath++;
}
}
// Move position at the other side of the off-mesh link.
dtVcopy(iterPos, endPos);
float eh = 0.0f;
_navMeshQuery->getPolyHeight(polys[0], iterPos, &eh);
iterPos[1] = eh;
}
}
// Store results.
if (nsmoothPath < MAX_SMOOTH)
{
2021-12-25 10:04:45 +08:00
// dtVcopy(&m_smoothPath[m_nsmoothPath * 3], iterPos);
// m_nsmoothPath++;
2015-05-29 16:45:33 +08:00
pathPoints.push_back(Vec3(iterPos[0], iterPos[1], iterPos[2]));
nsmoothPath++;
}
}
}
}
NS_AX_END
2015-05-29 16:45:33 +08:00
2022-07-15 19:17:01 +08:00
#endif // AX_USE_NAVMESH