// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Linq; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Game.Rulesets.Osu.UI; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Osu.Beatmaps; using osu.Game.Rulesets.Osu.Objects; using osuTK; namespace osu.Game.Rulesets.Osu.Utils { public static partial class OsuHitObjectGenerationUtils { // The relative distance to the edge of the playfield before objects' positions should start to "turn around" and curve towards the middle. // The closer the hit objects draw to the border, the sharper the turn private const float playfield_edge_ratio = 0.375f; private static readonly float border_distance_x = OsuPlayfield.BASE_SIZE.X * playfield_edge_ratio; private static readonly float border_distance_y = OsuPlayfield.BASE_SIZE.Y * playfield_edge_ratio; private static readonly Vector2 playfield_middle = OsuPlayfield.BASE_SIZE / 2; /// /// Rotate a hit object away from the playfield edge, while keeping a constant distance /// from the previous object. /// /// /// The extent of rotation depends on the position of the hit object. Hit objects /// closer to the playfield edge will be rotated to a larger extent. /// /// Position of the previous hit object. /// Position of the hit object to be rotated, relative to the previous hit object. /// /// The extent of rotation. /// 0 means the hit object is never rotated. /// 1 means the hit object will be fully rotated towards playfield center when it is originally at playfield edge. /// /// The new position of the hit object, relative to the previous one. public static Vector2 RotateAwayFromEdge(Vector2 prevObjectPos, Vector2 posRelativeToPrev, float rotationRatio = 0.5f) { float relativeRotationDistance = 0f; if (prevObjectPos.X < playfield_middle.X) { relativeRotationDistance = Math.Max( (border_distance_x - prevObjectPos.X) / border_distance_x, relativeRotationDistance ); } else { relativeRotationDistance = Math.Max( (prevObjectPos.X - (OsuPlayfield.BASE_SIZE.X - border_distance_x)) / border_distance_x, relativeRotationDistance ); } if (prevObjectPos.Y < playfield_middle.Y) { relativeRotationDistance = Math.Max( (border_distance_y - prevObjectPos.Y) / border_distance_y, relativeRotationDistance ); } else { relativeRotationDistance = Math.Max( (prevObjectPos.Y - (OsuPlayfield.BASE_SIZE.Y - border_distance_y)) / border_distance_y, relativeRotationDistance ); } return RotateVectorTowardsVector( posRelativeToPrev, playfield_middle - prevObjectPos, Math.Min(1, relativeRotationDistance * rotationRatio) ); } /// /// Rotates vector "initial" towards vector "destination". /// /// The vector to be rotated. /// The vector that "initial" should be rotated towards. /// How much "initial" should be rotated. 0 means no rotation. 1 means "initial" is fully rotated to equal "destination". /// The rotated vector. public static Vector2 RotateVectorTowardsVector(Vector2 initial, Vector2 destination, float rotationRatio) { float initialAngleRad = MathF.Atan2(initial.Y, initial.X); float destAngleRad = MathF.Atan2(destination.Y, destination.X); float diff = destAngleRad - initialAngleRad; while (diff < -MathF.PI) diff += 2 * MathF.PI; while (diff > MathF.PI) diff -= 2 * MathF.PI; float finalAngleRad = initialAngleRad + rotationRatio * diff; return new Vector2( initial.Length * MathF.Cos(finalAngleRad), initial.Length * MathF.Sin(finalAngleRad) ); } /// /// Reflects the position of the in the playfield horizontally. /// /// The object to reflect. public static void ReflectHorizontally(OsuHitObject osuObject) { osuObject.Position = new Vector2(OsuPlayfield.BASE_SIZE.X - osuObject.X, osuObject.Position.Y); if (!(osuObject is Slider slider)) return; // No need to update the head and tail circles, since slider handles that when the new slider path is set slider.NestedHitObjects.OfType().ForEach(h => h.Position = new Vector2(OsuPlayfield.BASE_SIZE.X - h.Position.X, h.Position.Y)); slider.NestedHitObjects.OfType().ForEach(h => h.Position = new Vector2(OsuPlayfield.BASE_SIZE.X - h.Position.X, h.Position.Y)); var controlPoints = slider.Path.ControlPoints.Select(p => new PathControlPoint(p.Position, p.Type)).ToArray(); foreach (var point in controlPoints) point.Position = new Vector2(-point.Position.X, point.Position.Y); slider.Path = new SliderPath(controlPoints, slider.Path.ExpectedDistance.Value); } /// /// Reflects the position of the in the playfield vertically. /// /// The object to reflect. public static void ReflectVertically(OsuHitObject osuObject) { osuObject.Position = new Vector2(osuObject.Position.X, OsuPlayfield.BASE_SIZE.Y - osuObject.Y); if (!(osuObject is Slider slider)) return; // No need to update the head and tail circles, since slider handles that when the new slider path is set slider.NestedHitObjects.OfType().ForEach(h => h.Position = new Vector2(h.Position.X, OsuPlayfield.BASE_SIZE.Y - h.Position.Y)); slider.NestedHitObjects.OfType().ForEach(h => h.Position = new Vector2(h.Position.X, OsuPlayfield.BASE_SIZE.Y - h.Position.Y)); var controlPoints = slider.Path.ControlPoints.Select(p => new PathControlPoint(p.Position, p.Type)).ToArray(); foreach (var point in controlPoints) point.Position = new Vector2(point.Position.X, -point.Position.Y); slider.Path = new SliderPath(controlPoints, slider.Path.ExpectedDistance.Value); } /// /// Rotate a slider about its start position by the specified angle. /// /// The slider to be rotated. /// The angle, measured in radians, to rotate the slider by. public static void RotateSlider(Slider slider, float rotation) { void rotateNestedObject(OsuHitObject nested) => nested.Position = rotateVector(nested.Position - slider.Position, rotation) + slider.Position; // No need to update the head and tail circles, since slider handles that when the new slider path is set slider.NestedHitObjects.OfType().ForEach(rotateNestedObject); slider.NestedHitObjects.OfType().ForEach(rotateNestedObject); var controlPoints = slider.Path.ControlPoints.Select(p => new PathControlPoint(p.Position, p.Type)).ToArray(); foreach (var point in controlPoints) point.Position = rotateVector(point.Position, rotation); slider.Path = new SliderPath(controlPoints, slider.Path.ExpectedDistance.Value); } /// /// Rotate a vector by the specified angle. /// /// The vector to be rotated. /// The angle, measured in radians, to rotate the vector by. /// The rotated vector. private static Vector2 rotateVector(Vector2 vector, float rotation) { float angle = MathF.Atan2(vector.Y, vector.X) + rotation; float length = vector.Length; return new Vector2( length * MathF.Cos(angle), length * MathF.Sin(angle) ); } public static bool IsHitObjectOnBeat(OsuBeatmap beatmap, OsuHitObject hitObject, bool downbeatsOnly = false) { var timingPoints = beatmap.ControlPointInfo.TimingPoints; var currentTimingPoint = timingPoints.Reverse().FirstOrDefault(p => p.Time <= hitObject.StartTime); if (currentTimingPoint == null) return false; double timeSinceTimingPoint = hitObject.StartTime - currentTimingPoint.Time; double length = downbeatsOnly ? currentTimingPoint.BeatLength * currentTimingPoint.TimeSignature.Numerator : currentTimingPoint.BeatLength; return (timeSinceTimingPoint + 1) % length < 2; } public static float GetRelativeTargetAngle(float targetDistance, float offset, bool flowDirection) { float angle = (float)(3.3 / (1 + 200 * Math.Pow(MathHelper.E, 0.016 * (targetDistance - 466))) + 0.45 + offset); float relativeAngle = MathHelper.Pi - angle; return flowDirection ? -relativeAngle : relativeAngle; } public static float RandomGaussian(Random rng, float mean = 0, float stdDev = 1) { double x1 = 1 - rng.NextDouble(); double x2 = 1 - rng.NextDouble(); double stdNormal = Math.Sqrt(-2 * Math.Log(x1)) * Math.Sin(MathHelper.TwoPi * x2); return mean + stdDev * (float)stdNormal; } } }