import Foundation public struct SpringParameters: Sendable, Equatable { public let mass: Double public let stiffness: Double public let damping: Double public init(mass: Double = 1, stiffness: Double, damping: Double) { precondition(mass.isFinite && mass > 0) precondition(stiffness.isFinite && stiffness > 0) precondition(damping.isFinite && damping >= 0) self.mass = mass self.stiffness = stiffness self.damping = damping } public init(response: Double, dampingFraction: Double) { precondition(response.isFinite && response > 0) precondition(dampingFraction.isFinite && dampingFraction >= 0) let frequency = 2 * Double.pi / response self.init(stiffness: frequency * frequency, damping: 2 * dampingFraction * frequency) } public init(duration: Double, bounce: Double) { precondition(bounce.isFinite && bounce >= 0 && bounce < 1) self.init(response: duration, dampingFraction: 1 - bounce) } public var dampingFraction: Double { damping / (2 * sqrt(mass * stiffness)) } } public struct SpringState: Sendable, Equatable { public var value: Double public var velocity: Double public init(value: Double, velocity: Double = 0) { precondition(value.isFinite && velocity.isFinite) self.value = value self.velocity = velocity } public func advanced(to target: Double, by deltaTime: Double, using spring: SpringParameters) -> Self { precondition(target.isFinite && deltaTime.isFinite && deltaTime >= 0) guard deltaTime > 0 else { return self } let frequency = sqrt(spring.stiffness / spring.mass) let decay = spring.damping / (2 * spring.mass) let displacement = value - target let discriminant = frequency * frequency - decay * decay let position: Double let speed: Double if abs(discriminant) <= frequency * frequency * 1e-10 { let envelope = exp(-decay * deltaTime) guard envelope > 0 else { return Self(value: target) } let coefficient = velocity + decay * displacement position = envelope * (displacement + coefficient * deltaTime) speed = envelope * (velocity - decay * coefficient * deltaTime) } else if discriminant > 0 { let envelope = exp(-decay * deltaTime) guard envelope > 0 else { return Self(value: target) } let oscillation = sqrt(discriminant) let angle = oscillation * deltaTime let phase = angle.isFinite ? angle : deltaTime.truncatingRemainder(dividingBy: 2 * Double.pi / oscillation) * oscillation let sine = sin(phase) let cosine = cos(phase) let coefficient = (velocity + decay * displacement) / oscillation position = envelope * (displacement * cosine + coefficient * sine) speed = envelope * (velocity * cosine - (decay * coefficient + displacement * oscillation) * sine) } else { let root = sqrt(-discriminant) // This form avoids subtracting two nearly equal roots for heavily damped springs. let slow = -frequency * frequency / (decay + root) let fast = -decay - root let slowCoefficient = (velocity - fast * displacement) / (slow - fast) let fastCoefficient = displacement - slowCoefficient let slowEnvelope = exp(slow * deltaTime) let fastEnvelope = exp(fast * deltaTime) position = slowCoefficient * slowEnvelope + fastCoefficient * fastEnvelope speed = slow * slowCoefficient * slowEnvelope + fast * fastCoefficient * fastEnvelope } return Self(value: target + position, velocity: speed) } public mutating func advance(to target: Double, by deltaTime: Double, using spring: SpringParameters) { self = advanced(to: target, by: deltaTime, using: spring) } }