Unreal RecastNavigation 开源项目详解
0 前言
Recastnavigation是一个游戏AI导航库,像Unity,UE引擎中都集成了这个开源项目, HALO中使用的也是这个开源库。导航最重要的就是为NPC寻路,以及其他的寻路需求。
需要注明的是,这个寻路库虽然厉害。但是他的核心是 平面寻路。也就是重力方向一直朝着 -Y 方向。如果是星球地形,既重力方向朝向球心,在任何一点重力方向都是不同的。那么这个开源库就不能使用了。
1 Recastnavigation 下载与编译
主页: https://github.com/recastnavigation/recastnavigation/tree/main?tab=readme-ov-file
编译: https://github.com/recastnavigation/recastnavigation/blob/main/Docs/_2_BuildingAndIntegrating.md
编译细节
这个项目 Recast and Detour 不依赖其他的一些库,但是 RecastDemo 即可视化的示例是需要SDL2库的,SDL2库可以理解为创建窗口应用程序所需要的库。所以我们需要下载SDL。
同时也需要 premake5 编译工具。
1.1 Windows
- Grab the latest SDL2 development library release from https://github.com/libsdl-org/SDL and unzip it into RecastDemo/Contrib. Rename the SDL folder such that the path RecastDemo/Contrib/SDL/lib/x86 is valid.
SDL2-devel-2.28.2-VC.zip 像这样的文件名的release才可以,不要直接下载 source code
// 成功后的文件夹应该是这个样子。
RecastDemo/Contrib
fastlz
readme-sdl.txt
SDL
stb_truetype.h
●Navigate to the RecastDemo folder and run premake5 vs2022
●Open Build/vs2022/recastnavigation.sln in Visual Studio 2022 or Jetbrains Rider.
●Set RecastDemo as the startup project, build, and run.
2. 代码详解
// Figure out how big the raster voxel grid will be based on the input geometry bounds.
rcCalcGridSize
// Voxelize the input geometry
rcAllocHeightfield
rcCreateHeightfield
rcMarkWalkableTriangles
rcRasterizeTriangles
// Clean up the voxel data and filter out non-walkable areas.
rcFilterLowHangingWalkableObstacles
rcFilterLedgeSpans
rcFilterWalkableLowHeightSpans
// Consolidate the voxel data into a more compact representation
rcAllocCompactHeightfield
rcBuildCompactHeightfield
// Further refine the voxel representation
rcErodeWalkableArea
rcBuildDistanceField
rcBuildRegions
// Triangulate the navmesh polygons from the voxel data
rcAllocContourSet
rcBuildContours
rcAllocPolyMesh
rcBuildPolyMesh
// Package the mesh with additional metadata that's useful at runtime.
rcAllocPolyMeshDetail
rcBuildPolyMeshDetail
// Cleanup
rcFreeHeightField
rcFreeCompactHeightfield
rcFreeContourSet
代码详解主要为2个部分。
- 数据载入
- 高度场建立
2.1 数据载入
首先Sample_SoloMesh::handleBuild()会调用InputGeom::getMesh()
那么Mesh数据从哪里来呢?
InputGeom::loadMesh()InputGeom::load()rcMeshLoaderObj::addVertex(float x, float y, float z, int& cap)其中cap是顶点的内存容量,以存8个顶点为开始,内存短缺后以2倍速度扩大。存储皆为1维数组,顶点与顶点之间的stride==3。void rcMeshLoaderObj::addTriangle(int a, int b, int c, int& cap)
有了Mesh之后,开始计算xz平面的栅格数量,设置为 *sizeX = &m_cfg.width, *sizeZ = &m_cfg.height。平面边缘,只要占到一半以上的cellSize,就认为是一个cell。
void rcCalcGridSize(const float* minBounds, const float* maxBounds, const float cellSize, int* sizeX, int* sizeZ)
{ // (0, 0.5) + 0.5 -> 0
// [0.5, 0.999) + 0.5 -> 1
*sizeX = (int)((maxBounds[0] - minBounds[0]) / cellSize + 0.5f);
*sizeZ = (int)((maxBounds[2] - minBounds[2]) / cellSize + 0.5f);
}
rcCreateHeightfield(m_ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch)相当于构造函数,初始化高度场信息HeightField or HeightMap。
m_triareas = new unsigned char[ntris];角色代理可以走的三角形。
rcMarkWalkableTriangles(m_ctx, m_cfg.walkableSlopeAngle, verts, nverts, tris, ntris, m_triareas);这个的关键就是三角形的法向量的 y 分量,等于三角形面与 x-z 平面的夹角 cos 值。运用相似三角形。
2.2 高度场建立
主要就是利用x-z平面的网格与映射到平面上的三角形的交点,通过相似三角形获得这个交点 (x-z)-> Y在三维三角形的高度。
这个切割方法是 Sutherland-Hodgman polygon-clipping algorithm 的变种。
Sutherland-Hodgman polygon-clipping algorithm
- 这个算法最重要的就是判断顶点在 切割窗口某一条 Edge 两侧的哪一侧。
- Edge存在切割窗口的那一侧是 in, 不存在则是 out.
https://stackoverflow.com/questions/21638509/determine-voxels-that-a-triangle-is-in
Abhishek Sharma : https://www.youtube.com/watch?v=OGW9Cqr5RRg&t=333s&pp=ygUTIFN1dGhlcmxhbmQtSG9kZ21hbg%3D%3D
2.2.1 高度场建立的核心函数。
bool rcRasterizeTriangles(rcContext* context,
const float* verts, const int /*nv*/,
const int* tris, const unsigned char* triAreaIDs, const int numTris,
rcHeightfield& heightfield, const int flagMergeThreshold)
{
rcAssert(context != NULL);
rcScopedTimer timer(context, RC_TIMER_RASTERIZE_TRIANGLES);
// Rasterize the triangles.
const float inverseCellSize = 1.0f / heightfield.cs;
const float inverseCellHeight = 1.0f / heightfield.ch;
for (int triIndex = 0; triIndex < numTris; ++triIndex)
{
const float* v0 = &verts[tris[triIndex * 3 + 0] * 3];
const float* v1 = &verts[tris[triIndex * 3 + 1] * 3];
const float* v2 = &verts[tris[triIndex * 3 + 2] * 3];
if (!rasterizeTri(v0, v1, v2, triAreaIDs[triIndex], heightfield, heightfield.bmin, heightfield.bmax, heightfield.cs, inverseCellSize, inverseCellHeight, flagMergeThreshold))
{
context->log(RC_LOG_ERROR, "rcRasterizeTriangles: Out of memory.");
return false;
}
}
return true;
}
2.2.2 图解
每个 cell 上面是一个 spanList。 他存储着所有在这个cell上面的 所有 三角形在这个cell上的高度信息。

