DotRecastNetSim/src/DotRecast.Detour/FindNearestPolyQuery.cs

61 lines
1.9 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
{
2023-03-25 09:43:20 +03:00
using static DotRecast.Core.RecastMath;
2023-03-16 19:48:49 +03:00
public class FindNearestPolyQuery : PolyQuery
{
private readonly NavMeshQuery query;
2023-03-28 19:52:26 +03:00
private readonly Vector3f center;
2023-03-16 19:48:49 +03:00
private long nearestRef;
2023-03-28 19:52:26 +03:00
private Vector3f nearestPt;
2023-03-16 19:48:49 +03:00
private bool overPoly;
private float nearestDistanceSqr;
2023-03-28 19:52:26 +03:00
public FindNearestPolyQuery(NavMeshQuery query, Vector3f 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-05-05 02:44:48 +03:00
Vector3f diff = VSub(center, 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-05-05 02:44:48 +03:00
d = VLenSqr(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
}