Line data Source code
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/. */
6 :
7 : #include "AxisPhysicsMSDModel.h"
8 : #include <math.h> // for sqrt and fabs
9 :
10 : namespace mozilla {
11 : namespace layers {
12 :
13 : /**
14 : * Constructs an AxisPhysicsMSDModel with initial values for state.
15 : *
16 : * @param aInitialPosition sets the initial position of the simulated spring,
17 : * in AppUnits.
18 : * @param aInitialDestination sets the resting position of the simulated spring,
19 : * in AppUnits.
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.
29 : */
30 4 : AxisPhysicsMSDModel::AxisPhysicsMSDModel(double aInitialPosition,
31 : double aInitialDestination,
32 : double aInitialVelocity,
33 : double aSpringConstant,
34 4 : double aDampingRatio)
35 : : AxisPhysicsModel(aInitialPosition, aInitialVelocity)
36 : , mDestination(aInitialDestination)
37 : , mSpringConstant(aSpringConstant)
38 4 : , mSpringConstantSqrtXTwo(sqrt(mSpringConstant) * 2.0)
39 8 : , mDampingRatio(aDampingRatio)
40 : {
41 4 : }
42 :
43 0 : AxisPhysicsMSDModel::~AxisPhysicsMSDModel()
44 : {
45 0 : }
46 :
47 : double
48 0 : AxisPhysicsMSDModel::Acceleration(const State &aState)
49 : {
50 : // Simulate a Mass-Damper-Spring Model; assume a unit mass
51 :
52 : // Hooke’s Law: http://en.wikipedia.org/wiki/Hooke%27s_law
53 0 : double spring_force = (mDestination - aState.p) * mSpringConstant;
54 0 : double damp_force = -aState.v * mDampingRatio * mSpringConstantSqrtXTwo;
55 :
56 0 : return spring_force + damp_force;
57 : }
58 :
59 :
60 : double
61 0 : AxisPhysicsMSDModel::GetDestination() const
62 : {
63 0 : return mDestination;
64 : }
65 :
66 : void
67 0 : AxisPhysicsMSDModel::SetDestination(double aDestination)
68 : {
69 0 : mDestination = aDestination;
70 0 : }
71 :
72 : bool
73 0 : AxisPhysicsMSDModel::IsFinished(double aSmallestVisibleIncrement)
74 : {
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 : // aSmallestVisibleIncrement while the speed is simultaneously less than
78 : // finishVelocity. This enables an under-damped system to overshoot the
79 : // destination when desired without prematurely triggering the finished state.
80 : // If finishVelocity is set too low, the animation may end long after
81 : // oscillation has finished, resulting in unnecessary processing.
82 : // If set too high, the animation may prematurely terminate when expected
83 : // to overshoot the destination in an under-damped system.
84 : // aSmallestVisibleIncrement * 2 was selected through experimentation that
85 : // revealed that a critically damped system will terminate within 100ms.
86 0 : const double finishVelocity = aSmallestVisibleIncrement * 2;
87 :
88 0 : return fabs(mDestination - GetPosition ()) < aSmallestVisibleIncrement
89 0 : && fabs(GetVelocity()) <= finishVelocity;
90 : }
91 :
92 : } // namespace layers
93 : } // namespace mozilla
|