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
|
|
|
|
{
|
2023-05-05 03:06:43 +03:00
|
|
|
public class FindNearestPolyQuery : IPolyQuery
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
private readonly NavMeshQuery query;
|
2023-06-03 15:47:26 +03:00
|
|
|
private readonly RcVec3f center;
|
2023-03-16 19:48:49 +03:00
|
|
|
private long nearestRef;
|
2023-06-03 15:47:26 +03:00
|
|
|
private RcVec3f nearestPt;
|
2023-03-16 19:48:49 +03:00
|
|
|
private bool overPoly;
|
|
|
|
private float nearestDistanceSqr;
|
|
|
|
|
2023-06-03 15:47:26 +03:00
|
|
|
public FindNearestPolyQuery(NavMeshQuery 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
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void Process(MeshTile tile, Poly poly, long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
// Find nearest polygon amongst the nearby polygons.
|
2023-05-05 02:44:48 +03:00
|
|
|
Result<ClosestPointOnPolyResult> closest = query.ClosestPointOnPoly(refs, center);
|
|
|
|
bool posOverPoly = closest.result.IsPosOverPoly();
|
|
|
|
var closestPtPoly = closest.result.GetClosest();
|
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-03 15:47:26 +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
|
|
|
|
{
|
2023-06-03 15:47:26 +03:00
|
|
|
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
|
|
|
}
|