DotRecastNetSim/src/DotRecast.Detour/DtFindNearestPolyQuery.cs

67 lines
1.8 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
{
2023-06-11 07:28:05 +03:00
private readonly DtNavMeshQuery _query;
private readonly RcVec3f _center;
private long _nearestRef;
private RcVec3f _nearestPt;
private bool _overPoly;
private float _nearestDistanceSqr;
2023-03-16 19:48:49 +03:00
public DtFindNearestPolyQuery(DtNavMeshQuery query, RcVec3f center)
2023-03-16 19:48:49 +03:00
{
2023-06-11 07:28:05 +03:00
this._query = query;
this._center = center;
_nearestDistanceSqr = float.MaxValue;
_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-11 07:28:05 +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;
2023-06-11 07:28:05 +03:00
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
}
2023-06-11 07:28:05 +03:00
if (d < _nearestDistanceSqr)
2023-03-16 19:48:49 +03:00
{
2023-06-11 07:28:05 +03:00
_nearestPt = closestPtPoly;
_nearestDistanceSqr = d;
_nearestRef = refs;
_overPoly = posOverPoly;
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
2023-06-11 07:28:05 +03:00
public long NearestRef()
2023-03-16 19:48:49 +03:00
{
2023-06-11 07:28:05 +03:00
return _nearestRef;
}
public RcVec3f NearestPt()
{
return _nearestPt;
}
public bool OverPoly()
{
return _overPoly;
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:09:10 +03:00
}