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 {
/// drag coefficient, a unitless value; the initial position, in the same
/// length units as used for [x]; and the initial velocity, in the same
/// velocity units as used for [dx].
FrictionSimulation(double drag, double position, double velocity)
: _drag = drag,
FrictionSimulation(double drag, double position, double velocity, {
Tolerance tolerance: Tolerance.defaultTolerance,
}) : _drag = drag,
_dragLog = math.log(drag),
_x = position,
_v = velocity;
_v = velocity,
super(tolerance: tolerance);
/// Creates a new friction simulation with its fluid drag coefficient set so
/// as to ensure that the simulation starts and ends at the specified
......@@ -42,8 +44,9 @@ class FrictionSimulation extends Simulation {
return new FrictionSimulation(
_dragFor(startPosition, endPosition, startVelocity, endVelocity),
startPosition,
startVelocity
)..tolerance = new Tolerance(velocity: endVelocity.abs());
startVelocity,
tolerance: new Tolerance(velocity: endVelocity.abs()),
);
}
final double _drag;
......
......@@ -19,6 +19,8 @@ import 'utils.dart';
/// by this group as they become active. This mean simulations should not be
/// shared among different groups that are active at the same time.
abstract class SimulationGroup extends Simulation {
/// Initializes the [tolerance] field for subclasses.
SimulationGroup({ Tolerance tolerance: Tolerance.defaultTolerance }) : super(tolerance: tolerance);
/// The currently active simulation.
///
......
......@@ -53,6 +53,9 @@ class SpringDescription {
/// used for the value of the [mass] property, and T is the time unit used for
/// driving the [SpringSimulation].
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.
......@@ -114,6 +117,9 @@ class SpringSimulation extends Simulation {
return nearZero(_solution.x(time), tolerance.distance) &&
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
......
......@@ -34,9 +34,11 @@ class BouncingScrollSimulation extends SimulationGroup {
@required double leadingExtent,
@required double trailingExtent,
@required SpringDescription spring,
Tolerance tolerance: Tolerance.defaultTolerance,
}) : _leadingExtent = leadingExtent,
_trailingExtent = trailingExtent,
_spring = spring {
_spring = spring,
super(tolerance: tolerance) {
assert(position != null);
assert(velocity != null);
assert(_leadingExtent != null);
......@@ -73,15 +75,15 @@ class BouncingScrollSimulation extends SimulationGroup {
if (position > _trailingExtent) {
_isSpringing = true;
_offset = intervalOffset;
_currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity);
_currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity, tolerance: tolerance);
return true;
} else if (position < _leadingExtent) {
_isSpringing = true;
_offset = intervalOffset;
_currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity);
_currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity, tolerance: tolerance);
return true;
} else if (_currentSimulation == null) {
_currentSimulation = new FrictionSimulation(0.135, position, velocity);
_currentSimulation = new FrictionSimulation(0.135, position, velocity, tolerance: tolerance);
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