Beispiel #1
0
        public Vector2 GetPosition(float time)
        {
            Vector2 temp;

            if (ControlPoints.Count < 2)
            {
                throw new Exception("You need at least 2 control points to calculate a position.");
            }

            if (Closed)
            {
                Add(ControlPoints[0]);

                _deltaT = 1f / (ControlPoints.Count - 1);

                var p = (int)(time / _deltaT);

                // use a circular indexing system
                var p0 = p - 1;
                if (p0 < 0)
                {
                    p0 = p0 + (ControlPoints.Count - 1);
                }
                else if (p0 >= ControlPoints.Count - 1)
                {
                    p0 = p0 - (ControlPoints.Count - 1);
                }
                var p1 = p;
                if (p1 < 0)
                {
                    p1 = p1 + (ControlPoints.Count - 1);
                }
                else if (p1 >= ControlPoints.Count - 1)
                {
                    p1 = p1 - (ControlPoints.Count - 1);
                }
                var p2 = p + 1;
                if (p2 < 0)
                {
                    p2 = p2 + (ControlPoints.Count - 1);
                }
                else if (p2 >= ControlPoints.Count - 1)
                {
                    p2 = p2 - (ControlPoints.Count - 1);
                }
                var p3 = p + 2;
                if (p3 < 0)
                {
                    p3 = p3 + (ControlPoints.Count - 1);
                }
                else if (p3 >= ControlPoints.Count - 1)
                {
                    p3 = p3 - (ControlPoints.Count - 1);
                }

                // relative time
                var lt = (time - _deltaT * p) / _deltaT;

                temp = VectorUtils.CatmullRom(ControlPoints[p0], ControlPoints[p1], ControlPoints[p2],
                                              ControlPoints[p3], lt);

                RemoveAt(ControlPoints.Count - 1);
            }
            else
            {
                var p = (int)(time / _deltaT);

                //
                var p0 = p - 1;
                if (p0 < 0)
                {
                    p0 = 0;
                }
                else if (p0 >= ControlPoints.Count - 1)
                {
                    p0 = ControlPoints.Count - 1;
                }
                var p1 = p;
                if (p1 < 0)
                {
                    p1 = 0;
                }
                else if (p1 >= ControlPoints.Count - 1)
                {
                    p1 = ControlPoints.Count - 1;
                }
                var p2 = p + 1;
                if (p2 < 0)
                {
                    p2 = 0;
                }
                else if (p2 >= ControlPoints.Count - 1)
                {
                    p2 = ControlPoints.Count - 1;
                }
                var p3 = p + 2;
                if (p3 < 0)
                {
                    p3 = 0;
                }
                else if (p3 >= ControlPoints.Count - 1)
                {
                    p3 = ControlPoints.Count - 1;
                }

                // relative time
                var lt = (time - _deltaT * p) / _deltaT;

                temp = VectorUtils.CatmullRom(ControlPoints[p0], ControlPoints[p1], ControlPoints[p2],
                                              ControlPoints[p3], lt);
            }

            return(temp);
        }