示例#1
0
        public virtual float GetNormalizedDistanceOfClosestPointAlongCenterPath(AreaPathNode endAreaPathNode, Vector2 point)
        {
            float output           = MathfExtensions.NULL_FLOAT;
            float centerPathLength = 0;
            List <LineSegment2D> centerPathSegments = new List <LineSegment2D>();
            List <AreaPathNode>  _areaPathNodes     = new List <AreaPathNode>();

            _areaPathNodes.AddRange(areaPathNodes);
            AreaPathNode  checkAreaPathNode;
            Vector2       closestPoint;
            float         distanceToClosestPoint = Mathf.Infinity;
            float         checkDistance;
            LineSegment2D centerPathSegment;
            float         currentNormalizedDistance = 0;
            Vector2       checkPoint;

            while (endAreaPathNode != rootAreaPathNode)
            {
                for (int i = 0; i < _areaPathNodes.Count; i++)
                {
                    checkAreaPathNode = _areaPathNodes[i];
                    if (checkAreaPathNode.children.Contains(endAreaPathNode))
                    {
                        centerPathSegment = new LineSegment2D(checkAreaPathNode.trs.position, endAreaPathNode.trs.position);
                        centerPathSegments.Insert(0, centerPathSegment);
                        centerPathLength += Vector2.Distance(endAreaPathNode.trs.position, checkAreaPathNode.trs.position);
                        endAreaPathNode   = checkAreaPathNode;
                        checkPoint        = centerPathSegment.ClosestPoint(point);
                        checkDistance     = Vector2.Distance(checkPoint, point);
                        if (checkDistance < distanceToClosestPoint)
                        {
                            distanceToClosestPoint = checkDistance;
                            closestPoint           = checkPoint;
                            output = currentNormalizedDistance + centerPathSegment.GetDirectedDistanceAlongParallel(closestPoint);
                        }
                        currentNormalizedDistance += centerPathSegment.GetLength() / centerPathLength;
                        break;
                    }
                }
            }
            return(output);
        }
示例#2
0
 public virtual float GetDistance(Vector2 position)
 {
     return(Vector2.Distance(position, lineSegment.ClosestPoint(position)));
 }