1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-30 23:57:25 +08:00
osu-lazer/osu.Game/Rulesets/UI/Scrolling/Algorithms/SequentialScrollAlgorithm.cs

165 lines
7.5 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Timing;
2018-11-02 18:52:40 +08:00
namespace osu.Game.Rulesets.UI.Scrolling.Algorithms
2018-04-13 17:19:50 +08:00
{
public class SequentialScrollAlgorithm : IScrollAlgorithm
2018-04-13 17:19:50 +08:00
{
private static readonly IComparer<PositionMapping> by_position_comparer = Comparer<PositionMapping>.Create((c1, c2) => c1.Position.CompareTo(c2.Position));
2018-04-13 17:19:50 +08:00
private readonly IReadOnlyList<MultiplierControlPoint> controlPoints;
/// <summary>
/// Stores a mapping of time -> position for each control point.
/// </summary>
private readonly List<PositionMapping> positionMappings = new List<PositionMapping>();
public SequentialScrollAlgorithm(IReadOnlyList<MultiplierControlPoint> controlPoints)
2018-04-13 17:19:50 +08:00
{
this.controlPoints = controlPoints;
}
Fix lifetime calculation in overlapping algorithm Changes to lifetime calculation in scrolling rulesets introduced in #7367, which aimed to account for the distance between hit objects' origin and its edge entering the scrolling area, fixed some issues with hitobjects appearing abruptly, but also regressed some other scenarios. Upon investigation, the regression was localised to the overlapping scroll algorithm. The reason for this was two-fold: * The previous code used TimeAt() to calculate the time of travel from the hit object's edge to its origin. For other algorithms, that time can be accurately reconstructed, because they don't have periods of time where there are multiple hit objects scrolling at different velocities. That invariant does not hold for the overlapping algorithm, therefore it is possible for different values to be technically correct for TimeAt(). However, the only value that matters for the adjustment is the one that's indicated by the control point that applies to the hit object origin, which can be uniquely identified. * Additionally, the offset returned (even if correct) was applied externally to the hit object's start time and passed to GetDisplayStartTime(). In the overlapping algorithm, the choice of control point used in GetDisplayStartTime() is important, since the value of the speed multiplier is read within. Externally rewinding the hit object's start time meant that in some cases the speed multiplier of the *previous* control point is applied, which led to hit objects appearing too late if the scrolling rate decreased. Because of the above, modify GetDisplayStartTime() to take the offset into account in all algorithms, and apply the adjustment correctly inside of them. The constant and sequential algorithms needed no adjustment from the previous logic, since: * the constant algorithm disregarded control points, and * the sequential algorithm would effectively rewind to time = 0, calculate the absolute distance from time = 0 to the hit object start, apply the origin offset *to the absolute distance*, and then convert back to time, applying all control points in sequence. Due to this it was impossible for control points to get mixed up while calculating. As for the overlapping algorithm, the high-level logic is as follows: * The distance that the origin has to travel is the length of the scroll plus the distance from the origin to the object edge. * The above distance divided by the scroll length gives the relative scroll lengths that the object has to travel. * As one relative scroll length takes one time range, the relative travel length multiplied by the time range gives the absolute travel time of the object origin. * Finally, the control point multiplier applicable at origin time is applied to the whole travel time. Correctness of the above is demonstrated by visual tests added before and headless unit tests of the algorithms themselves. The sequential scroll algorithm was not covered by unit tests, and remains uncovered due to floating-point inaccuracies that should be addressed separately.
2020-02-07 05:46:31 +08:00
public double GetDisplayStartTime(double originTime, float offset, double timeRange, float scrollLength)
{
return TimeAt(-(scrollLength + offset), originTime, timeRange, scrollLength);
Fix lifetime calculation in overlapping algorithm Changes to lifetime calculation in scrolling rulesets introduced in #7367, which aimed to account for the distance between hit objects' origin and its edge entering the scrolling area, fixed some issues with hitobjects appearing abruptly, but also regressed some other scenarios. Upon investigation, the regression was localised to the overlapping scroll algorithm. The reason for this was two-fold: * The previous code used TimeAt() to calculate the time of travel from the hit object's edge to its origin. For other algorithms, that time can be accurately reconstructed, because they don't have periods of time where there are multiple hit objects scrolling at different velocities. That invariant does not hold for the overlapping algorithm, therefore it is possible for different values to be technically correct for TimeAt(). However, the only value that matters for the adjustment is the one that's indicated by the control point that applies to the hit object origin, which can be uniquely identified. * Additionally, the offset returned (even if correct) was applied externally to the hit object's start time and passed to GetDisplayStartTime(). In the overlapping algorithm, the choice of control point used in GetDisplayStartTime() is important, since the value of the speed multiplier is read within. Externally rewinding the hit object's start time meant that in some cases the speed multiplier of the *previous* control point is applied, which led to hit objects appearing too late if the scrolling rate decreased. Because of the above, modify GetDisplayStartTime() to take the offset into account in all algorithms, and apply the adjustment correctly inside of them. The constant and sequential algorithms needed no adjustment from the previous logic, since: * the constant algorithm disregarded control points, and * the sequential algorithm would effectively rewind to time = 0, calculate the absolute distance from time = 0 to the hit object start, apply the origin offset *to the absolute distance*, and then convert back to time, applying all control points in sequence. Due to this it was impossible for control points to get mixed up while calculating. As for the overlapping algorithm, the high-level logic is as follows: * The distance that the origin has to travel is the length of the scroll plus the distance from the origin to the object edge. * The above distance divided by the scroll length gives the relative scroll lengths that the object has to travel. * As one relative scroll length takes one time range, the relative travel length multiplied by the time range gives the absolute travel time of the object origin. * Finally, the control point multiplier applicable at origin time is applied to the whole travel time. Correctness of the above is demonstrated by visual tests added before and headless unit tests of the algorithms themselves. The sequential scroll algorithm was not covered by unit tests, and remains uncovered due to floating-point inaccuracies that should be addressed separately.
2020-02-07 05:46:31 +08:00
}
public float GetLength(double startTime, double endTime, double timeRange, float scrollLength)
{
var objectLength = relativePositionAt(endTime, timeRange) - relativePositionAt(startTime, timeRange);
return (float)(objectLength * scrollLength);
}
public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
{
double timelineLength = relativePositionAt(time, timeRange) - relativePositionAt(currentTime, timeRange);
return (float)(timelineLength * scrollLength);
}
2018-11-09 18:55:48 +08:00
public double TimeAt(float position, double currentTime, double timeRange, float scrollLength)
{
if (controlPoints.Count == 0)
return position * timeRange;
2018-11-09 18:55:48 +08:00
// Find the position at the current time, and the given length.
double relativePosition = relativePositionAt(currentTime, timeRange) + position / scrollLength;
2018-11-09 18:55:48 +08:00
var positionMapping = findControlPointMapping(timeRange, new PositionMapping(0, null, relativePosition), by_position_comparer);
2018-11-09 18:55:48 +08:00
// Begin at the control point's time and add the remaining time to reach the given position.
return positionMapping.Time + (relativePosition - positionMapping.Position) * timeRange / positionMapping.ControlPoint.Multiplier;
}
public void Reset() => positionMappings.Clear();
/// <summary>
/// Finds the position which corresponds to a point in time.
/// This is a non-linear operation that depends on all the control points up to and including the one active at the time value.
/// </summary>
/// <param name="time">The time to find the position at.</param>
/// <param name="timeRange">The amount of time visualised by the scrolling area.</param>
/// <returns>A positive value indicating the position at <paramref name="time"/>.</returns>
private double relativePositionAt(in double time, in double timeRange)
2018-04-13 17:19:50 +08:00
{
if (controlPoints.Count == 0)
return time / timeRange;
var mapping = findControlPointMapping(timeRange, new PositionMapping(time));
// Begin at the control point's position and add the remaining distance to reach the given time.
return mapping.Position + (time - mapping.Time) / timeRange * mapping.ControlPoint.Multiplier;
}
/// <summary>
/// Finds a <see cref="MultiplierControlPoint"/>'s <see cref="PositionMapping"/> that is relevant to a given <see cref="PositionMapping"/>.
/// </summary>
/// <remarks>
/// This is used to find the last <see cref="MultiplierControlPoint"/> occuring prior to a time value, or prior to a position value (if <see cref="by_position_comparer"/> is used).
/// </remarks>
/// <param name="timeRange">The time range.</param>
/// <param name="search">The <see cref="PositionMapping"/> to find the closest <see cref="PositionMapping"/> to.</param>
/// <param name="comparer">The comparison. If null, the default comparer is used (by time).</param>
/// <returns>The <see cref="MultiplierControlPoint"/>'s <see cref="PositionMapping"/> that is relevant for <paramref name="search"/>.</returns>
private PositionMapping findControlPointMapping(in double timeRange, in PositionMapping search, IComparer<PositionMapping> comparer = null)
{
generatePositionMappings(timeRange);
var mappingIndex = positionMappings.BinarySearch(search, comparer ?? Comparer<PositionMapping>.Default);
if (mappingIndex < 0)
2018-04-13 17:19:50 +08:00
{
// If the search value isn't found, the _next_ control point is returned, but we actually want the _previous_ control point.
// In doing so, we must make sure to not underflow the position mapping list (i.e. always use the 0th control point for time < first_control_point_time).
mappingIndex = Math.Max(0, ~mappingIndex - 1);
Debug.Assert(mappingIndex < positionMappings.Count);
}
var mapping = positionMappings[mappingIndex];
Debug.Assert(mapping.ControlPoint != null);
return mapping;
}
/// <summary>
/// Generates the mapping of <see cref="MultiplierControlPoint"/> (and their respective start times) to their relative position from 0.
/// </summary>
/// <param name="timeRange">The time range.</param>
private void generatePositionMappings(in double timeRange)
{
if (positionMappings.Count > 0)
return;
2018-04-13 17:19:50 +08:00
if (controlPoints.Count == 0)
return;
2018-04-13 17:19:50 +08:00
positionMappings.Add(new PositionMapping(controlPoints[0].StartTime, controlPoints[0]));
2018-04-13 17:19:50 +08:00
for (int i = 0; i < controlPoints.Count - 1; i++)
{
var current = controlPoints[i];
var next = controlPoints[i + 1];
// Figure out how much of the time range the duration represents, and adjust it by the speed multiplier
float length = (float)((next.StartTime - current.StartTime) / timeRange * current.Multiplier);
positionMappings.Add(new PositionMapping(next.StartTime, next, positionMappings[^1].Position + length));
}
}
private readonly struct PositionMapping : IComparable<PositionMapping>
{
/// <summary>
/// The time corresponding to this position.
/// </summary>
public readonly double Time;
/// <summary>
/// The <see cref="MultiplierControlPoint"/> at <see cref="Time"/>.
/// </summary>
[CanBeNull]
public readonly MultiplierControlPoint ControlPoint;
/// <summary>
/// The relative position from 0 of <see cref="ControlPoint"/>.
/// </summary>
public readonly double Position;
public PositionMapping(double time, MultiplierControlPoint controlPoint = null, double position = default)
{
Time = time;
ControlPoint = controlPoint;
Position = position;
2018-04-13 17:19:50 +08:00
}
public int CompareTo(PositionMapping other) => Time.CompareTo(other.Time);
2018-04-13 17:19:50 +08:00
}
}
}