diff --git a/osu.Game.Rulesets.Osu/Objects/Slider.cs b/osu.Game.Rulesets.Osu/Objects/Slider.cs index 203e829180..cc3ffd376e 100644 --- a/osu.Game.Rulesets.Osu/Objects/Slider.cs +++ b/osu.Game.Rulesets.Osu/Objects/Slider.cs @@ -41,7 +41,7 @@ namespace osu.Game.Rulesets.Osu.Objects public Vector2 StackedPositionAt(double t) => StackedPosition + this.CurvePositionAt(t); - private readonly SliderPath path = new SliderPath(); + private readonly SliderPath path = new SliderPath { OptimiseCatmull = true }; public SliderPath Path { diff --git a/osu.Game/Rulesets/Objects/SliderPath.cs b/osu.Game/Rulesets/Objects/SliderPath.cs index f33a07f082..e8e769e3fa 100644 --- a/osu.Game/Rulesets/Objects/SliderPath.cs +++ b/osu.Game/Rulesets/Objects/SliderPath.cs @@ -42,6 +42,17 @@ namespace osu.Game.Rulesets.Objects private readonly List cumulativeLength = new List(); private readonly Cached pathCache = new Cached(); + /// + /// Any additional length of the path which was optimised out during piecewise approximation, but should still be considered as part of . + /// + /// + /// This is a hack for Catmull paths. + /// + private double optimisedLength; + + /// + /// The final calculated length of the path. + /// private double calculatedLength; private readonly List segmentEnds = new List(); @@ -123,6 +134,24 @@ namespace osu.Game.Rulesets.Objects } } + private bool optimiseCatmull; + + /// + /// Whether to optimise Catmull path segments, usually resulting in removing bulbs around stacked knots. + /// + /// + /// This changes the path shape and should therefore not be used. + /// + public bool OptimiseCatmull + { + get => optimiseCatmull; + set + { + optimiseCatmull = value; + invalidate(); + } + } + /// /// Computes the slider path until a given progress that ranges from 0 (beginning of the slider) /// to 1 (end of the slider) and stores the generated path in the given list. @@ -244,6 +273,7 @@ namespace osu.Game.Rulesets.Objects { calculatedPath.Clear(); segmentEnds.Clear(); + optimisedLength = 0; if (ControlPoints.Count == 0) return; @@ -269,6 +299,7 @@ namespace osu.Game.Rulesets.Objects else if (segmentVertices.Length > 1) { List subPath = calculateSubPath(segmentVertices, segmentType); + // Skip the first vertex if it is the same as the last vertex from the previous segment bool skipFirst = calculatedPath.Count > 0 && subPath.Count > 0 && calculatedPath.Last() == subPath[0]; @@ -295,6 +326,7 @@ namespace osu.Game.Rulesets.Objects return PathApproximator.LinearToPiecewiseLinear(subControlPoints); case SplineType.PerfectCurve: + { if (subControlPoints.Length != 3) break; @@ -305,9 +337,58 @@ namespace osu.Game.Rulesets.Objects break; return subPath; + } case SplineType.Catmull: - return PathApproximator.CatmullToPiecewiseLinear(subControlPoints); + { + List subPath = PathApproximator.CatmullToPiecewiseLinear(subControlPoints); + + if (!OptimiseCatmull) + return subPath; + + // At draw time, osu!stable optimises paths by only keeping piecewise segments that are 6px apart. + // For the most part we don't care about this optimisation, and its additional heuristics are hard to reproduce in every implementation. + // + // However, it matters for Catmull paths which form "bulbs" around sequential knots with identical positions, + // so we'll apply a very basic form of the optimisation here and return a length representing the optimised portion. + // The returned length is important so that the optimisation doesn't cause the path to get extended to match the value of ExpectedDistance. + + List optimisedPath = new List(subPath.Count); + + Vector2? lastStart = null; + double lengthRemovedSinceStart = 0; + + for (int i = 0; i < subPath.Count; i++) + { + if (lastStart == null) + { + optimisedPath.Add(subPath[i]); + lastStart = subPath[i]; + continue; + } + + Debug.Assert(i > 0); + + double distFromStart = Vector2.Distance(lastStart.Value, subPath[i]); + lengthRemovedSinceStart += Vector2.Distance(subPath[i - 1], subPath[i]); + + // See PathApproximator.catmull_detail. + const int catmull_detail = 50; + const int catmull_segment_length = catmull_detail * 2; + + // Either 6px from the start, the last vertex at every knot, or the end of the path. + if (distFromStart > 6 || (i + 1) % catmull_segment_length == 0 || i == subPath.Count - 1) + { + optimisedPath.Add(subPath[i]); + optimisedLength += lengthRemovedSinceStart - distFromStart; + + lastStart = null; + lengthRemovedSinceStart = 0; + } + } + + return optimisedPath; + } } return PathApproximator.BSplineToPiecewiseLinear(subControlPoints, type.Degree ?? subControlPoints.Length); @@ -315,7 +396,7 @@ namespace osu.Game.Rulesets.Objects private void calculateLength() { - calculatedLength = 0; + calculatedLength = optimisedLength; cumulativeLength.Clear(); cumulativeLength.Add(0);