Skip to content

Commit

Permalink
Update generate WalkingMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Nov 29, 2024
1 parent 00d2863 commit ecf1122
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Projects/Recast/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ include_directories(${Recast_INCLUDE_DIR})

set_target_properties(Recast PROPERTIES VERSION ${SKYLICHT_VERSION})

if (BUILD_SHARED_LIBS)
if (BUILD_LINUX OR BUILD_MACOS)
add_compile_options(-fPIC)
endif()
endif()

if (INSTALL_LIBS)
install(TARGETS Recast
EXPORT RecastTargets
Expand Down
1 change: 0 additions & 1 deletion Projects/Skylicht/Graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ setup_project_group("${skylicht_graph_source}" ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Graph ${ENGINE_SHARED_OR_STATIC_LIB} ${skylicht_graph_source})

if (BUILD_SHARED_LIBS)

if (BUILD_LINUX OR BUILD_MACOS)
add_compile_options(-fPIC)
endif()
Expand Down
38 changes: 38 additions & 0 deletions Projects/Skylicht/Graph/ObstacleAvoidance/CObstacleAvoidance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ namespace Skylicht
return 1;
}

bool CObstacleAvoidance::isLineHit(const core::vector3df& a, const core::vector3df& b, float h)
{
SObstacleSegment* segs = m_segments.pointer();
core::vector3df v = b - a;
float t = 0.0f;

for (u32 i = 0, n = m_segments.size(); i < n; i++)
{
SObstacleSegment& s = segs[i];

if (fabs(s.Begin.Y - a.Y) < h && fabs(s.End.Y - a.Y) < h)
{
int intersection = isectRaySeg(a, v, s.Begin, s.End, t);
if (intersection)
{
return true;
}
}
}
return false;
}

void CObstacleAvoidance::copySegments(CObstacleAvoidance* toTarget, const core::aabbox3df& box)
{
SObstacleSegment* segs = m_segments.pointer();
core::line3df line;

for (u32 i = 0, n = m_segments.size(); i < n; i++)
{
SObstacleSegment& s = segs[i];
line.setLine(s.Begin, s.End);
if (box.intersectsWithLine(line))
{
toTarget->addSegment(s.Begin, s.End);
}
}
}

core::vector3df CObstacleAvoidance::collide(const core::vector3df& position, const core::vector3df& vel, float radius, int recursionDepth)
{
if (recursionDepth >= 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@ namespace Skylicht

void clear();

void copySegments(CObstacleAvoidance* toTarget, const core::aabbox3df& box);

inline core::array<SObstacleSegment>& getSegments()
{
return m_segments;
}

inline bool isLineHit(const core::line3df& line, float h = 1.0f)
{
return isLineHit(line.start, line.end, h);
}

bool isLineHit(const core::vector3df& a, const core::vector3df& b, float h = 1.0f);

core::vector3df collide(const core::vector3df& position, const core::vector3df& vel, float radius, int recursionDepth = 0);
};
}
Expand Down
12 changes: 10 additions & 2 deletions Projects/Skylicht/Graph/WalkingMap/CWalkingMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace Skylicht
}
}

void CWalkingMap::generate(float tileWidth, float tileHeight, CMesh* recastMesh)
void CWalkingMap::generate(float tileWidth, float tileHeight, CMesh* recastMesh, CObstacleAvoidance* obstacle)
{
generate(tileWidth, tileHeight, recastMesh->getBoundingBox());

Expand Down Expand Up @@ -171,6 +171,8 @@ namespace Skylicht

STileXYZ tile(t->X, t->Y, t->Z);
m_hashTiles[tile] = t;

obstacle->copySegments(&t->Obstacle, t->BBox);
}

// link Neighbour
Expand All @@ -192,7 +194,13 @@ namespace Skylicht
t->Y + y,
t->Z + z);
if (nei)
t->Neighbours.push_back(nei);
{
if (!t->Obstacle.isLineHit(t->Position, nei->Position, tileHeight) &&
!nei->Obstacle.isLineHit(t->Position, nei->Position, tileHeight))
{
t->Neighbours.push_back(nei);
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Projects/Skylicht/Graph/WalkingMap/CWalkingMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Skylicht
int Z;
core::vector3df Position;
core::aabbox3df BBox;
core::array<SObstacleSegment> Cols;
CObstacleAvoidance Obstacle;
core::array<core::triangle3df> Tris;
core::array<STile*> Neighbours;
bool Visit;
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace Skylicht

virtual ~CWalkingMap();

void generate(float tileWidth, float tileHeight, CMesh* recastMesh);
void generate(float tileWidth, float tileHeight, CMesh* recastMesh, CObstacleAvoidance* obstacle);

void release();

Expand Down
4 changes: 3 additions & 1 deletion Samples/Graph/Source/CDemoNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void CDemoNavMesh::update()
SColor red(255, 100, 0, 0);
SColor white(255, 100, 100, 100);
SColor green(255, 0, 100, 0);
SColor yellow(255, 100, 100, 0);

// draw debug recastmesh
/*
Expand Down Expand Up @@ -161,6 +162,7 @@ void CDemoNavMesh::update()
if (nei->Visit == false)
{
debug->addLine(tile->Position, nei->Position, green);
debug->addPosition(tile->Position, m_tileHeight * 0.1f, yellow);
}
}

Expand Down Expand Up @@ -222,5 +224,5 @@ void CDemoNavMesh::buildNavMesh()

void CDemoNavMesh::buildWalkingMap()
{
m_walkingMap->generate(m_tileWidth, m_tileHeight, m_outputMesh);
m_walkingMap->generate(m_tileWidth, m_tileHeight, m_outputMesh, m_obstacle);
}

0 comments on commit ecf1122

Please sign in to comment.