From c9ee4da7a48d5ec876999f86fcab67d9bc94bcd1 Mon Sep 17 00:00:00 2001 From: maarvin Date: Sat, 7 Feb 2026 13:30:50 +0100 Subject: [PATCH] Add `double` variant for `Spring` class (#6706) * Add double variant for spring class * Fix precedence implicit brackets --------- Co-authored-by: marvin Co-authored-by: Dean Herbert --- osu.Framework/Graphics/Transforms/Spring.cs | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/osu.Framework/Graphics/Transforms/Spring.cs b/osu.Framework/Graphics/Transforms/Spring.cs index 8fe123a5f..9759d7238 100644 --- a/osu.Framework/Graphics/Transforms/Spring.cs +++ b/osu.Framework/Graphics/Transforms/Spring.cs @@ -117,10 +117,18 @@ namespace osu.Framework.Graphics.Transforms protected void ComputeSingleValue(float dt, ref float current, ref float velocity, float target, float targetVelocity) { - float k2Stable = MathF.Max(MathF.Max(k2, dt * dt / 2 + dt * k1 / 2), dt * k1); + float k2Stable = MathF.Max(MathF.Max(k2, (dt * dt) / 2 + (dt * k1) / 2), dt * k1); current += dt * velocity; - velocity += (dt * (target + k3 * targetVelocity - current - k1 * velocity)) / k2Stable; + velocity += (dt * (target + (k3 * targetVelocity) - current - (k1 * velocity))) / k2Stable; + } + + protected void ComputeSingleValue(float dt, ref double current, ref double velocity, double target, double targetVelocity) + { + float k2Stable = MathF.Max(MathF.Max(k2, (dt * dt) / 2 + (dt * k1) / 2), dt * k1); + + current += dt * velocity; + velocity += (dt * (target + (k3 * targetVelocity) - current - (k1 * velocity))) / k2Stable; } } @@ -136,6 +144,18 @@ namespace osu.Framework.Graphics.Transforms } } + public class DoubleSpring : Spring + { + protected override double GetTargetVelocity(double target, double previousTarget, float dt) => (target - previousTarget) / dt; + + protected override double ComputeNextValue(float dt, double target, double targetVelocity) + { + ComputeSingleValue(dt, ref Current, ref Velocity, target, targetVelocity); + + return Current; + } + } + public class Vector2Spring : Spring { protected override Vector2 GetTargetVelocity(Vector2 target, Vector2 previousTarget, float dt) => (target - previousTarget) / dt;