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

77 lines
3.5 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;
using NUnit.Framework;
namespace DotRecast.Detour.Dynamic.Test;
2023-03-16 19:48:49 +03:00
public class DynamicNavMeshTest
{
2023-03-29 18:59:00 +03:00
private static readonly Vector3f START_POS = Vector3f.Of(70.87453f, 0.0010070801f, 86.69021f);
private static readonly Vector3f END_POS = Vector3f.Of(-50.22061f, 0.0010070801f, -70.761444f);
private static readonly Vector3f EXTENT = Vector3f.Of(0.1f, 0.1f, 0.1f);
private static readonly Vector3f SPHERE_POS = Vector3f.Of(45.381645f, 0.0010070801f, 52.68981f);
2023-03-14 08:02:43 +03:00
[Test]
2023-03-16 19:48:49 +03:00
public void e2eTest()
{
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();
VoxelFile f = reader.read(bis);
// create dynamic navmesh
DynamicNavMesh mesh = new DynamicNavMesh(f);
// build navmesh asynchronously using multiple threads
Task<bool> future = mesh.build(Task.Factory);
// wait for build to complete
bool _ = future.Result;
// create new query
NavMeshQuery query = new NavMeshQuery(mesh.navMesh());
QueryFilter filter = new DefaultQueryFilter();
// find path
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(),
2023-03-16 19:48:49 +03:00
end.getNearestPos(), filter, NavMeshQuery.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
Collider colldier = new SphereCollider(SPHERE_POS, 20, SampleAreaModifications.SAMPLE_POLYAREA_TYPE_GROUND, 0.1f);
long colliderId = mesh.addCollider(colldier);
// update navmesh asynchronously
future = mesh.update(Task.Factory);
// wait for update to complete
_ = future.Result;
// create new query
query = new NavMeshQuery(mesh.navMesh());
// find path again
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,
2023-03-16 19:48:49 +03:00
NavMeshQuery.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
mesh.removeCollider(colliderId);
// update navmesh asynchronously
future = mesh.update(Task.Factory);
// wait for update to complete
_ = future.Result;
// create new query
query = new NavMeshQuery(mesh.navMesh());
// find path one more time
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,
2023-03-16 19:48:49 +03:00
NavMeshQuery.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
}