コード例 #1
0
        private float GetTime(Vector2 a, Vector2 b, bool isPath = true)
        {
            float speed = GetSpeed(isPath);
            float dist  = CalcF.Distance(a, b);

            return(dist / speed);
        }
コード例 #2
0
        public float DistanceFrom(Vector2 p)
        {
            float fromOrigin = CalcF.Distance(Origin, p);

            if (fromOrigin >= Radius)
            {
                fromOrigin -= Radius;
            }

            return(fromOrigin);
        }
コード例 #3
0
        private float GetTotalDistance()
        {
            float   dist = 0.0f;
            Vector2 from = From;

            foreach (Vector2 to in Points)
            {
                dist += CalcF.Distance(from, to);
                from  = to;
            }

            dist += CalcF.Distance(from, To);
            return(dist);
        }
コード例 #4
0
        // gets the distance travelled for the specified index of the point.
        public float GetDistanceAt(int index)
        {
            float   dist = 0.0f;
            Vector2 from = From;
            int     i    = 0;

            foreach (Vector2 to in Points)
            {
                if (index == i)
                {
                    break;
                }


                dist += CalcF.Distance(from, to);
                from  = to;
                i++;
            }

            return(dist);
        }
コード例 #5
0
        // this gets the index of the closest point based on a current position.
        private int ClosestPointAt(Vector2 pos)
        {
            float dist  = 0.0f;
            int   index = 0;

            for (int i = 0; i < Points.Count; i++)
            {
                float amt = CalcF.Distance(pos, Points[i]);

                if (amt < dist)
                {
                    dist  = amt;
                    index = i;
                }
                else if (amt == 0)
                {
                    return(i);
                }
            }

            return(index);
        }
コード例 #6
0
 public float DistanceFromOrigin(Vector2 p)
 => CalcF.Distance(Origin, p);
コード例 #7
0
        // gets the desired point based on the completion of this route.
        private Vector2 GetPositionAt(float progress)
        {
            // converts the progress into the total distance travelled based on progress.
            float travelled = RangeF.Convert(0.0f, 1.0f, 0.0f, GetTotalDistance(), progress);

            float   dist = 0.0f;
            Vector2 from = From;
            int     i    = 0;

            // TODO: Create a sub-list referencing Points, and simply insert the From/To values
            foreach (Vector2 to in Points)
            {
                dist += CalcF.Distance(from, to);
                from  = to;

                if (dist >= travelled)
                {
                    break;
                }

                i++;
            }

            if (Points.Count == 0) // if there aren't any points specified
            {
                float pathProgress = RangeF.Convert(0.0f, GetTotalDistance(), 0.0f, 1.0f, travelled);

                return(new Vector2(RangeF.Convert(0.0f, 1.0f, From.X, To.X, pathProgress),
                                   RangeF.Convert(0.0f, 1.0f, From.Y, To.Y, pathProgress)));
            }
            else if (i == 0) // if the total travelled was less than the first checkpoint.
            {
                float pathProgress = RangeF.Convert(0.0f, GetDistanceAt(i), 0.0f, 1.0f, travelled);

                return(new Vector2(RangeF.Convert(0.0f, 1.0f, From.X, Points[i].X, pathProgress),
                                   RangeF.Convert(0.0f, 1.0f, From.Y, Points[i].Y, pathProgress)));
            }
            else if (dist < travelled) // if the total travelled is greater than all points
            {
                dist += CalcF.Distance(from, To);

                if (travelled >= dist)
                {
                    return(To);
                }
                else
                {
                    float pathProgress = RangeF.Convert(GetDistanceAt(i), GetTotalDistance(), 0.0f, 1.0f, travelled);

                    return(new Vector2(RangeF.Convert(0.0f, 1.0f, Points[i].X, To.X, pathProgress),
                                       RangeF.Convert(0.0f, 1.0f, Points[i].Y, To.Y, pathProgress)));
                }
            }
            else // if the travelled amount if within the points specified.
            {
                float pathProgress = RangeF.Convert(GetDistanceAt(i - 1), GetDistanceAt(i), 0.0f, 1.0f, travelled);

                return(new Vector2(RangeF.Convert(0.0f, 1.0f, Points[i - 1].X, Points[i].X, pathProgress),
                                   RangeF.Convert(0.0f, 1.0f, Points[i - 1].Y, Points[i].Y, pathProgress)));
            }
        }