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

79 lines
3.6 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;
// create new query
DtNavMeshQuery query = new DtNavMeshQuery(mesh.NavMesh());
IDtQueryFilter filter = new DtQueryDefaultFilter();
2023-03-14 08:02:43 +03:00
// find path
2023-05-05 02:44:48 +03:00
FindNearestPolyResult start = query.FindNearestPoly(START_POS, EXTENT, filter).result;
FindNearestPolyResult end = query.FindNearestPoly(END_POS, EXTENT, filter).result;
List<long> path = query.FindPath(start.GetNearestRef(), end.GetNearestRef(), start.GetNearestPos(),
end.GetNearestPos(), filter, DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE, float.MaxValue).result;
2023-03-14 08:02:43 +03:00
// check path length without any obstacles
Assert.That(path.Count, Is.EqualTo(16));
// 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-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-03-14 08:02:43 +03:00
// find path again
2023-05-05 02:44:48 +03:00
start = query.FindNearestPoly(START_POS, EXTENT, filter).result;
end = query.FindNearestPoly(END_POS, EXTENT, filter).result;
path = query.FindPath(start.GetNearestRef(), end.GetNearestRef(), start.GetNearestPos(), end.GetNearestPos(), filter,
DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE, float.MaxValue).result;
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-03-14 08:02:43 +03:00
// find path one more time
2023-05-05 02:44:48 +03:00
start = query.FindNearestPoly(START_POS, EXTENT, filter).result;
end = query.FindNearestPoly(END_POS, EXTENT, filter).result;
path = query.FindPath(start.GetNearestRef(), end.GetNearestRef(), start.GetNearestPos(), end.GetNearestPos(), filter,
DtNavMeshQuery.DT_FINDPATH_ANY_ANGLE, float.MaxValue).result;
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
}