private void AddDistanceTime(float distance, float time, Vector2 point) { DistanceTime dt = new DistanceTime(); dt.distance = distance; dt.t = time; dt.point = point; this.CurveSnapshots.Add(dt); }
public Vector2 PositionAtDistance(float d) { // because interpolation is not linearly correlated to distance along the curve this // method exists to get the PositionAtDistance // // binary search to find the DistanceTime struct that is close enough to our distance that // we want to find int high = this.CurveSnapshots.Count - 1; int low = 0; while (low <= high) { int mid = (high + low) / 2; if (mid == high || mid == low) { if (mid + 1 >= this.CurveSnapshots.Count) { return(this.CurveSnapshots[mid].point); } else { DistanceTime a = this.CurveSnapshots[mid]; DistanceTime b = this.CurveSnapshots[mid + 1]; // interpolate betweeen the two snapshots return(this.Lerp(a.point, b.point, (d - a.distance) / (b.distance - a.distance))); } } if (this.CurveSnapshots[mid].distance > d) { high = mid; } else { low = mid; } } return(Vector2.Zero); }