DotRecastNetSim/src/DotRecast.Detour/DtFindNearestPolyQuery.cs

57 lines
1.7 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
using System;
2023-03-28 19:52:26 +03:00
using DotRecast.Core;
2023-04-23 08:13:10 +03:00
using DotRecast.Detour.QueryResults;
2023-03-14 08:02:43 +03:00
2023-03-16 19:09:10 +03:00
namespace DotRecast.Detour
{
public class DtFindNearestPolyQuery : IDtPolyQuery
2023-03-16 19:48:49 +03:00
{
private readonly DtNavMeshQuery query;
private readonly RcVec3f center;
2023-03-16 19:48:49 +03:00
private long nearestRef;
private RcVec3f nearestPt;
2023-03-16 19:48:49 +03:00
private bool overPoly;
private float nearestDistanceSqr;
public DtFindNearestPolyQuery(DtNavMeshQuery query, RcVec3f center)
2023-03-16 19:48:49 +03:00
{
this.query = query;
this.center = center;
nearestDistanceSqr = float.MaxValue;
2023-03-28 19:52:26 +03:00
nearestPt = center;
2023-03-14 08:02:43 +03:00
}
public void Process(DtMeshTile tile, DtPoly poly, long refs)
2023-03-16 19:48:49 +03:00
{
// Find nearest polygon amongst the nearby polygons.
2023-06-10 09:16:52 +03:00
query.ClosestPointOnPoly(refs, center, out var closestPtPoly, out var posOverPoly);
2023-03-16 19:48:49 +03:00
// If a point is directly over a polygon and closer than
// climb height, favor that instead of straight line nearest point.
float d = 0;
RcVec3f diff = center.Subtract(closestPtPoly);
2023-03-16 19:48:49 +03:00
if (posOverPoly)
{
2023-04-29 06:48:56 +03:00
d = Math.Abs(diff.y) - tile.data.header.walkableClimb;
2023-03-16 19:48:49 +03:00
d = d > 0 ? d * d : 0;
}
else
{
d = RcVec3f.LenSqr(diff);
2023-03-16 19:48:49 +03:00
}
if (d < nearestDistanceSqr)
{
nearestPt = closestPtPoly;
nearestDistanceSqr = d;
nearestRef = refs;
overPoly = posOverPoly;
}
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public FindNearestPolyResult Result()
2023-03-16 19:48:49 +03:00
{
return new FindNearestPolyResult(nearestRef, nearestPt, overPoly);
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:09:10 +03:00
}