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); int p = (int)(time / _deltaT); // use a circular indexing system int p0 = p - 1; if (p0 < 0) { p0 = p0 + (ControlPoints.Count - 1); } else if (p0 >= ControlPoints.Count - 1) { p0 = p0 - (ControlPoints.Count - 1); } int p1 = p; if (p1 < 0) { p1 = p1 + (ControlPoints.Count - 1); } else if (p1 >= ControlPoints.Count - 1) { p1 = p1 - (ControlPoints.Count - 1); } int p2 = p + 1; if (p2 < 0) { p2 = p2 + (ControlPoints.Count - 1); } else if (p2 >= ControlPoints.Count - 1) { p2 = p2 - (ControlPoints.Count - 1); } int p3 = p + 2; if (p3 < 0) { p3 = p3 + (ControlPoints.Count - 1); } else if (p3 >= ControlPoints.Count - 1) { p3 = p3 - (ControlPoints.Count - 1); } // relative time float lt = (time - _deltaT * p) / _deltaT; temp = VectorMath.CatmullRom(ControlPoints[p0], ControlPoints[p1], ControlPoints[p2], ControlPoints[p3], lt); RemoveAt(ControlPoints.Count - 1); } else { int p = (int)(time / _deltaT); // int p0 = p - 1; if (p0 < 0) { p0 = 0; } else if (p0 >= ControlPoints.Count - 1) { p0 = ControlPoints.Count - 1; } int p1 = p; if (p1 < 0) { p1 = 0; } else if (p1 >= ControlPoints.Count - 1) { p1 = ControlPoints.Count - 1; } int p2 = p + 1; if (p2 < 0) { p2 = 0; } else if (p2 >= ControlPoints.Count - 1) { p2 = ControlPoints.Count - 1; } int p3 = p + 2; if (p3 < 0) { p3 = 0; } else if (p3 >= ControlPoints.Count - 1) { p3 = ControlPoints.Count - 1; } // relative time float lt = (time - _deltaT * p) / _deltaT; temp = VectorMath.CatmullRom(ControlPoints[p0], ControlPoints[p1], ControlPoints[p2], ControlPoints[p3], lt); } return(temp); }