remove nullable float in DtNavMeshRaycast.Raycast, RcPolyMeshRaycast.Raycast

This commit is contained in:
ikpil 2023-08-01 23:32:28 +09:00
parent ccba2afb0c
commit 912b9e27fb
4 changed files with 36 additions and 35 deletions

View File

@ -20,32 +20,32 @@ using DotRecast.Core;
namespace DotRecast.Detour namespace DotRecast.Detour
{ {
/** /**
* Simple helper to find an intersection between a ray and a nav mesh * Simple helper to find an intersection between a ray and a nav mesh
*/ */
public static class DtNavMeshRaycast public static class DtNavMeshRaycast
{ {
public static float? Raycast(DtNavMesh mesh, RcVec3f src, RcVec3f dst) public static bool Raycast(DtNavMesh mesh, RcVec3f src, RcVec3f dst, out float hitTime)
{ {
hitTime = 0.0f;
for (int t = 0; t < mesh.GetMaxTiles(); ++t) for (int t = 0; t < mesh.GetMaxTiles(); ++t)
{ {
DtMeshTile tile = mesh.GetTile(t); DtMeshTile tile = mesh.GetTile(t);
if (tile != null && tile.data != null) if (tile != null && tile.data != null)
{ {
float? intersection = Raycast(tile, src, dst); if (Raycast(tile, src, dst, out hitTime))
if (null != intersection)
{ {
return intersection; return true;
} }
} }
} }
return null; return false;
} }
private static float? Raycast(DtMeshTile tile, RcVec3f sp, RcVec3f sq) private static bool Raycast(DtMeshTile tile, RcVec3f sp, RcVec3f sq, out float hitTime)
{ {
hitTime = 0.0f;
for (int i = 0; i < tile.data.header.polyCount; ++i) for (int i = 0; i < tile.data.header.polyCount; ++i)
{ {
DtPoly p = tile.data.polys[i]; DtPoly p = tile.data.polys[i];
@ -79,9 +79,9 @@ namespace DotRecast.Detour
} }
} }
if (Intersections.IntersectSegmentTriangle(sp, sq, verts[0], verts[1], verts[2], out var intersection)) if (Intersections.IntersectSegmentTriangle(sp, sq, verts[0], verts[1], verts[2], out hitTime))
{ {
return intersection; return true;
} }
} }
} }
@ -91,7 +91,7 @@ namespace DotRecast.Detour
} }
} }
return null; return false;
} }
} }
} }

View File

