From 47a93f7ab3b43c82684a4e2eaa872dee99348936 Mon Sep 17 00:00:00 2001 From: ikpil Date: Mon, 31 Jul 2023 10:17:59 +0900 Subject: [PATCH] remove nullable float in TraversHeightfield --- src/DotRecast.Detour.Dynamic/VoxelQuery.cs | 24 ++++++++++--------- .../Tools/DynamicUpdateTool.cs | 8 +++---- .../VoxelQueryTest.cs | 12 +++++----- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/DotRecast.Detour.Dynamic/VoxelQuery.cs b/src/DotRecast.Detour.Dynamic/VoxelQuery.cs index 2a17cc7..f2c2d40 100644 --- a/src/DotRecast.Detour.Dynamic/VoxelQuery.cs +++ b/src/DotRecast.Detour.Dynamic/VoxelQuery.cs @@ -47,12 +47,12 @@ namespace DotRecast.Detour.Dynamic * * @return Optional with hit parameter (t) or empty if no hit found */ - public float? Raycast(RcVec3f start, RcVec3f end) + public bool Raycast(RcVec3f start, RcVec3f end, out float hit) { - return TraverseTiles(start, end); + return TraverseTiles(start, end, out hit); } - private float? TraverseTiles(RcVec3f start, RcVec3f end) + private bool TraverseTiles(RcVec3f start, RcVec3f end, out float hit) { float relStartX = start.x - origin.x; float relStartZ = start.z - origin.z; @@ -79,10 +79,10 @@ namespace DotRecast.Detour.Dynamic float t = 0; while (true) { - float? hit = TraversHeightfield(sx, sz, start, end, t, Math.Min(1, Math.Min(tMaxX, tMaxZ))); - if (hit.HasValue) + bool isHit = TraversHeightfield(sx, sz, start, end, t, Math.Min(1, Math.Min(tMaxX, tMaxZ)), out hit); + if (isHit) { - return hit; + return true; } if ((dx > 0 ? sx >= ex : sx <= ex) && (dz > 0 ? sz >= ez : sz <= ez)) @@ -104,10 +104,10 @@ namespace DotRecast.Detour.Dynamic } } - return null; + return false; } - private float? TraversHeightfield(int x, int z, RcVec3f start, RcVec3f end, float tMin, float tMax) + private bool TraversHeightfield(int x, int z, RcVec3f start, RcVec3f end, float tMin, float tMax, out float hit) { RcHeightfield hf = heightfieldProvider.Invoke(x, z); if (null != hf) @@ -151,7 +151,8 @@ namespace DotRecast.Detour.Dynamic { if (span.smin <= ymin && span.smax >= ymax) { - return Math.Min(1, tMin + t); + hit = Math.Min(1, tMin + t); + return true; } span = span.next; @@ -178,7 +179,8 @@ namespace DotRecast.Detour.Dynamic } } - return null; + hit = 0.0f; + return false; } } -} +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs b/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs index 08078c9..55b05e8 100644 --- a/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/DynamicUpdateTool.cs @@ -174,12 +174,12 @@ public class DynamicUpdateTool : IRcTool RcVec3f sp = RcVec3f.Of(spos.x, spos.y + 1.3f, spos.z); RcVec3f ep = RcVec3f.Of(epos.x, epos.y + 1.3f, epos.z); long t1 = RcFrequency.Ticks; - float? hitPos = dynaMesh.VoxelQuery().Raycast(sp, ep); + bool hasHit = dynaMesh.VoxelQuery().Raycast(sp, ep, out var hitPos); long t2 = RcFrequency.Ticks; raycastTime = (t2 - t1) / TimeSpan.TicksPerMillisecond; - raycastHit = hitPos.HasValue; - raycastHitPos = hitPos.HasValue - ? RcVec3f.Of(sp.x + hitPos.Value * (ep.x - sp.x), sp.y + hitPos.Value * (ep.y - sp.y), sp.z + hitPos.Value * (ep.z - sp.z)) + raycastHit = hasHit; + raycastHitPos = hasHit + ? RcVec3f.Of(sp.x + hitPos * (ep.x - sp.x), sp.y + hitPos * (ep.y - sp.y), sp.z + hitPos * (ep.z - sp.z)) : ep; } } diff --git a/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs b/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs index 42b93ba..0590310 100644 --- a/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs +++ b/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs @@ -59,7 +59,7 @@ public class VoxelQueryTest RcVec3f end = RcVec3f.Of(320, 10, 57); // When - query.Raycast(start, end); + query.Raycast(start, end, out var hit); // Then hfProvider.Verify(mock => mock.Invoke(It.IsAny(), It.IsAny()), Times.Exactly(6)); Assert.That(captorX, Is.EqualTo(new[] { 0, 1, 1, 1, 2, 2 })); @@ -73,8 +73,8 @@ public class VoxelQueryTest VoxelQuery query = mesh.VoxelQuery(); RcVec3f start = RcVec3f.Of(7.4f, 0.5f, -64.8f); RcVec3f end = RcVec3f.Of(31.2f, 0.5f, -75.3f); - float? hit = query.Raycast(start, end); - Assert.That(hit, Is.Null); + bool isHit = query.Raycast(start, end, out var hit); + Assert.That(isHit, Is.EqualTo(false)); } [Test] @@ -84,9 +84,9 @@ public class VoxelQueryTest VoxelQuery query = mesh.VoxelQuery(); RcVec3f start = RcVec3f.Of(32.3f, 0.5f, 47.9f); RcVec3f end = RcVec3f.Of(-31.2f, 0.5f, -29.8f); - float? hit = query.Raycast(start, end); - Assert.That(hit, Is.Not.Null); - Assert.That(hit.Value, Is.EqualTo(0.5263836f).Within(1e-7f)); + bool isHit = query.Raycast(start, end, out var hit); + Assert.That(isHit, Is.EqualTo(true)); + Assert.That(hit, Is.EqualTo(0.5263836f).Within(1e-7f)); } private DynamicNavMesh CreateDynaMesh()