X. Ref
- A* : https://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#heuristics-for-grid-maps
- UE DOC: https://www.unrealdoc.com/p/navigation-mesh
Unreal RecastNavigation 开源项目详解的更多相关文章
- eclipse里面构建maven项目详解(转载)
本文来源于:http://my.oschina.net/u/1540325/blog/548530 eclipse里面构建maven项目详解 1 环境安装及分配 Maven是基于项目对象模 ...
- JAVA Eclipse使用Maven构建web项目详解(SSM框架)
tips: 启动项目后,welcome-file的链接即为测试用例 部署maven web项目 Eclipse使用Maven构建web项目详解 pom.xml添加webapp依赖: <depen ...
- [转帖](整理)GNU Hurd项目详解
(整理)GNU Hurd项目详解 http://www.ha97.com/3188.html 发表于: 开源世界 | 作者: 博客教主 标签: GNU,Hurd,详解,项目 Hurd原本是要成为GNU ...
- Redis 配置文件 redis.conf 项目详解
Redis.conf 配置文件详解 # [Redis](http://yijiebuyi.com/category/redis.html) 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, ...
- Mac下Intellij IDea发布Web项目详解一
Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intellij IDea发布J ...
- Mac下Intellij IDea发布Java Web项目详解五 开始测试
测试前准备工作目录 Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intell ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- tomcat通过tomcat 安装根目录下的conf-Catalina-localhost目录发布项目详解
tomcat通过conf-Catalina-localhost目录发布项目详解 Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目 ...
- python选课系统项目详解
选课系统项目详解 选课系统简介及分析 选课系统架构设计分析 选课系统目录设计 管理员视图 注册 登录 创建学校 创建课程 创建讲师 学生视图 注册 登录 选择学校 选择课程 查看分数 教师视图 登录 ...
- Usage、Usage Minimum和Usage Maximum项目详解
(1)一个产生多个数据域(Report Count>1)的主项目之前有一个以上的[用途]时,每个[用途]与一个数据域依次对应,如果数据域个数(Report Count)超过[用途]的个数,则剩余 ...
随机推荐
- 局域网中linux和window共享文件方案——samba
注明: 曾经写过:局域网中如何为Ubuntu20.04和window10共享文件,本文可以视作为该篇的续篇. 本文主要内容为Samba软件的安装和配置,以及相关的磁盘操作. 注意:(硬盘的UUID会受 ...
- spring5的新特性
1.背景 2.依赖环境的变化 整个 Spring5 框架的代码基于 Java8,运行时兼容 JDK9,许多不建议使用的类和方 法在代码库中删除 3.自带了通用的日志封装 3.1.日志的简单使用 Spr ...
- Odd and Even Zeroes 题解
前言 题目链接:洛谷:UVA. 题目简述 定义 \(\operatorname{count}(num)\) 表示 \(num\) 末尾 \(0\) 的个数.给出 \(n\)(\(n \leq 10^{ ...
- 由浅深入理解java多线程,java并发,synchronized实现原理及线程锁机制
由浅深入理解java多线程,java并发,synchronized实现原理及线程锁机制 目录 由浅深入理解java多线程,java并发,synchronized实现原理及线程锁机制 一,线程的生命周期 ...
- sqlserver 索引优化 CPU占用过高 执行分析 服务器检查[转]
SELECT TOP 10 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans) ...
- BST 二叉搜索树 BinarySearchTree C++实现(递归/非递归)
目录 二叉搜索树 基本概念 常用结论 用途 二叉搜索树的性能分析 二叉搜索树的操作 查找 插入 删除 代码实现 BSTree.hpp test.cc 二叉搜索树 基本概念 二叉搜索树(BST,Bina ...
- IP报文格式详解
下图为常见的IP报文格式表: 上面是IP的报文格式,接下来我们先说明各个字段的意义.然后,用Etheral软件转包分析IP的报文格式. 1.版本:ip报文中,版本占了4位,用来表示该协议采用的是那一个 ...
- 自动调用关闭释放资源try-with-resources
try-with-resources自动执行释放资源 看到了try这个关键字立马就应该能想到异常处理机制try-catch-finally语句块.这里要说的东西和异常处理背后的机制其实几乎是一样的,只 ...
- vue grid layout 设定某组件为最低位,其它子item无法拖拽超过
逻辑: 设定某项X为底部 逻辑: for 循环获取最低位的item Y的信息:i 和 y 如果i != X.i initY = X.y 则调整X.y ...
- 【YashanDB数据库】Yashandb表闪回业务表实践
数据误删除 DELETE 操作闪回 示例(HEAP表) 基于闪回查询(建议): select * from sales.branches1; BRANCH_NO BRANCH_NAME AREA_NO ...