DtFindNearestPolyQuery.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using DotRecast.Core;
  3. namespace DotRecast.Detour
  4. {
  5. public class DtFindNearestPolyQuery : IDtPolyQuery
  6. {
  7. private readonly DtNavMeshQuery _query;
  8. private readonly RcVec3f _center;
  9. private long _nearestRef;
  10. private RcVec3f _nearestPt;
  11. private bool _overPoly;
  12. private float _nearestDistanceSqr;
  13. public DtFindNearestPolyQuery(DtNavMeshQuery query, RcVec3f center)
  14. {
  15. this._query = query;
  16. this._center = center;
  17. _nearestDistanceSqr = float.MaxValue;
  18. _nearestPt = center;
  19. }
  20. public void Process(DtMeshTile tile, DtPoly poly, long refs)
  21. {
  22. // Find nearest polygon amongst the nearby polygons.
  23. _query.ClosestPointOnPoly(refs, _center, out var closestPtPoly, out var posOverPoly);
  24. // If a point is directly over a polygon and closer than
  25. // climb height, favor that instead of straight line nearest point.
  26. float d = 0;
  27. RcVec3f diff = _center.Subtract(closestPtPoly);
  28. if (posOverPoly)
  29. {
  30. d = Math.Abs(diff.y) - tile.data.header.walkableClimb;
  31. d = d > 0 ? d * d : 0;
  32. }
  33. else
  34. {
  35. d = RcVec3f.LenSqr(diff);
  36. }
  37. if (d < _nearestDistanceSqr)
  38. {
  39. _nearestPt = closestPtPoly;
  40. _nearestDistanceSqr = d;
  41. _nearestRef = refs;
  42. _overPoly = posOverPoly;
  43. }
  44. }
  45. public long NearestRef()
  46. {
  47. return _nearestRef;
  48. }
  49. public RcVec3f NearestPt()
  50. {
  51. return _nearestPt;
  52. }
  53. public bool OverPoly()
  54. {
  55. return _overPoly;
  56. }
  57. }
  58. }