forked from mirror/DotRecast
remove nullable float in TraversHeightfield
This commit is contained in:
parent
0614c040ba
commit
47a93f7ab3
|
@ -47,12 +47,12 @@ namespace DotRecast.Detour.Dynamic
|
||||||
*
|
*
|
||||||
* @return Optional with hit parameter (t) or empty if no hit found
|
* @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 relStartX = start.x - origin.x;
|
||||||
float relStartZ = start.z - origin.z;
|
float relStartZ = start.z - origin.z;
|
||||||
|
@ -79,10 +79,10 @@ namespace DotRecast.Detour.Dynamic
|
||||||
float t = 0;
|
float t = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
float? hit = TraversHeightfield(sx, sz, start, end, t, Math.Min(1, Math.Min(tMaxX, tMaxZ)));
|
bool isHit = TraversHeightfield(sx, sz, start, end, t, Math.Min(1, Math.Min(tMaxX, tMaxZ)), out hit);
|
||||||
if (hit.HasValue)
|
if (isHit)
|
||||||
{
|
{
|
||||||
return hit;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((dx > 0 ? sx >= ex : sx <= ex) && (dz > 0 ? sz >= ez : sz <= ez))
|
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);
|
RcHeightfield hf = heightfieldProvider.Invoke(x, z);
|
||||||
if (null != hf)
|
if (null != hf)
|
||||||
|
@ -151,7 +151,8 @@ namespace DotRecast.Detour.Dynamic
|
||||||
{
|
{
|
||||||
if (span.smin <= ymin && span.smax >= ymax)
|
if (span.smin <= ymin && span.smax >= ymax)
|
||||||
{
|
{
|
||||||
return Math.Min(1, tMin + t);
|
hit = Math.Min(1, tMin + t);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
span = span.next;
|
span = span.next;
|
||||||
|
@ -178,7 +179,8 @@ namespace DotRecast.Detour.Dynamic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
hit = 0.0f;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -174,12 +174,12 @@ public class DynamicUpdateTool : IRcTool
|
||||||
RcVec3f sp = RcVec3f.Of(spos.x, spos.y + 1.3f, spos.z);
|
RcVec3f sp = RcVec3f.Of(spos.x, spos.y + 1.3f, spos.z);
|
||||||
RcVec3f ep = RcVec3f.Of(epos.x, epos.y + 1.3f, epos.z);
|
RcVec3f ep = RcVec3f.Of(epos.x, epos.y + 1.3f, epos.z);
|
||||||
long t1 = RcFrequency.Ticks;
|
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;
|
long t2 = RcFrequency.Ticks;
|
||||||
raycastTime = (t2 - t1) / TimeSpan.TicksPerMillisecond;
|
raycastTime = (t2 - t1) / TimeSpan.TicksPerMillisecond;
|
||||||
raycastHit = hitPos.HasValue;
|
raycastHit = hasHit;
|
||||||
raycastHitPos = hitPos.HasValue
|
raycastHitPos = hasHit
|
||||||
? 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))
|
? RcVec3f.Of(sp.x + hitPos * (ep.x - sp.x), sp.y + hitPos * (ep.y - sp.y), sp.z + hitPos * (ep.z - sp.z))
|
||||||
: ep;
|
: ep;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ public class VoxelQueryTest
|
||||||
RcVec3f end = RcVec3f.Of(320, 10, 57);
|
RcVec3f end = RcVec3f.Of(320, 10, 57);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
query.Raycast(start, end);
|
query.Raycast(start, end, out var hit);
|
||||||
// Then
|
// Then
|
||||||
hfProvider.Verify(mock => mock.Invoke(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(6));
|
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 }));
|
Assert.That(captorX, Is.EqualTo(new[] { 0, 1, 1, 1, 2, 2 }));
|
||||||
|
@ -73,8 +73,8 @@ public class VoxelQueryTest
|
||||||
VoxelQuery query = mesh.VoxelQuery();
|
VoxelQuery query = mesh.VoxelQuery();
|
||||||
RcVec3f start = RcVec3f.Of(7.4f, 0.5f, -64.8f);
|
RcVec3f start = RcVec3f.Of(7.4f, 0.5f, -64.8f);
|
||||||
RcVec3f end = RcVec3f.Of(31.2f, 0.5f, -75.3f);
|
RcVec3f end = RcVec3f.Of(31.2f, 0.5f, -75.3f);
|
||||||
float? hit = query.Raycast(start, end);
|
bool isHit = query.Raycast(start, end, out var hit);
|
||||||
Assert.That(hit, Is.Null);
|
Assert.That(isHit, Is.EqualTo(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -84,9 +84,9 @@ public class VoxelQueryTest
|
||||||
VoxelQuery query = mesh.VoxelQuery();
|
VoxelQuery query = mesh.VoxelQuery();
|
||||||
RcVec3f start = RcVec3f.Of(32.3f, 0.5f, 47.9f);
|
RcVec3f start = RcVec3f.Of(32.3f, 0.5f, 47.9f);
|
||||||
RcVec3f end = RcVec3f.Of(-31.2f, 0.5f, -29.8f);
|
RcVec3f end = RcVec3f.Of(-31.2f, 0.5f, -29.8f);
|
||||||
float? hit = query.Raycast(start, end);
|
bool isHit = query.Raycast(start, end, out var hit);
|
||||||
Assert.That(hit, Is.Not.Null);
|
Assert.That(isHit, Is.EqualTo(true));
|
||||||
Assert.That(hit.Value, Is.EqualTo(0.5263836f).Within(1e-7f));
|
Assert.That(hit, Is.EqualTo(0.5263836f).Within(1e-7f));
|
||||||
}
|
}
|
||||||
|
|
||||||
private DynamicNavMesh CreateDynaMesh()
|
private DynamicNavMesh CreateDynaMesh()
|
||||||
|
|
Loading…
Reference in New Issue