@ -770,20 +770,21 @@ public class RecastDemo : IRecastDemoChannel
if (_sample == null) if (_sample == null)
return; return;
float? hit = null; float hitTime = 0.0f;
bool hit = false;
if (inputGeom != null) if (inputGeom != null)
{ {
hit = inputGeom.RaycastMesh(rayStart, rayEnd); hit = inputGeom.RaycastMesh(rayStart, rayEnd, out hitTime);
} }
if (!hit.HasValue && _sample.GetNavMesh() != null) if (!hit && _sample.GetNavMesh() != null)
{ {
hit = DtNavMeshRaycast.Raycast(_sample.GetNavMesh(), rayStart, rayEnd); hit = DtNavMeshRaycast.Raycast(_sample.GetNavMesh(), rayStart, rayEnd, out hitTime);
} }
if (!hit.HasValue && _sample.GetRecastResults() != null) if (!hit && _sample.GetRecastResults() != null)
{ {
hit = RcPolyMeshRaycast.Raycast(_sample.GetRecastResults(), rayStart, rayEnd); hit = RcPolyMeshRaycast.Raycast(_sample.GetRecastResults(), rayStart, rayEnd, out hitTime);
} }
RcVec3f rayDir = RcVec3f.Of(rayEnd.x - rayStart.x, rayEnd.y - rayStart.y, rayEnd.z - rayStart.z); RcVec3f rayDir = RcVec3f.Of(rayEnd.x - rayStart.x, rayEnd.y - rayStart.y, rayEnd.z - rayStart.z);
@ -795,9 +796,8 @@ public class RecastDemo : IRecastDemoChannel
rayTool.HandleClickRay(rayStart, rayDir, processHitTestShift); rayTool.HandleClickRay(rayStart, rayDir, processHitTestShift);
} }
if (hit.HasValue) if (hit)
{ {
float hitTime = hit.Value;
if (0 != (_modState & KeyModState.Control)) if (0 != (_modState & KeyModState.Control))
{ {
// Marker // Marker

View File

@ -126,12 +126,14 @@ namespace DotRecast.Recast.DemoTool.Geom
_offMeshConnections.RemoveAll(filter); // TODO : 확인 필요 _offMeshConnections.RemoveAll(filter); // TODO : 확인 필요
} }
public float? RaycastMesh(RcVec3f src, RcVec3f dst) public bool RaycastMesh(RcVec3f src, RcVec3f dst, out float tmin)
{ {
tmin = 1.0f;
// Prune hit ray. // Prune hit ray.
if (!Intersections.IsectSegAABB(src, dst, bmin, bmax, out var btmin, out var btmax)) if (!Intersections.IsectSegAABB(src, dst, bmin, bmax, out var btmin, out var btmax))
{ {
return null; return false;
} }
float[] p = new float[2]; float[] p = new float[2];
@ -144,10 +146,10 @@ namespace DotRecast.Recast.DemoTool.Geom
List<RcChunkyTriMeshNode> chunks = _mesh.chunkyTriMesh.GetChunksOverlappingSegment(p, q); List<RcChunkyTriMeshNode> chunks = _mesh.chunkyTriMesh.GetChunksOverlappingSegment(p, q);
if (0 == chunks.Count) if (0 == chunks.Count)
{ {
return null; return false;
} }
float? tmin = 1.0f; tmin = 1.0f;
bool hit = false; bool hit = false;
foreach (RcChunkyTriMeshNode chunk in chunks) foreach (RcChunkyTriMeshNode chunk in chunks)
{ {
@ -181,9 +183,7 @@ namespace DotRecast.Recast.DemoTool.Geom
} }
} }
return hit return hit;
? tmin
: null;
} }

View File

@ -23,25 +23,26 @@ namespace DotRecast.Recast
{ {
public static class RcPolyMeshRaycast public static class RcPolyMeshRaycast
{ {
public static float? Raycast(IList<RecastBuilderResult> results, RcVec3f src, RcVec3f dst) public static bool Raycast(IList<RecastBuilderResult> results, RcVec3f src, RcVec3f dst, out float hitTime)
{ {
hitTime = 0.0f;
foreach (RecastBuilderResult result in results) foreach (RecastBuilderResult result in results)
{ {
if (result.GetMeshDetail() != null) if (result.GetMeshDetail() != null)
{ {
float? intersection = Raycast(result.GetMesh(), result.GetMeshDetail(), src, dst); if (Raycast(result.GetMesh(), result.GetMeshDetail(), src, dst, out hitTime))
if (null != intersection)
{ {
return intersection; return true;
} }
} }
} }
return null; return false;
} }
private static float? Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3f sp, RcVec3f sq) private static bool Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3f sp, RcVec3f sq, out float hitTime)
{ {
hitTime = 0;
if (meshDetail != null) if (meshDetail != null)
{ {
for (int i = 0; i < meshDetail.nmeshes; ++i) for (int i = 0; i < meshDetail.nmeshes; ++i)
@ -62,9 +63,9 @@ namespace DotRecast.Recast
vs[k].z = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 2]; vs[k].z = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3 + 2];
} }
if (Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2], out var intersection)) if (Intersections.IntersectSegmentTriangle(sp, sq, vs[0], vs[1], vs[2], out hitTime))
{ {
return intersection; return true;
} }
} }
} }
@ -74,7 +75,7 @@ namespace DotRecast.Recast
// TODO: check PolyMesh instead // TODO: check PolyMesh instead
} }
return null; return false;
} }
} }
} }