2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using DotRecast.Core;
|
|
|
|
using DotRecast.Detour.Dynamic.Io;
|
|
|
|
using DotRecast.Recast;
|
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace DotRecast.Detour.Dynamic.Test;
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
public class VoxelQueryTest
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
private const int TILE_WIDTH = 100;
|
|
|
|
private const int TILE_DEPTH = 90;
|
2023-03-29 18:59:00 +03:00
|
|
|
private static readonly Vector3f ORIGIN = Vector3f.Of(50, 10, 40);
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void shouldTraverseTiles()
|
|
|
|
{
|
|
|
|
var hfProvider = new Mock<Func<int, int, Heightfield>>();
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
// Given
|
|
|
|
List<int> captorX = new();
|
|
|
|
List<int> captorZ = new();
|
|
|
|
|
|
|
|
hfProvider
|
|
|
|
.Setup(e => e.Invoke(It.IsAny<int>(), It.IsAny<int>()))
|
|
|
|
.Returns((Heightfield)null)
|
|
|
|
.Callback<int, int>((x, z) =>
|
|
|
|
{
|
|
|
|
captorX.Add(x);
|
|
|
|
captorZ.Add(z);
|
|
|
|
});
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
VoxelQuery query = new VoxelQuery(ORIGIN, TILE_WIDTH, TILE_DEPTH, hfProvider.Object);
|
2023-03-31 18:28:54 +03:00
|
|
|
Vector3f start = Vector3f.Of(120, 10, 365);
|
|
|
|
Vector3f end = Vector3f.Of(320, 10, 57);
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-03-14 08:02:43 +03:00
|
|
|
// When
|
|
|
|
query.raycast(start, end);
|
|
|
|
// Then
|
|
|
|
hfProvider.Verify(mock => mock.Invoke(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(6));
|
2023-03-16 19:48:49 +03:00
|
|
|
Assert.That(captorX, Is.EqualTo(new[] { 0, 1, 1, 1, 2, 2 }));
|
|
|
|
Assert.That(captorZ, Is.EqualTo(new[] { 3, 3, 2, 1, 1, 0 }));
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-03-16 19:48:49 +03:00
|
|
|
public void shouldHandleRaycastWithoutObstacles()
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
DynamicNavMesh mesh = createDynaMesh();
|
|
|
|
VoxelQuery query = mesh.voxelQuery();
|
2023-03-31 18:28:54 +03:00
|
|
|
Vector3f start = Vector3f.Of(7.4f, 0.5f, -64.8f);
|
|
|
|
Vector3f end = Vector3f.Of(31.2f, 0.5f, -75.3f);
|
2023-03-14 08:02:43 +03:00
|
|
|
float? hit = query.raycast(start, end);
|
|
|
|
Assert.That(hit, Is.Null);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2023-03-16 19:48:49 +03:00
|
|
|
public void shouldHandleRaycastWithObstacles()
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
DynamicNavMesh mesh = createDynaMesh();
|
|
|
|
VoxelQuery query = mesh.voxelQuery();
|
2023-03-31 18:28:54 +03:00
|
|
|
Vector3f start = Vector3f.Of(32.3f, 0.5f, 47.9f);
|
|
|
|
Vector3f end = Vector3f.Of(-31.2f, 0.5f, -29.8f);
|
2023-03-14 08:02:43 +03:00
|
|
|
float? hit = query.raycast(start, end);
|
|
|
|
Assert.That(hit, Is.Not.Null);
|
|
|
|
Assert.That(hit.Value, Is.EqualTo(0.5263836f).Within(1e-7f));
|
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
private DynamicNavMesh createDynaMesh()
|
|
|
|
{
|
2023-03-14 08:02:43 +03:00
|
|
|
var 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
|
|
|
|
var mesh = new DynamicNavMesh(f);
|
|
|
|
// build navmesh asynchronously using multiple threads
|
|
|
|
Task<bool> future = mesh.build(Task.Factory);
|
|
|
|
// wait for build to complete
|
|
|
|
var _ = future.Result;
|
|
|
|
return mesh;
|
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|