From e5a96acac0c4d3d359b89cc623a20336293f456c Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 14 Oct 2015 16:32:26 -0700 Subject: [PATCH] Added ScrollSpringSimulation SimulationGroup isDone() now uses currentIntervalOffset --- .../newton/lib/src/scroll_simulation.dart | 4 +-- packages/newton/lib/src/simulation_group.dart | 2 +- .../newton/lib/src/spring_simulation.dart | 20 +++++++++++++ packages/newton/test/newton_test.dart | 28 ++++++++++++++++--- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index f45b6724c5c..64e0a45ba11 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -47,12 +47,12 @@ class ScrollSimulation extends SimulationGroup { if (position > _trailingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new SpringSimulation(_spring, position, _trailingExtent, velocity); + _currentSimulation = new ScrollSpringSimulation(_spring, position, _trailingExtent, velocity); return true; } else if (position < _leadingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new SpringSimulation(_spring, position, _leadingExtent, velocity); + _currentSimulation = new ScrollSpringSimulation(_spring, position, _leadingExtent, velocity); return true; } } diff --git a/packages/newton/lib/src/simulation_group.dart b/packages/newton/lib/src/simulation_group.dart index 44e7bb8673e..a7359dac3fc 100644 --- a/packages/newton/lib/src/simulation_group.dart +++ b/packages/newton/lib/src/simulation_group.dart @@ -41,7 +41,7 @@ abstract class SimulationGroup extends Simulation { @override bool isDone(double time) { _stepIfNecessary(time); - return currentSimulation.isDone(time); + return currentSimulation.isDone(time - currentIntervalOffset); } double _lastStep = -1.0; diff --git a/packages/newton/lib/src/spring_simulation.dart b/packages/newton/lib/src/spring_simulation.dart index 5b267b0daa1..143f96c77fd 100644 --- a/packages/newton/lib/src/spring_simulation.dart +++ b/packages/newton/lib/src/spring_simulation.dart @@ -61,3 +61,23 @@ class SpringSimulation extends Simulation { _nearEqual(x(time), _endPosition, this.tolerance.distance) && _nearZero(dx(time), this.tolerance.velocity); } + +/// A SpringSimulation where the value of x() is guaranteed to have exactly the +/// end value when the simulation isDone(). +class ScrollSpringSimulation extends SpringSimulation { + ScrollSpringSimulation(SpringDescription desc, double start, double end, double velocity) + : super(desc, start, end, velocity); + + bool _isDone(double position, double velocity) { + return _nearEqual(position, _endPosition, tolerance.distance) && _nearZero(velocity, tolerance.velocity); + } + + @override + double x(double time) { + double xAtTime = super.x(time); + return _isDone(xAtTime, dx(time)) ? _endPosition : xAtTime; + } + + @override + bool isDone(double time) => _isDone(x(time), dx(time)); +} diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index cd302c0e32f..20efa211d2c 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -194,14 +194,16 @@ void main() { mass: 1.0, springConstant: 50.0, ratio: 0.5); var scroll = new ScrollSimulation(100.0, 800.0, 0.0, 300.0, spring, 0.3); - - scroll.tolerance = const Tolerance(velocity: 0.01, distance: 0.01); - + scroll.tolerance = const Tolerance(velocity: 0.5, distance: 0.1); expect(scroll.isDone(0.0), false); + expect(scroll.isDone(0.5), false); // switch from friction to spring expect(scroll.isDone(3.5), true); var scroll2 = new ScrollSimulation(100.0, -800.0, 0.0, 300.0, spring, 0.3); - expect(scroll2.isDone(4.5), true); + scroll2.tolerance = const Tolerance(velocity: 0.5, distance: 0.1); + expect(scroll2.isDone(0.0), false); + expect(scroll2.isDone(0.5), false); // switch from friction to spring + expect(scroll2.isDone(3.5), true); }); test('scroll_with_inf_edge_ends', () { @@ -229,4 +231,22 @@ void main() { // We should never switch expect(scroll.currentIntervalOffset, 0.0); }); + + test('over/under scroll spring', () { + var spring = new SpringDescription.withDampingRatio(mass: 1.0, springConstant: 170.0, ratio: 1.1); + var scroll = new ScrollSimulation(500.0, -7500.0, 0.0, 1000.0, spring, 0.025); + scroll.tolerance = new Tolerance(velocity: 45.0, distance: 1.5); + + expect(scroll.isDone(0.0), false); + expect(scroll.x(0.0), closeTo(500.0, .0001)); + expect(scroll.dx(0.0), closeTo(-7500.0, .0001)); + + expect(scroll.isDone(0.025), false); + expect(scroll.x(0.025), closeTo(320.0, 1.0)); + expect(scroll.dx(0.25), closeTo(-2982, 1.0)); + + expect(scroll.isDone(2.0), true); + expect(scroll.x(2.0), 0.0); + expect(scroll.dx(2.0), closeTo(0.0, 45.0)); + }); }