From 96e09605d8fabe836396d2cb92d0deabaacb93a2 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Tue, 29 Jun 2021 12:33:40 +0800 Subject: [PATCH 01/17] Osu random mod improvements - Reduce "jump streams" by increasing maximum jump angle and variance in jump angle - Reduce weird jumps to sliders by shifting hit circles in front of sliders --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 66 ++++++++++++++++++++-- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index d1212096bf..d3a7f4fc74 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -26,6 +26,11 @@ namespace osu.Game.Rulesets.Osu.Mods private static readonly float playfield_diagonal = OsuPlayfield.BASE_SIZE.LengthFast; + /// + /// Number of previous hit circles to be shifted together when a slider needs to be moved. + /// + private const int shift_object_count = 10; + private Random rng; public void ApplyToBeatmap(IBeatmap beatmap) @@ -43,14 +48,22 @@ namespace osu.Game.Rulesets.Osu.Mods float rateOfChangeMultiplier = 0; + int cntSinceNewCombo = 0; + for (int i = 0; i < hitObjects.Count; i++) { var hitObject = hitObjects[i]; var current = new RandomObjectInfo(hitObject); - // rateOfChangeMultiplier only changes every i iterations to prevent shaky-line-shaped streams - if (i % 3 == 0) + // rateOfChangeMultiplier only changes every 5 iterations in a combo + // to prevent shaky-line-shaped streams + if (hitObject.NewCombo) + cntSinceNewCombo = 0; + else + cntSinceNewCombo++; + + if (cntSinceNewCombo % 5 == 0) rateOfChangeMultiplier = (float)rng.NextDouble() * 2 - 1; if (hitObject is Spinner) @@ -67,7 +80,24 @@ namespace osu.Game.Rulesets.Osu.Mods current.EndPositionRandomised = current.PositionRandomised; if (hitObject is Slider slider) - moveSliderIntoPlayfield(slider, current); + { + Vector2 shift = moveSliderIntoPlayfield(slider, current); + + if (shift != Vector2.Zero) + { + var toBeShifted = new List(); + + for (int j = i - 1; j >= i - shift_object_count && j >= 0; j--) + { + if (!(hitObjects[j] is HitCircle)) break; + + toBeShifted.Add(hitObjects[j]); + } + + if (toBeShifted.Count > 0) + applyDecreasingShift(toBeShifted, shift); + } + } previous = current; } @@ -94,7 +124,9 @@ namespace osu.Game.Rulesets.Osu.Mods // The max. angle (relative to the angle of the vector pointing from the 2nd last to the last hit object) // is proportional to the distance between the last and the current hit object // to allow jumps and prevent too sharp turns during streams. - var randomAngleRad = rateOfChangeMultiplier * 2 * Math.PI * distanceToPrev / playfield_diagonal; + + // Allow maximum jump angle when jump distance is more than half of playfield diagonal length + var randomAngleRad = rateOfChangeMultiplier * 2 * Math.PI * Math.Min(1f, distanceToPrev / (playfield_diagonal * 0.5f)); current.AngleRad = (float)randomAngleRad + previous.AngleRad; if (current.AngleRad < 0) @@ -122,10 +154,13 @@ namespace osu.Game.Rulesets.Osu.Mods /// /// Moves the and all necessary nested s into the if they aren't already. /// - private void moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) + /// The that this slider has been shifted by. + private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) { var minMargin = getMinSliderMargin(slider); + var prevPosition = slider.Position; + slider.Position = new Vector2( Math.Clamp(slider.Position.X, minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right), Math.Clamp(slider.Position.Y, minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom) @@ -135,6 +170,27 @@ namespace osu.Game.Rulesets.Osu.Mods currentObjectInfo.EndPositionRandomised = slider.EndPosition; shiftNestedObjects(slider, currentObjectInfo.PositionRandomised - currentObjectInfo.PositionOriginal); + + return slider.Position - prevPosition; + } + + /// + /// Decreasingly shift a list of s by a specified amount. + /// The first item in the list is shifted by the largest amount, while the last item is shifted by the smallest amount. + /// + /// The list of hit objects to be shifted. + /// The amount to be shifted. + private void applyDecreasingShift(IList hitObjects, Vector2 shift) + { + for (int i = 0; i < hitObjects.Count; i++) + { + Vector2 position = hitObjects[i].Position + shift * ((hitObjects.Count - i) / (float)(hitObjects.Count + 1)); + + position.X = MathHelper.Clamp(position.X, 0, OsuPlayfield.BASE_SIZE.X); + position.Y = MathHelper.Clamp(position.Y, 0, OsuPlayfield.BASE_SIZE.Y); + + hitObjects[i].Position = position; + } } /// From 0c5777c2c82b4cd0cc1e7bbed59833c3e0d81a63 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Tue, 29 Jun 2021 12:56:05 +0800 Subject: [PATCH 02/17] Added comments --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index d3a7f4fc74..25bdac7be3 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -89,6 +89,7 @@ namespace osu.Game.Rulesets.Osu.Mods for (int j = i - 1; j >= i - shift_object_count && j >= 0; j--) { + // only shift hit circles if (!(hitObjects[j] is HitCircle)) break; toBeShifted.Add(hitObjects[j]); @@ -184,6 +185,8 @@ namespace osu.Game.Rulesets.Osu.Mods { for (int i = 0; i < hitObjects.Count; i++) { + // The first object is shifted by a vector slightly smaller than shift + // The last object is shifted by a vector slightly larger than zero Vector2 position = hitObjects[i].Position + shift * ((hitObjects.Count - i) / (float)(hitObjects.Count + 1)); position.X = MathHelper.Clamp(position.X, 0, OsuPlayfield.BASE_SIZE.X); From 2722565204b59c3b99be66ea801127863e28b3e8 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Tue, 29 Jun 2021 13:36:30 +0800 Subject: [PATCH 03/17] Take circle radius into account when clamping to playfield --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 25bdac7be3..62ca5e5fb4 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -74,6 +74,10 @@ namespace osu.Game.Rulesets.Osu.Mods applyRandomisation(rateOfChangeMultiplier, previous, current); + // Move hit objects back into the playfield if they are outside of it, + // which would sometimes happen during big jumps otherwise. + current.PositionRandomised = clampToPlayfield(current.PositionRandomised, (float)hitObject.Radius); + hitObject.Position = current.PositionRandomised; // update end position as it may have changed as a result of the position update. @@ -142,14 +146,7 @@ namespace osu.Game.Rulesets.Osu.Mods current.AngleRad = (float)Math.Atan2(posRelativeToPrev.Y, posRelativeToPrev.X); - var position = previous.EndPositionRandomised + posRelativeToPrev; - - // Move hit objects back into the playfield if they are outside of it, - // which would sometimes happen during big jumps otherwise. - position.X = MathHelper.Clamp(position.X, 0, OsuPlayfield.BASE_SIZE.X); - position.Y = MathHelper.Clamp(position.Y, 0, OsuPlayfield.BASE_SIZE.Y); - - current.PositionRandomised = position; + current.PositionRandomised = previous.EndPositionRandomised + posRelativeToPrev; } /// @@ -185,14 +182,12 @@ namespace osu.Game.Rulesets.Osu.Mods { for (int i = 0; i < hitObjects.Count; i++) { + var hitObject = hitObjects[i]; // The first object is shifted by a vector slightly smaller than shift // The last object is shifted by a vector slightly larger than zero - Vector2 position = hitObjects[i].Position + shift * ((hitObjects.Count - i) / (float)(hitObjects.Count + 1)); + Vector2 position = hitObject.Position + shift * ((hitObjects.Count - i) / (float)(hitObjects.Count + 1)); - position.X = MathHelper.Clamp(position.X, 0, OsuPlayfield.BASE_SIZE.X); - position.Y = MathHelper.Clamp(position.Y, 0, OsuPlayfield.BASE_SIZE.Y); - - hitObjects[i].Position = position; + hitObject.Position = clampToPlayfield(position, (float)hitObject.Radius); } } @@ -217,6 +212,13 @@ namespace osu.Game.Rulesets.Osu.Mods minMargin.Left = Math.Min(minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right); minMargin.Top = Math.Min(minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom); + var radius = (float)slider.Radius; + + minMargin.Left += radius; + minMargin.Right += radius; + minMargin.Top += radius; + minMargin.Bottom += radius; + return minMargin; } @@ -236,6 +238,14 @@ namespace osu.Game.Rulesets.Osu.Mods } } + private Vector2 clampToPlayfield(Vector2 position, float radius) + { + position.X = MathHelper.Clamp(position.X, radius, OsuPlayfield.BASE_SIZE.X - radius); + position.Y = MathHelper.Clamp(position.Y, radius, OsuPlayfield.BASE_SIZE.Y - radius); + + return position; + } + private class RandomObjectInfo { public float AngleRad { get; set; } From 8d1eae7c705c95f159304b262f658348ce2c84c8 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Tue, 29 Jun 2021 14:25:45 +0800 Subject: [PATCH 04/17] Use `IndexInCurrentCombo` --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 62ca5e5fb4..78a49f8e91 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -48,8 +48,6 @@ namespace osu.Game.Rulesets.Osu.Mods float rateOfChangeMultiplier = 0; - int cntSinceNewCombo = 0; - for (int i = 0; i < hitObjects.Count; i++) { var hitObject = hitObjects[i]; @@ -58,12 +56,7 @@ namespace osu.Game.Rulesets.Osu.Mods // rateOfChangeMultiplier only changes every 5 iterations in a combo // to prevent shaky-line-shaped streams - if (hitObject.NewCombo) - cntSinceNewCombo = 0; - else - cntSinceNewCombo++; - - if (cntSinceNewCombo % 5 == 0) + if (hitObject.IndexInCurrentCombo % 5 == 0) rateOfChangeMultiplier = (float)rng.NextDouble() * 2 - 1; if (hitObject is Spinner) From 3f185a062234cf8f9e86511a365ea496c93d7506 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Wed, 30 Jun 2021 10:35:06 +0800 Subject: [PATCH 05/17] Fixed an exception when clamping large sliders --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 78a49f8e91..e0a3e83241 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -152,10 +152,15 @@ namespace osu.Game.Rulesets.Osu.Mods var prevPosition = slider.Position; - slider.Position = new Vector2( - Math.Clamp(slider.Position.X, minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right), - Math.Clamp(slider.Position.Y, minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom) - ); + var newX = minMargin.Left + minMargin.Right > OsuPlayfield.BASE_SIZE.X + ? currentObjectInfo.PositionOriginal.X + : Math.Clamp(slider.Position.X, minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right); + + var newY = minMargin.Top + minMargin.Bottom > OsuPlayfield.BASE_SIZE.Y + ? currentObjectInfo.PositionOriginal.Y + : Math.Clamp(slider.Position.Y, minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom); + + slider.Position = new Vector2(newX, newY); currentObjectInfo.PositionRandomised = slider.Position; currentObjectInfo.EndPositionRandomised = slider.EndPosition; From 3c1f0452a2263faa5b6e017fbf02aff40c4b4c3d Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Thu, 1 Jul 2021 10:06:14 +0800 Subject: [PATCH 06/17] Refactor and rename `getMinSliderMargin` to `getSliderBoundingBox`. --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 57 ++++++++++++---------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index e0a3e83241..71c070d91b 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; -using osu.Framework.Graphics; +using osu.Framework.Graphics.Primitives; using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Rulesets.Mods; @@ -148,19 +148,14 @@ namespace osu.Game.Rulesets.Osu.Mods /// The that this slider has been shifted by. private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) { - var minMargin = getMinSliderMargin(slider); + var minMargin = getSliderBoundingBox(slider); var prevPosition = slider.Position; - var newX = minMargin.Left + minMargin.Right > OsuPlayfield.BASE_SIZE.X - ? currentObjectInfo.PositionOriginal.X - : Math.Clamp(slider.Position.X, minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right); - - var newY = minMargin.Top + minMargin.Bottom > OsuPlayfield.BASE_SIZE.Y - ? currentObjectInfo.PositionOriginal.Y - : Math.Clamp(slider.Position.Y, minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom); - - slider.Position = new Vector2(newX, newY); + slider.Position = new Vector2( + Math.Clamp(slider.Position.X, minMargin.Left, minMargin.Right), + Math.Clamp(slider.Position.Y, minMargin.Top, minMargin.Bottom) + ); currentObjectInfo.PositionRandomised = slider.Position; currentObjectInfo.EndPositionRandomised = slider.EndPosition; @@ -190,34 +185,44 @@ namespace osu.Game.Rulesets.Osu.Mods } /// - /// Calculates the min. distances from the 's position to the playfield border for the slider to be fully inside of the playfield. + /// Calculates the bounding box of a 's position for the slider to be fully inside of the playfield. /// - private MarginPadding getMinSliderMargin(Slider slider) + private RectangleF getSliderBoundingBox(Slider slider) { var pathPositions = new List(); slider.Path.GetPathToProgress(pathPositions, 0, 1); - var minMargin = new MarginPadding(); + var box = new RectangleF(); foreach (var pos in pathPositions) { - minMargin.Left = Math.Max(minMargin.Left, -pos.X); - minMargin.Right = Math.Max(minMargin.Right, pos.X); - minMargin.Top = Math.Max(minMargin.Top, -pos.Y); - minMargin.Bottom = Math.Max(minMargin.Bottom, pos.Y); + box.X = Math.Max(box.X, -pos.X); + box.Y = Math.Max(box.Y, -pos.Y); + box.Width = Math.Min(box.Width, OsuPlayfield.BASE_SIZE.X - pos.X - box.X); + box.Height = Math.Min(box.Height, OsuPlayfield.BASE_SIZE.Y - pos.Y - box.Y); } - minMargin.Left = Math.Min(minMargin.Left, OsuPlayfield.BASE_SIZE.X - minMargin.Right); - minMargin.Top = Math.Min(minMargin.Top, OsuPlayfield.BASE_SIZE.Y - minMargin.Bottom); - var radius = (float)slider.Radius; - minMargin.Left += radius; - minMargin.Right += radius; - minMargin.Top += radius; - minMargin.Bottom += radius; + box.X += radius; + box.Y += radius; + box.Width -= radius * 2; + box.Height -= radius * 2; - return minMargin; + // If the slider is larger than the playfield, force the slider to stay at its original position + if (box.Width < 0) + { + box.Width = 0; + box.X = slider.Position.X; + } + + if (box.Height < 0) + { + box.Height = 0; + box.Y = slider.Position.Y; + } + + return box; } /// From 328dcb4d6b76e546d20acbb2a46b4b2d2419e54f Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Thu, 1 Jul 2021 10:07:26 +0800 Subject: [PATCH 07/17] Use `Math.Clamp` instead of `MathHelper.Clamp` --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 71c070d91b..f4358118c7 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -243,8 +243,8 @@ namespace osu.Game.Rulesets.Osu.Mods private Vector2 clampToPlayfield(Vector2 position, float radius) { - position.X = MathHelper.Clamp(position.X, radius, OsuPlayfield.BASE_SIZE.X - radius); - position.Y = MathHelper.Clamp(position.Y, radius, OsuPlayfield.BASE_SIZE.Y - radius); + position.X = Math.Clamp(position.X, radius, OsuPlayfield.BASE_SIZE.X - radius); + position.Y = Math.Clamp(position.Y, radius, OsuPlayfield.BASE_SIZE.Y - radius); return position; } From 6e1839fcf2e48f90806bdebf3953d8152c3eab4f Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Thu, 1 Jul 2021 10:08:28 +0800 Subject: [PATCH 08/17] Rename `shift_object_count` to `objects_to_shift_before_slider` --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index f4358118c7..844d8d76a1 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -29,7 +29,7 @@ namespace osu.Game.Rulesets.Osu.Mods /// /// Number of previous hit circles to be shifted together when a slider needs to be moved. /// - private const int shift_object_count = 10; + private const int objects_to_shift_before_slider = 10; private Random rng; @@ -84,7 +84,7 @@ namespace osu.Game.Rulesets.Osu.Mods { var toBeShifted = new List(); - for (int j = i - 1; j >= i - shift_object_count && j >= 0; j--) + for (int j = i - 1; j >= i - objects_to_shift_before_slider && j >= 0; j--) { // only shift hit circles if (!(hitObjects[j] is HitCircle)) break; From 7585f1f79088b63e30cebc866757c7e248da5040 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Thu, 1 Jul 2021 10:59:06 +0800 Subject: [PATCH 09/17] Move special case handling back to `moveSliderIntoPlayfield` --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 31 +++++++++------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 844d8d76a1..ede929bfc4 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -148,14 +148,20 @@ namespace osu.Game.Rulesets.Osu.Mods /// The that this slider has been shifted by. private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) { - var minMargin = getSliderBoundingBox(slider); + var boundingBox = getSliderBoundingBox(slider); var prevPosition = slider.Position; - slider.Position = new Vector2( - Math.Clamp(slider.Position.X, minMargin.Left, minMargin.Right), - Math.Clamp(slider.Position.Y, minMargin.Top, minMargin.Bottom) - ); + // If the slider is larger than the playfield, force it to stay at the original position + var newX = boundingBox.Width < 0 + ? currentObjectInfo.PositionOriginal.X + : Math.Clamp(slider.Position.X, boundingBox.Left, boundingBox.Right); + + var newY = boundingBox.Height < 0 + ? currentObjectInfo.PositionOriginal.Y + : Math.Clamp(slider.Position.Y, boundingBox.Top, boundingBox.Bottom); + + slider.Position = new Vector2(newX, newY); currentObjectInfo.PositionRandomised = slider.Position; currentObjectInfo.EndPositionRandomised = slider.EndPosition; @@ -192,7 +198,7 @@ namespace osu.Game.Rulesets.Osu.Mods var pathPositions = new List(); slider.Path.GetPathToProgress(pathPositions, 0, 1); - var box = new RectangleF(); + var box = new RectangleF(Vector2.Zero, OsuPlayfield.BASE_SIZE); foreach (var pos in pathPositions) { @@ -209,19 +215,6 @@ namespace osu.Game.Rulesets.Osu.Mods box.Width -= radius * 2; box.Height -= radius * 2; - // If the slider is larger than the playfield, force the slider to stay at its original position - if (box.Width < 0) - { - box.Width = 0; - box.X = slider.Position.X; - } - - if (box.Height < 0) - { - box.Height = 0; - box.Y = slider.Position.Y; - } - return box; } From c69455cfd019d47e67f22538c2f9a83a5f0713b6 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Thu, 1 Jul 2021 11:20:55 +0800 Subject: [PATCH 10/17] Fixed slider bounding box calculation --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index ede929bfc4..abc7db0a82 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -202,8 +202,16 @@ namespace osu.Game.Rulesets.Osu.Mods foreach (var pos in pathPositions) { + // Reduce Width and Height accordingly after increasing X and Y + // to keep the right and bottom edge of the rectangle in place + var right = box.Right; box.X = Math.Max(box.X, -pos.X); + box.Width = right - box.X; + + var bottom = box.Bottom; box.Y = Math.Max(box.Y, -pos.Y); + box.Height = bottom - box.Y; + box.Width = Math.Min(box.Width, OsuPlayfield.BASE_SIZE.X - pos.X - box.X); box.Height = Math.Min(box.Height, OsuPlayfield.BASE_SIZE.Y - pos.Y - box.Y); } From eecf4af0293384a6fe3c0da24eaf322529ba7951 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Mon, 5 Jul 2021 09:16:01 +0800 Subject: [PATCH 11/17] Rename `getSliderBoundingBox` and add comments --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index abc7db0a82..4c856ef4a0 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -148,18 +148,19 @@ namespace osu.Game.Rulesets.Osu.Mods /// The that this slider has been shifted by. private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) { - var boundingBox = getSliderBoundingBox(slider); + var area = getSliderPlacementArea(slider); var prevPosition = slider.Position; + // Clamp slider position to the placement area // If the slider is larger than the playfield, force it to stay at the original position - var newX = boundingBox.Width < 0 + var newX = area.Width < 0 ? currentObjectInfo.PositionOriginal.X - : Math.Clamp(slider.Position.X, boundingBox.Left, boundingBox.Right); + : Math.Clamp(slider.Position.X, area.Left, area.Right); - var newY = boundingBox.Height < 0 + var newY = area.Height < 0 ? currentObjectInfo.PositionOriginal.Y - : Math.Clamp(slider.Position.Y, boundingBox.Top, boundingBox.Bottom); + : Math.Clamp(slider.Position.Y, area.Top, area.Bottom); slider.Position = new Vector2(newX, newY); @@ -191,15 +192,21 @@ namespace osu.Game.Rulesets.Osu.Mods } /// - /// Calculates the bounding box of a 's position for the slider to be fully inside of the playfield. + /// Calculates a that includes all possible positions of the slider such that + /// the entire slider is inside the playfield. /// - private RectangleF getSliderBoundingBox(Slider slider) + /// + /// If the slider is larger than the playfield, the returned may have negative width/height. + /// + private RectangleF getSliderPlacementArea(Slider slider) { var pathPositions = new List(); slider.Path.GetPathToProgress(pathPositions, 0, 1); + // Initially, assume that the slider can be placed anywhere in the playfield var box = new RectangleF(Vector2.Zero, OsuPlayfield.BASE_SIZE); + // Then narrow down the area with each path position foreach (var pos in pathPositions) { // Reduce Width and Height accordingly after increasing X and Y @@ -216,6 +223,7 @@ namespace osu.Game.Rulesets.Osu.Mods box.Height = Math.Min(box.Height, OsuPlayfield.BASE_SIZE.Y - pos.Y - box.Y); } + // Reduce the area by slider radius, so that the slider fits inside the playfield completely var radius = (float)slider.Radius; box.X += radius; From e10b7867c158059e9f041edbdbc5ac10011f2203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sat, 10 Jul 2021 12:13:36 +0200 Subject: [PATCH 12/17] Rewrite method again to hopefully help readability --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 54 ++++++++++++---------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 4c856ef4a0..cd63b618bd 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -148,7 +148,7 @@ namespace osu.Game.Rulesets.Osu.Mods /// The that this slider has been shifted by. private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) { - var area = getSliderPlacementArea(slider); + var area = calculatePossibleMovementBounds(slider); var prevPosition = slider.Position; @@ -192,46 +192,52 @@ namespace osu.Game.Rulesets.Osu.Mods } /// - /// Calculates a that includes all possible positions of the slider such that - /// the entire slider is inside the playfield. + /// Calculates a which contains all of the possible movements of the slider (in relative X/Y coordinates) + /// such that the entire slider is inside the playfield. /// /// /// If the slider is larger than the playfield, the returned may have negative width/height. /// - private RectangleF getSliderPlacementArea(Slider slider) + private RectangleF calculatePossibleMovementBounds(Slider slider) { var pathPositions = new List(); slider.Path.GetPathToProgress(pathPositions, 0, 1); - // Initially, assume that the slider can be placed anywhere in the playfield - var box = new RectangleF(Vector2.Zero, OsuPlayfield.BASE_SIZE); + float minX = float.PositiveInfinity; + float maxX = float.NegativeInfinity; - // Then narrow down the area with each path position + float minY = float.PositiveInfinity; + float maxY = float.NegativeInfinity; + + // Compute the bounding box of the slider. foreach (var pos in pathPositions) { - // Reduce Width and Height accordingly after increasing X and Y - // to keep the right and bottom edge of the rectangle in place - var right = box.Right; - box.X = Math.Max(box.X, -pos.X); - box.Width = right - box.X; + minX = MathF.Min(minX, pos.X); + maxX = MathF.Max(maxX, pos.X); - var bottom = box.Bottom; - box.Y = Math.Max(box.Y, -pos.Y); - box.Height = bottom - box.Y; - - box.Width = Math.Min(box.Width, OsuPlayfield.BASE_SIZE.X - pos.X - box.X); - box.Height = Math.Min(box.Height, OsuPlayfield.BASE_SIZE.Y - pos.Y - box.Y); + minY = MathF.Min(minY, pos.Y); + maxY = MathF.Max(maxY, pos.Y); } - // Reduce the area by slider radius, so that the slider fits inside the playfield completely + // Take the circle radius into account. var radius = (float)slider.Radius; - box.X += radius; - box.Y += radius; - box.Width -= radius * 2; - box.Height -= radius * 2; + minX -= radius; + minY -= radius; - return box; + maxX += radius; + maxY += radius; + + // Given the bounding box of the slider (via min/max X/Y), + // the amount that the slider can move to the left is minX (with the sign flipped, since positive X is to the right), + // and the amount that it can move to the right is WIDTH - maxX. + // Same calculation applies for the Y axis. + float left = -minX; + float right = OsuPlayfield.BASE_SIZE.X - maxX; + float top = -minY; + float bottom = OsuPlayfield.BASE_SIZE.Y - maxY; + + return new RectangleF(left, top, right - left, bottom - top); } /// From c181a724c6e903e61d43f23f5ff12f65ae183788 Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Sun, 11 Jul 2021 22:01:28 +0800 Subject: [PATCH 13/17] Refactor hit object clamping --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 99 +++++++++++++--------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index cd63b618bd..e134dcef89 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -67,34 +67,32 @@ namespace osu.Game.Rulesets.Osu.Mods applyRandomisation(rateOfChangeMultiplier, previous, current); - // Move hit objects back into the playfield if they are outside of it, - // which would sometimes happen during big jumps otherwise. - current.PositionRandomised = clampToPlayfield(current.PositionRandomised, (float)hitObject.Radius); + // Move hit objects back into the playfield if they are outside of it + Vector2 shift = Vector2.Zero; - hitObject.Position = current.PositionRandomised; - - // update end position as it may have changed as a result of the position update. - current.EndPositionRandomised = current.PositionRandomised; - - if (hitObject is Slider slider) + if (hitObject is HitCircle circle) { - Vector2 shift = moveSliderIntoPlayfield(slider, current); + shift = clampHitCircleToPlayfield(circle, current); + } + else if (hitObject is Slider slider) + { + shift = clampSliderToPlayfield(slider, current); + } - if (shift != Vector2.Zero) + if (shift != Vector2.Zero) + { + var toBeShifted = new List(); + + for (int j = i - 1; j >= i - objects_to_shift_before_slider && j >= 0; j--) { - var toBeShifted = new List(); + // only shift hit circles + if (!(hitObjects[j] is HitCircle)) break; - for (int j = i - 1; j >= i - objects_to_shift_before_slider && j >= 0; j--) - { - // only shift hit circles - if (!(hitObjects[j] is HitCircle)) break; - - toBeShifted.Add(hitObjects[j]); - } - - if (toBeShifted.Count > 0) - applyDecreasingShift(toBeShifted, shift); + toBeShifted.Add(hitObjects[j]); } + + if (toBeShifted.Count > 0) + applyDecreasingShift(toBeShifted, shift); } previous = current; @@ -145,31 +143,29 @@ namespace osu.Game.Rulesets.Osu.Mods /// /// Moves the and all necessary nested s into the if they aren't already. /// - /// The that this slider has been shifted by. - private Vector2 moveSliderIntoPlayfield(Slider slider, RandomObjectInfo currentObjectInfo) + /// The deviation from the original randomised position in order to fit within the playfield. + private Vector2 clampSliderToPlayfield(Slider slider, RandomObjectInfo objectInfo) { var area = calculatePossibleMovementBounds(slider); - var prevPosition = slider.Position; + var previousPosition = objectInfo.PositionRandomised; // Clamp slider position to the placement area // If the slider is larger than the playfield, force it to stay at the original position var newX = area.Width < 0 - ? currentObjectInfo.PositionOriginal.X - : Math.Clamp(slider.Position.X, area.Left, area.Right); + ? objectInfo.PositionOriginal.X + : Math.Clamp(previousPosition.X, area.Left, area.Right); var newY = area.Height < 0 - ? currentObjectInfo.PositionOriginal.Y - : Math.Clamp(slider.Position.Y, area.Top, area.Bottom); + ? objectInfo.PositionOriginal.Y + : Math.Clamp(previousPosition.Y, area.Top, area.Bottom); - slider.Position = new Vector2(newX, newY); + slider.Position = objectInfo.PositionRandomised = new Vector2(newX, newY); + objectInfo.EndPositionRandomised = slider.EndPosition; - currentObjectInfo.PositionRandomised = slider.Position; - currentObjectInfo.EndPositionRandomised = slider.EndPosition; + shiftNestedObjects(slider, objectInfo.PositionRandomised - objectInfo.PositionOriginal); - shiftNestedObjects(slider, currentObjectInfo.PositionRandomised - currentObjectInfo.PositionOriginal); - - return slider.Position - prevPosition; + return objectInfo.PositionRandomised - previousPosition; } /// @@ -187,7 +183,7 @@ namespace osu.Game.Rulesets.Osu.Mods // The last object is shifted by a vector slightly larger than zero Vector2 position = hitObject.Position + shift * ((hitObjects.Count - i) / (float)(hitObjects.Count + 1)); - hitObject.Position = clampToPlayfield(position, (float)hitObject.Radius); + hitObject.Position = clampToPlayfieldWithPadding(position, (float)hitObject.Radius); } } @@ -256,12 +252,35 @@ namespace osu.Game.Rulesets.Osu.Mods } } - private Vector2 clampToPlayfield(Vector2 position, float radius) + /// + /// Move the randomised position of a hit circle so that it fits inside the playfield. + /// + /// The deviation from the original randomised position in order to fit within the playfield. + private Vector2 clampHitCircleToPlayfield(HitCircle circle, RandomObjectInfo objectInfo) { - position.X = Math.Clamp(position.X, radius, OsuPlayfield.BASE_SIZE.X - radius); - position.Y = Math.Clamp(position.Y, radius, OsuPlayfield.BASE_SIZE.Y - radius); + var previousPosition = objectInfo.PositionRandomised; + objectInfo.EndPositionRandomised = objectInfo.PositionRandomised = clampToPlayfieldWithPadding( + objectInfo.PositionRandomised, + (float)circle.Radius + ); - return position; + circle.Position = objectInfo.PositionRandomised; + + return objectInfo.PositionRandomised - previousPosition; + } + + /// + /// Clamp a position to playfield, keeping a specified distance from the edges. + /// + /// The position to be clamped. + /// The minimum distance allowed from playfield edges. + /// The clamped position. + private Vector2 clampToPlayfieldWithPadding(Vector2 position, float padding) + { + return new Vector2( + Math.Clamp(position.X, padding, OsuPlayfield.BASE_SIZE.X - padding), + Math.Clamp(position.Y, padding, OsuPlayfield.BASE_SIZE.Y - padding) + ); } private class RandomObjectInfo From 7aecafeecb26f72ca1d2e7a1d2f72b266a2b09a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sun, 11 Jul 2021 16:46:30 +0200 Subject: [PATCH 14/17] Rename constant to reflect its purpose --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index e134dcef89..4409017ac9 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -27,9 +27,9 @@ namespace osu.Game.Rulesets.Osu.Mods private static readonly float playfield_diagonal = OsuPlayfield.BASE_SIZE.LengthFast; /// - /// Number of previous hit circles to be shifted together when a slider needs to be moved. + /// Number of previous hitobjects to be shifted together when another object is being moved. /// - private const int objects_to_shift_before_slider = 10; + private const int preceding_hitobjects_to_shift = 10; private Random rng; @@ -83,7 +83,7 @@ namespace osu.Game.Rulesets.Osu.Mods { var toBeShifted = new List(); - for (int j = i - 1; j >= i - objects_to_shift_before_slider && j >= 0; j--) + for (int j = i - 1; j >= i - preceding_hitobjects_to_shift && j >= 0; j--) { // only shift hit circles if (!(hitObjects[j] is HitCircle)) break; From 63dedb36dec1612c5d68c0293d1d551fbecd57fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sun, 11 Jul 2021 16:49:23 +0200 Subject: [PATCH 15/17] Rename variable --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 4409017ac9..f919ecf839 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -146,19 +146,19 @@ namespace osu.Game.Rulesets.Osu.Mods /// The deviation from the original randomised position in order to fit within the playfield. private Vector2 clampSliderToPlayfield(Slider slider, RandomObjectInfo objectInfo) { - var area = calculatePossibleMovementBounds(slider); + var possibleMovementBounds = calculatePossibleMovementBounds(slider); var previousPosition = objectInfo.PositionRandomised; // Clamp slider position to the placement area // If the slider is larger than the playfield, force it to stay at the original position - var newX = area.Width < 0 + var newX = possibleMovementBounds.Width < 0 ? objectInfo.PositionOriginal.X - : Math.Clamp(previousPosition.X, area.Left, area.Right); + : Math.Clamp(previousPosition.X, possibleMovementBounds.Left, possibleMovementBounds.Right); - var newY = area.Height < 0 + var newY = possibleMovementBounds.Height < 0 ? objectInfo.PositionOriginal.Y - : Math.Clamp(previousPosition.Y, area.Top, area.Bottom); + : Math.Clamp(previousPosition.Y, possibleMovementBounds.Top, possibleMovementBounds.Bottom); slider.Position = objectInfo.PositionRandomised = new Vector2(newX, newY); objectInfo.EndPositionRandomised = slider.EndPosition; From 6b663037e47b0b448d98449134248699dfce9d6d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 13 Jul 2021 19:37:02 +0900 Subject: [PATCH 16/17] Use `switch` for pattern matching --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index f919ecf839..7794ef8072 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -70,13 +70,15 @@ namespace osu.Game.Rulesets.Osu.Mods // Move hit objects back into the playfield if they are outside of it Vector2 shift = Vector2.Zero; - if (hitObject is HitCircle circle) + switch (hitObject) { - shift = clampHitCircleToPlayfield(circle, current); - } - else if (hitObject is Slider slider) - { - shift = clampSliderToPlayfield(slider, current); + case HitCircle circle: + shift = clampHitCircleToPlayfield(circle, current); + break; + + case Slider slider: + shift = clampSliderToPlayfield(slider, current); + break; } if (shift != Vector2.Zero) From 4314946e10726756eb7a2df717fbbab131a14e19 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 13 Jul 2021 19:37:17 +0900 Subject: [PATCH 17/17] Reorganise functions to order more logically (hitcircle before slider methods) --- osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs index 7794ef8072..1a2e5d92b4 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRandom.cs @@ -142,6 +142,23 @@ namespace osu.Game.Rulesets.Osu.Mods current.PositionRandomised = previous.EndPositionRandomised + posRelativeToPrev; } + /// + /// Move the randomised position of a hit circle so that it fits inside the playfield. + /// + /// The deviation from the original randomised position in order to fit within the playfield. + private Vector2 clampHitCircleToPlayfield(HitCircle circle, RandomObjectInfo objectInfo) + { + var previousPosition = objectInfo.PositionRandomised; + objectInfo.EndPositionRandomised = objectInfo.PositionRandomised = clampToPlayfieldWithPadding( + objectInfo.PositionRandomised, + (float)circle.Radius + ); + + circle.Position = objectInfo.PositionRandomised; + + return objectInfo.PositionRandomised - previousPosition; + } + /// /// Moves the and all necessary nested s into the if they aren't already. /// @@ -254,23 +271,6 @@ namespace osu.Game.Rulesets.Osu.Mods } } - /// - /// Move the randomised position of a hit circle so that it fits inside the playfield. - /// - /// The deviation from the original randomised position in order to fit within the playfield. - private Vector2 clampHitCircleToPlayfield(HitCircle circle, RandomObjectInfo objectInfo) - { - var previousPosition = objectInfo.PositionRandomised; - objectInfo.EndPositionRandomised = objectInfo.PositionRandomised = clampToPlayfieldWithPadding( - objectInfo.PositionRandomised, - (float)circle.Radius - ); - - circle.Position = objectInfo.PositionRandomised; - - return objectInfo.PositionRandomised - previousPosition; - } - /// /// Clamp a position to playfield, keeping a specified distance from the edges. ///