【golang/navmesh】使用recast navigation进行寻路

目录

说在前面

  • go version:1.20.2 linux/amd64
  • 操作系统:wsl2
  • detour-go版本:v0.2.0
  • github:这里,求star!

安装

  • 使用go mod安装即可

    shell 复制代码
    go get github.com/o0olele/detour-go

使用

  • 使用场景模型构建navmesh

    • 通过recast navigation demo进行构建;构建完成后点击Save保存二进制文件
    • 通过在线工具构建;完成后点击Export as Recast NavMesh保存二进制文件
  • 使用detour-go加载二进制navmesh文件

    c 复制代码
    var mesh = loader.LoadTileMesh("./navmesh.bin")
    if mesh == nil {
    	panic("load mesh failed.")
    }
  • 进行寻路

    c 复制代码
    // 初始化nav mesh query
    var meshQuery = detour.DtAllocNavMeshQuery()
    var status = meshQuery.Init(mesh, 2048)
    if detour.DtStatusFailed(status) {
    	panic("init mesh query failed.")
    }
    
    // 初始化 query filter
    var meshFilter = detour.DtAllocDtQueryFilter()
    
    // 确定寻路起点
    var agentPos [3]float32
    var agentHalfExtents = [3]float32{1, 0.75, 1}
    var agentNearestPoly detour.DtPolyRef
    status = meshQuery.FindNearestPoly(agentPos[:], agentHalfExtents[:], meshFilter, &agentNearestPoly, agentPos[:])
    if detour.DtStatusFailed(status) {
    	panic("find closest point failed.")
    }
    
    // 确定寻路终点
    var agentTarget = [3]float32{1.1322085857391357, 10.197294235229492, -5.400757312774658}
    var agentTragetRef detour.DtPolyRef
    status = meshQuery.FindNearestPoly(agentTarget[:], agentHalfExtents[:], meshFilter, &agentTragetRef, agentTarget[:])
    if detour.DtStatusFailed(status) {
    	panic("find agent target closest point failed.")
    }
    
    // 寻路
    var path [256]detour.DtPolyRef
    var pathCount int
    meshQuery.FindPath(agentNearestPoly, agentTragetRef, agentPos[:], agentTarget[:], meshFilter, path[:], &pathCount, 256)
    
    // 详细路径
    var straightPath [256 * 3]float32
    var straightPathFlags [256]detour.DtStraightPathFlags
    var straightPathRef [256]detour.DtPolyRef
    var straightPathCount int
    meshQuery.FindStraightPath(agentPos[:], agentTarget[:], path[:], pathCount, straightPath[:], straightPathFlags[:], straightPathRef[:], &straightPathCount, 256, 0)
    fmt.Println(straightPath[:straightPathCount*3])

可视化

  • 复制examples/web下的文件

    shell 复制代码
    go run main.go
  • 在浏览器中访问http://localhost:9001/public

  • 点击LoadTileMesh,选择刚刚保存的二进制navmesh文件

  • 点击Add Agent,添加一个agent

  • 鼠标左键选择移动的目标点

相关推荐
Aaron - Wistron28 分钟前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境39 分钟前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境1 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
Dxy12393102161 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
0566462 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄2 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰2 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#
昆曲之源_娄江河畔2 小时前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql
IT_陈寒3 小时前
Vite热更新失效?你可能漏了这个配置
前端·人工智能·后端
Leighteen3 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言