1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "AxisPhysicsMSDModel.h"
8 #include <math.h> // for sqrt and fabs
14 * Constructs an AxisPhysicsMSDModel with initial values for state.
16 * @param aInitialPosition sets the initial position of the simulated spring,
18 * @param aInitialDestination sets the resting position of the simulated spring,
20 * @param aInitialVelocity sets the initial velocity of the simulated spring,
21 * in AppUnits / second. Critically-damped and over-damped systems are
22 * guaranteed not to overshoot aInitialDestination if this is set to 0;
23 * however, it is possible to overshoot and oscillate if not set to 0 or
24 * the system is under-damped.
25 * @param aSpringConstant sets the strength of the simulated spring. Greater
26 * values of mSpringConstant result in a stiffer / stronger spring.
27 * @param aDampingRatio controls the amount of dampening force and determines
28 * if the system is under-damped, critically-damped, or over-damped.
30 AxisPhysicsMSDModel::AxisPhysicsMSDModel(double aInitialPosition
,
31 double aInitialDestination
,
32 double aInitialVelocity
,
33 double aSpringConstant
,
35 : AxisPhysicsModel(aInitialPosition
, aInitialVelocity
)
36 , mDestination(aInitialDestination
)
37 , mSpringConstant(aSpringConstant
)
38 , mSpringConstantSqrtXTwo(sqrt(mSpringConstant
) * 2.0)
39 , mDampingRatio(aDampingRatio
)
43 AxisPhysicsMSDModel::~AxisPhysicsMSDModel()
48 AxisPhysicsMSDModel::Acceleration(const State
&aState
)
50 // Simulate a Mass-Damper-Spring Model; assume a unit mass
52 // Hooke’s Law: http://en.wikipedia.org/wiki/Hooke%27s_law
53 double spring_force
= (mDestination
- aState
.p
) * mSpringConstant
;
54 double damp_force
= -aState
.v
* mDampingRatio
* mSpringConstantSqrtXTwo
;
56 return spring_force
+ damp_force
;
61 AxisPhysicsMSDModel::GetDestination()
67 AxisPhysicsMSDModel::SetDestination(double aDestination
)
69 mDestination
= aDestination
;
73 AxisPhysicsMSDModel::IsFinished()
75 // In order to satisfy the condition of reaching the destination, the distance
76 // between the simulation position and the destination must be less than
77 // kFinishDistance while the speed is simultaneously less than
78 // kFinishVelocity. This enables an under-damped system to overshoot the
79 // destination when desired without prematurely triggering the finished state.
81 // As the number of app units per css pixel is 60 and retina / HiDPI displays
82 // may display two pixels for every css pixel, setting kFinishDistance to 30.0
83 // ensures that there will be no perceptable shift in position at the end
85 const double kFinishDistance
= 30.0;
87 // If kFinishVelocity is set too low, the animation may end long after
88 // oscillation has finished, resulting in unnecessary processing.
89 // If set too high, the animation may prematurely terminate when expected
90 // to overshoot the destination in an under-damped system.
91 // 60.0 was selected through experimentation that revealed that a
92 // critically damped system will terminate within 100ms.
93 const double kFinishVelocity
= 60.0;
95 return fabs(mDestination
- GetPosition ()) < kFinishDistance
96 && fabs(GetVelocity()) <= kFinishVelocity
;