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

86 lines
3.4 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.Detour.Dynamic.Colliders;
using DotRecast.Detour.Dynamic.Io;
2023-04-23 08:13:10 +03:00
using DotRecast.Detour.QueryResults;
2023-03-14 08:02:43 +03:00
using NUnit.Framework;
namespace DotRecast.Detour.Dynamic.Test;
2023-04-25 17:22:44 +03:00
[Parallelizable]
2023-03-16 19:48:49 +03:00
public class DynamicNavMeshTest
{
private static readonly RcVec3f START_POS = RcVec3f.Of(70.87453f, 0.0010070801f, 86.69021f);
private static readonly RcVec3f END_POS = RcVec3f.Of(-50.22061f, 0.0010070801f, -70.761444f);
private static readonly RcVec3f EXTENT = RcVec3f.Of(0.1f, 0.1f, 0.1f);
private static readonly RcVec3f SPHERE_POS = RcVec3f.Of(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
{
2023-03-14 08:02:43 +03:00
byte[] bytes = Loader.ToBytes("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
using var bis = new BinaryReader(ms);
2023-03-16 19:48:49 +03:00
2023-03-14 08:02:43 +03:00
// load voxels from file
VoxelFileReader reader = new VoxelFileReader();
2023-05-05 02:44:48 +03:00
VoxelFile f = reader.Read(bis);
2023-03-14 08:02:43 +03:00
// create dynamic navmesh
DynamicNavMesh mesh = new DynamicNavMesh(f);
// build navmesh asynchronously using multiple threads
2023-05-05 02:44:48 +03:00
Task<bool> future = mesh.Build(Task.Factory);
2023-03-14 08:02:43 +03:00
// wait for build to complete
bool _ = future.Result;
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-05-05 03:06:43 +03:00
ICollider colldier = new SphereCollider(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
2023-05-05 02:44:48 +03:00
future = mesh.Update(Task.Factory);
2023-03-14 08:02:43 +03:00
// wait for update to complete
_ = future.Result;
// 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
2023-05-05 02:44:48 +03:00
future = mesh.Update(Task.Factory);
2023-03-14 08:02:43 +03:00
// wait for update to complete
_ = future.Result;
// 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
}