From 6b36bb495a4bd8b340422bd3da1307f98d6c2226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Tue, 20 Sep 2022 09:53:32 +0200 Subject: [PATCH] Refactor spring solver --- src/Avalonia.Base/Utilities/SpringSolver.cs | 51 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/Utilities/SpringSolver.cs b/src/Avalonia.Base/Utilities/SpringSolver.cs index c0ad7a9952..6a9e69d221 100644 --- a/src/Avalonia.Base/Utilities/SpringSolver.cs +++ b/src/Avalonia.Base/Utilities/SpringSolver.cs @@ -37,10 +37,55 @@ internal struct SpringSolver private double m_A; private double m_B; - public SpringSolver(double mass, double stiffness, double damping, double initialVelocity) + /// + /// + /// + /// The time period. + /// The damping ratio. + /// + public SpringSolver(TimeSpan period, double zeta, double initialVelocity) + : this( + 2 * Math.PI / period.TotalSeconds, + zeta, + initialVelocity) { - m_w0 = Math.Sqrt(stiffness / mass); - m_zeta = damping / (2 * Math.Sqrt(stiffness * mass)); + // T is period + // T = 2 * PI * sqrt(m / k) + } + + /// + /// + /// + /// The mass of the oscillating body. + /// The stiffness of the oscillated body (spring constant). + /// The actual damping. + /// The initial velocity. + public SpringSolver(double m, double k, double c, double initialVelocity) + : this( + Math.Sqrt(k / m), // ωn + c / (2 * Math.Sqrt(k * m)), // c / Cc + initialVelocity) + { + // Cc is critical damping coefficient + // Cc = 2 * Sqrt(k * m) + // Cc = 2 * m * wn + // Cc = 2 * m * Sqrt(k / m) + + // ζ is damping ratio (Greek letter zeta) + // ζ = m_zeta = c / Cc + } + + /// + /// + /// + /// The the natural frequency of the system [rad/s]. + /// The damping ratio. + /// + public SpringSolver(double ωn, double zeta, double initialVelocity) + { + // ωn = sqrt(k / m) + m_w0 = ωn; + m_zeta = zeta; if (m_zeta < 1) { // Under-damped.