Commit cf991a34 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Code cleanup for physics (#9193)

* Add tolerance argument to FrictionSimulation.
* Change FrictionSimulation.through to setting tolerance
  via the constructor rather than afterwards.
* Allow SimulationGroup constructor to take tolerance argument.
* Add a toString for SpringDescription.
* Add a toString for SpringSimulation.
* Push this change to BouncingScrollSimulation.
parent dcd38334
...@@ -18,11 +18,13 @@ class FrictionSimulation extends Simulation { ...@@ -18,11 +18,13 @@ class FrictionSimulation extends Simulation {
/// drag coefficient, a unitless value; the initial position, in the same /// drag coefficient, a unitless value; the initial position, in the same
/// length units as used for [x]; and the initial velocity, in the same /// length units as used for [x]; and the initial velocity, in the same
/// velocity units as used for [dx]. /// velocity units as used for [dx].
FrictionSimulation(double drag, double position, double velocity) FrictionSimulation(double drag, double position, double velocity, {
: _drag = drag, Tolerance tolerance: Tolerance.defaultTolerance,
_dragLog = math.log(drag), }) : _drag = drag,
_x = position, _dragLog = math.log(drag),
_v = velocity; _x = position,
_v = velocity,
super(tolerance: tolerance);
/// Creates a new friction simulation with its fluid drag coefficient set so /// Creates a new friction simulation with its fluid drag coefficient set so
/// as to ensure that the simulation starts and ends at the specified /// as to ensure that the simulation starts and ends at the specified
...@@ -42,8 +44,9 @@ class FrictionSimulation extends Simulation { ...@@ -42,8 +44,9 @@ class FrictionSimulation extends Simulation {
return new FrictionSimulation( return new FrictionSimulation(
_dragFor(startPosition, endPosition, startVelocity, endVelocity), _dragFor(startPosition, endPosition, startVelocity, endVelocity),
startPosition, startPosition,
startVelocity startVelocity,
)..tolerance = new Tolerance(velocity: endVelocity.abs()); tolerance: new Tolerance(velocity: endVelocity.abs()),
);
} }
final double _drag; final double _drag;
......
...@@ -19,6 +19,8 @@ import 'utils.dart'; ...@@ -19,6 +19,8 @@ import 'utils.dart';
/// by this group as they become active. This mean simulations should not be /// by this group as they become active. This mean simulations should not be
/// shared among different groups that are active at the same time. /// shared among different groups that are active at the same time.
abstract class SimulationGroup extends Simulation { abstract class SimulationGroup extends Simulation {
/// Initializes the [tolerance] field for subclasses.
SimulationGroup({ Tolerance tolerance: Tolerance.defaultTolerance }) : super(tolerance: tolerance);
/// The currently active simulation. /// The currently active simulation.
/// ///
......
...@@ -53,6 +53,9 @@ class SpringDescription { ...@@ -53,6 +53,9 @@ class SpringDescription {
/// used for the value of the [mass] property, and T is the time unit used for /// used for the value of the [mass] property, and T is the time unit used for
/// driving the [SpringSimulation]. /// driving the [SpringSimulation].
final double damping; final double damping;
@override
String toString() => '$runtimeType(mass: ${mass.toStringAsFixed(1)}, springConstant: ${springConstant.toStringAsFixed(1)}, damping: ${damping.toStringAsFixed(1)})';
} }
/// The kind of spring solution that the [SpringSimulation] is using to simulate the spring. /// The kind of spring solution that the [SpringSimulation] is using to simulate the spring.
...@@ -114,6 +117,9 @@ class SpringSimulation extends Simulation { ...@@ -114,6 +117,9 @@ class SpringSimulation extends Simulation {
return nearZero(_solution.x(time), tolerance.distance) && return nearZero(_solution.x(time), tolerance.distance) &&
nearZero(_solution.dx(time), tolerance.velocity); nearZero(_solution.dx(time), tolerance.velocity);
} }
@override
String toString() => '$runtimeType(end: $_endPosition, $type)';
} }
/// A SpringSimulation where the value of [x] is guaranteed to have exactly the /// A SpringSimulation where the value of [x] is guaranteed to have exactly the
......
...@@ -34,9 +34,11 @@ class BouncingScrollSimulation extends SimulationGroup { ...@@ -34,9 +34,11 @@ class BouncingScrollSimulation extends SimulationGroup {
@required double leadingExtent, @required double leadingExtent,
@required double trailingExtent, @required double trailingExtent,
@required SpringDescription spring, @required SpringDescription spring,
Tolerance tolerance: Tolerance.defaultTolerance,
}) : _leadingExtent = leadingExtent, }) : _leadingExtent = leadingExtent,
_trailingExtent = trailingExtent, _trailingExtent = trailingExtent,
_spring = spring { _spring = spring,
super(tolerance: tolerance) {
assert(position != null); assert(position != null);
assert(velocity != null); assert(velocity != null);
assert(_leadingExtent != null); assert(_leadingExtent != null);
...@@ -73,15 +75,15 @@ class BouncingScrollSimulation extends SimulationGroup { ...@@ -73,15 +75,15 @@ class BouncingScrollSimulation extends SimulationGroup {
if (position > _trailingExtent) { if (position > _trailingExtent) {
_isSpringing = true; _isSpringing = true;
_offset = intervalOffset; _offset = intervalOffset;
_currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity); _currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity, tolerance: tolerance);
return true; return true;
} else if (position < _leadingExtent) { } else if (position < _leadingExtent) {
_isSpringing = true; _isSpringing = true;
_offset = intervalOffset; _offset = intervalOffset;
_currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity); _currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity, tolerance: tolerance);
return true; return true;
} else if (_currentSimulation == null) { } else if (_currentSimulation == null) {
_currentSimulation = new FrictionSimulation(0.135, position, velocity); _currentSimulation = new FrictionSimulation(0.135, position, velocity, tolerance: tolerance);
return true; return true;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment