Example #1
0
        /** Move as close as possible to the specified point */
        public void MoveToClosestPoint(Vector3 point)
        {
            if (path == null)
            {
                return;
            }

            float bestDist   = float.PositiveInfinity;
            float bestFactor = 0f;
            int   bestIndex  = 0;

            for (int i = 0; i < path.Count - 1; i++)
            {
                float   factor  = VectorMath.ClosestPointOnLineFactor(path[i], path[i + 1], point);
                Vector3 closest = Vector3.Lerp(path[i], path[i + 1], factor);
                float   dist    = (point - closest).sqrMagnitude;

                if (dist < bestDist)
                {
                    bestDist   = dist;
                    bestFactor = factor;
                    bestIndex  = i;
                }
            }

            MoveToSegment(bestIndex, bestFactor);
        }
Example #2
0
        public void MoveToLocallyClosestPoint(Vector3 point, bool allowForwards = true, bool allowBackwards = true)
        {
            if (path == null)
            {
                return;
            }

            while (allowForwards && segmentIndex < path.Count - 2 && (path[segmentIndex + 1] - point).sqrMagnitude <= (path[segmentIndex] - point).sqrMagnitude)
            {
                NextSegment();
            }

            while (allowBackwards && segmentIndex > 0 && (path[segmentIndex - 1] - point).sqrMagnitude <= (path[segmentIndex] - point).sqrMagnitude)
            {
                PrevSegment();
            }

            // Check the distances to the two segments extending from the vertex path[segmentIndex]
            // and pick the position on those segments that is closest to the #point parameter.
            float factor1 = 0, factor2 = 0, d1 = float.PositiveInfinity, d2 = float.PositiveInfinity;

            if (segmentIndex > 0)
            {
                factor1 = VectorMath.ClosestPointOnLineFactor(path[segmentIndex - 1], path[segmentIndex], point);
                d1      = (Vector3.Lerp(path[segmentIndex - 1], path[segmentIndex], factor1) - point).sqrMagnitude;
            }

            if (segmentIndex < path.Count - 1)
            {
                factor2 = VectorMath.ClosestPointOnLineFactor(path[segmentIndex], path[segmentIndex + 1], point);
                d2      = (Vector3.Lerp(path[segmentIndex], path[segmentIndex + 1], factor2) - point).sqrMagnitude;
            }

            if (d1 < d2)
            {
                MoveToSegment(segmentIndex - 1, factor1);
            }
            else
            {
                MoveToSegment(segmentIndex, factor2);
            }
        }
Example #3
0
 public static VECTOR Lerp(VECTOR a, VECTOR b, FLOAT amount)
 {
     return(VECTOR.Lerp(a, b, amount));
 }