remove nullable float in TraversHeightfield

This commit is contained in:
ikpil 2023-07-31 10:17:59 +09:00
parent 0614c040ba
commit 47a93f7ab3
3 changed files with 23 additions and 21 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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<int>(), It.IsAny<int>()), 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()