Skip to content

Commit c9ee4da

Browse files
minetoblendmarvinpeppy
authored
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>
1 parent 5c37ed9 commit c9ee4da

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

osu.Framework/Graphics/Transforms/Spring.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ public T Update(double elapsed, T target, T? targetVelocity = null)
117117

118118
protected void ComputeSingleValue(float dt, ref float current, ref float velocity, float target, float targetVelocity)
119119
{
120-
float k2Stable = MathF.Max(MathF.Max(k2, dt * dt / 2 + dt * k1 / 2), dt * k1);
120+
float k2Stable = MathF.Max(MathF.Max(k2, (dt * dt) / 2 + (dt * k1) / 2), dt * k1);
121121

122122
current += dt * velocity;
123-
velocity += (dt * (target + k3 * targetVelocity - current - k1 * velocity)) / k2Stable;
123+
velocity += (dt * (target + (k3 * targetVelocity) - current - (k1 * velocity))) / k2Stable;
124+
}
125+
126+
protected void ComputeSingleValue(float dt, ref double current, ref double velocity, double target, double targetVelocity)
127+
{
128+
float k2Stable = MathF.Max(MathF.Max(k2, (dt * dt) / 2 + (dt * k1) / 2), dt * k1);
129+
130+
current += dt * velocity;
131+
velocity += (dt * (target + (k3 * targetVelocity) - current - (k1 * velocity))) / k2Stable;
124132
}
125133
}
126134

@@ -136,6 +144,18 @@ protected override float ComputeNextValue(float dt, float target, float targetVe
136144
}
137145
}
138146

147+
public class DoubleSpring : Spring<double>
148+
{
149+
protected override double GetTargetVelocity(double target, double previousTarget, float dt) => (target - previousTarget) / dt;
150+
151+
protected override double ComputeNextValue(float dt, double target, double targetVelocity)
152+
{
153+
ComputeSingleValue(dt, ref Current, ref Velocity, target, targetVelocity);
154+
155+
return Current;
156+
}
157+
}
158+
139159
public class Vector2Spring : Spring<Vector2>
140160
{
141161
protected override Vector2 GetTargetVelocity(Vector2 target, Vector2 previousTarget, float dt) => (target - previousTarget) / dt;

0 commit comments

Comments
 (0)