DotRecastNetSim/test/DotRecast.Detour.Dynamic.Test/DynamicNavMeshTest.cs

81 lines
3.3 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DotRecast.Core;
using DotRecast.Core.Numerics;
2023-03-14 08:02:43 +03:00
using DotRecast.Detour.Dynamic.Colliders;
using DotRecast.Detour.Dynamic.Io;
2023-08-05 16:39:18 +03:00
using DotRecast.Detour.Dynamic.Test.Io;
2023-03-14 08:02:43 +03:00
using NUnit.Framework;
namespace DotRecast.Detour.Dynamic.Test;
2023-03-16 19:48:49 +03:00
public class DynamicNavMeshTest
{
2023-10-12 17:52:32 +03:00
private static readonly RcVec3f START_POS = new RcVec3f(70.87453f, 0.0010070801f, 86.69021f);
private static readonly RcVec3f END_POS = new RcVec3f(-50.22061f, 0.0010070801f, -70.761444f);
private static readonly RcVec3f EXTENT = new RcVec3f(0.1f, 0.1f, 0.1f);
private static readonly RcVec3f SPHERE_POS = new RcVec3f(45.381645f, 0.0010070801f, 52.68981f);
2023-03-14 08:02:43 +03:00
[Test]
2023-05-05 02:44:48 +03:00
public void E2eTest()
2023-03-16 19:48:49 +03:00
{
2024-05-25 03:36:00 +03:00
byte[] bytes = RcIO.ReadFileIfFound("test_tiles.voxels");
2023-03-14 08:02:43 +03:00
using var ms = new MemoryStream(bytes);
2023-08-19 09:45:05 +03:00
using var br = new BinaryReader(ms);
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
// load voxels from file
2023-09-16 06:49:58 +03:00
DtVoxelFileReader reader = new DtVoxelFileReader(DtVoxelTileLZ4ForTestCompressor.Shared);
DtVoxelFile f = reader.Read(br);
2023-03-14 08:02:43 +03:00
// create dynamic navmesh
2023-09-16 06:49:58 +03:00
DtDynamicNavMesh mesh = new DtDynamicNavMesh(f);
2023-03-14 08:02:43 +03:00
// build navmesh asynchronously using multiple threads
mesh.Build(Task.Factory);
2023-06-11 07:28:05 +03:00
2023-03-14 08:02:43 +03:00
// create new query
DtNavMeshQuery query = new DtNavMeshQuery(mesh.NavMesh());
IDtQueryFilter filter = new DtQueryDefaultFilter();
2023-06-11 07:28:05 +03:00
2023-03-14 08:02:43 +03:00
// find path
2023-06-11 07:28:05 +03:00
query.FindNearestPoly(START_POS, EXTENT, filter, out var startRef, out var startPt, out var _);
query.FindNearestPoly(END_POS, EXTENT, filter, out var endRef, out var endPt, out var _);
2023-06-23 01:33:03 +03:00
var path = new List<long>();
query.FindPath(startRef, endRef, startPt, endPt, filter, ref path, DtFindPathOption.AnyAngle);
2023-03-14 08:02:43 +03:00
// check path length without any obstacles
Assert.That(path.Count, Is.EqualTo(16));
2023-06-11 07:28:05 +03:00
2023-03-14 08:02:43 +03:00
// place obstacle
2023-09-16 06:49:58 +03:00
IDtCollider colldier = new DtSphereCollider(SPHERE_POS, 20, SampleAreaModifications.SAMPLE_POLYAREA_TYPE_GROUND, 0.1f);
2023-05-05 02:44:48 +03:00
long colliderId = mesh.AddCollider(colldier);
2023-06-11 07:28:05 +03:00
2023-03-14 08:02:43 +03:00
// update navmesh asynchronously
mesh.Update(Task.Factory);
2023-03-14 08:02:43 +03:00
// create new query
query = new DtNavMeshQuery(mesh.NavMesh());
2023-06-11 07:28:05 +03:00
2023-03-14 08:02:43 +03:00
// find path again
2023-06-11 07:28:05 +03:00
query.FindNearestPoly(START_POS, EXTENT, filter, out startRef, out startPt, out var _);
query.FindNearestPoly(END_POS, EXTENT, filter, out endRef, out endPt, out var _);
2023-06-23 01:33:03 +03:00
query.FindPath(startRef, endRef, startPt, endPt, filter, ref path, DtFindPathOption.AnyAngle);
2023-06-22 18:46:51 +03:00
2023-03-14 08:02:43 +03:00
// check path length with obstacles
Assert.That(path.Count, Is.EqualTo(19));
// remove obstacle
2023-05-05 02:44:48 +03:00
mesh.RemoveCollider(colliderId);
2023-03-14 08:02:43 +03:00
// update navmesh asynchronously
mesh.Update(Task.Factory);
2023-03-14 08:02:43 +03:00
// create new query
query = new DtNavMeshQuery(mesh.NavMesh());
2023-06-22 18:46:51 +03:00
2023-03-14 08:02:43 +03:00
// find path one more time
2023-06-11 07:28:05 +03:00
query.FindNearestPoly(START_POS, EXTENT, filter, out startRef, out startPt, out var _);
query.FindNearestPoly(END_POS, EXTENT, filter, out endRef, out endPt, out var _);
2023-06-23 01:33:03 +03:00
query.FindPath(startRef, endRef, startPt, endPt, filter, ref path, DtFindPathOption.AnyAngle);
2023-06-22 18:46:51 +03:00
2023-03-14 08:02:43 +03:00
// path length should be back to the initial value
Assert.That(path.Count, Is.EqualTo(16));
}
2023-03-16 19:48:49 +03:00
}