Add double variant for Spring class (#6706)

* Add double variant for spring class

* Fix precedence implicit brackets

---------

Co-authored-by: marvin <m.schuerz@hautzy.com>
Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
maarvin
2026-02-07 13:30:50 +01:00
committed by GitHub
parent 5c37ed90cd
commit c9ee4da7a4

View File

@@ -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<double>
{
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<Vector2>
{
protected override Vector2 GetTargetVelocity(Vector2 target, Vector2 previousTarget, float dt) => (target - previousTarget) / dt;