From 5a439792f16f1e9b4263ffa856cc5741b9240660 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 1 Jul 2015 18:07:25 -0700 Subject: [PATCH 01/21] Non composite simulations: Friction, gravity, spring --- packages/newton/README.md | 3 + packages/newton/lib/newton.dart | 17 ++++ packages/newton/lib/src/fall.dart | 8 ++ packages/newton/lib/src/friction.dart | 26 ++++++ packages/newton/lib/src/gravity.dart | 26 ++++++ packages/newton/lib/src/scroll.dart | 8 ++ packages/newton/lib/src/simulation.dart | 26 ++++++ packages/newton/lib/src/spring.dart | 46 +++++++++++ packages/newton/lib/src/spring_solution.dart | 85 ++++++++++++++++++++ packages/newton/lib/src/utils.dart | 12 +++ packages/newton/test/newton_test.dart | 11 +++ 11 files changed, 268 insertions(+) create mode 100644 packages/newton/README.md create mode 100644 packages/newton/lib/newton.dart create mode 100644 packages/newton/lib/src/fall.dart create mode 100644 packages/newton/lib/src/friction.dart create mode 100644 packages/newton/lib/src/gravity.dart create mode 100644 packages/newton/lib/src/scroll.dart create mode 100644 packages/newton/lib/src/simulation.dart create mode 100644 packages/newton/lib/src/spring.dart create mode 100644 packages/newton/lib/src/spring_solution.dart create mode 100644 packages/newton/lib/src/utils.dart create mode 100644 packages/newton/test/newton_test.dart diff --git a/packages/newton/README.md b/packages/newton/README.md new file mode 100644 index 0000000000..0562d23ed7 --- /dev/null +++ b/packages/newton/README.md @@ -0,0 +1,3 @@ +# Newton + +Simple Physics Simulations for Dart. Springs, friction, gravity, etc. diff --git a/packages/newton/lib/newton.dart b/packages/newton/lib/newton.dart new file mode 100644 index 0000000000..bc2de1a90f --- /dev/null +++ b/packages/newton/lib/newton.dart @@ -0,0 +1,17 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +library newton; + +import 'dart:math' as Math; + +part 'src/simulation.dart'; +part 'src/utils.dart'; + +part 'src/fall.dart'; +part 'src/friction.dart'; +part 'src/gravity.dart'; +part 'src/scroll.dart'; +part 'src/spring.dart'; +part 'src/spring_solution.dart'; diff --git a/packages/newton/lib/src/fall.dart b/packages/newton/lib/src/fall.dart new file mode 100644 index 0000000000..ca5ec9556b --- /dev/null +++ b/packages/newton/lib/src/fall.dart @@ -0,0 +1,8 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +// TODO(csg): Composite simulation +class Fall {} diff --git a/packages/newton/lib/src/friction.dart b/packages/newton/lib/src/friction.dart new file mode 100644 index 0000000000..c0d2320982 --- /dev/null +++ b/packages/newton/lib/src/friction.dart @@ -0,0 +1,26 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +class Friction extends Simulation { + final double _drag; + final double _dragNaturalLog; + final double _x; + final double _v; + + Friction(double drag, double position, double velocity) + : _drag = drag, + _dragNaturalLog = Math.log(drag), + _x = position, + _v = velocity; + + double x(double time) => + _x + _v + Math.pow(_drag, time) / _dragNaturalLog - _v / _dragNaturalLog; + + double dx(double time) => _v * Math.pow(_drag, time); + + @override + bool isDone(double time) => dx(time).abs() < 1.0; +} diff --git a/packages/newton/lib/src/gravity.dart b/packages/newton/lib/src/gravity.dart new file mode 100644 index 0000000000..4eb11f69d4 --- /dev/null +++ b/packages/newton/lib/src/gravity.dart @@ -0,0 +1,26 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +class Gravity extends Simulation { + final double _x; + final double _v; + final double _a; + final double _end; + + Gravity( + double acceleration, double distance, double endDistance, double velocity) + : _a = acceleration, + _x = distance, + _v = velocity, + _end = endDistance; + + double x(double time) => _x + _v * time + 0.5 * _a * time * time; + + double dx(double time) => _v + time * _a; + + @override + bool isDone(double time) => x(time).abs() >= _end; +} diff --git a/packages/newton/lib/src/scroll.dart b/packages/newton/lib/src/scroll.dart new file mode 100644 index 0000000000..12f9f4957a --- /dev/null +++ b/packages/newton/lib/src/scroll.dart @@ -0,0 +1,8 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +// TODO(csg): Composite simulation +class Scroll {} diff --git a/packages/newton/lib/src/simulation.dart b/packages/newton/lib/src/simulation.dart new file mode 100644 index 0000000000..12b6b926c2 --- /dev/null +++ b/packages/newton/lib/src/simulation.dart @@ -0,0 +1,26 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +abstract class Simulatable { + /// The current position of the object in the simulation + double x(double time); + + /// The current velocity of the object in the simulation + double dx(double time); +} + +/// The base class for all simulations. The user is meant to instantiate an +/// instance of a simulation and query the same for the position and velocity +/// of the body at a given interval. +/// +/// Note: All operation on subclasses of Simulation are idempotent. Composite +/// simulations are not guaranteed to be idempotent however. FIXME(csg): How do +/// I make this apparent? +abstract class Simulation implements Simulatable { + + /// Returns if the simulation is done at a given time + bool isDone(double time); +} diff --git a/packages/newton/lib/src/spring.dart b/packages/newton/lib/src/spring.dart new file mode 100644 index 0000000000..aee59baea9 --- /dev/null +++ b/packages/newton/lib/src/spring.dart @@ -0,0 +1,46 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +class SpringDesc { + /// The mass of the spring (m) + final double mass; + + /// The spring constant (k) + final double springConstant; + + /// The damping coefficient. + /// Note: Not to be confused with the damping ratio (zeta). Use the separate + /// constructor provided for this purpose + final double damping; + + SpringDesc(this.mass, this.springConstant, this.damping); + SpringDesc.withDampingRatio(double mass, double springConstant, double zeta) + : this.mass = mass, + this.springConstant = springConstant, + this.damping = zeta * 2.0 * Math.sqrt(mass * springConstant); +} + +/// Creates a spring simulation. Depending on the spring description, a +/// critically, under or overdamped spring will be created. +class Spring extends Simulation { + final double _endPosition; + + final _SpringSolution _solution; + + /// A spring description with the provided spring description, start distance, + /// end distance and velocity. + Spring(SpringDesc desc, double start, double end, double velocity) + : this._endPosition = end, + _solution = new _SpringSolution(desc, start - end, velocity); + + double x(double time) => _endPosition + _solution.x(time); + + double dx(double time) => _solution.dx(time); + + @override + bool isDone(double time) => + _nearEqual(x(time), _endPosition) && _nearZero(dx(time)); +} diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart new file mode 100644 index 0000000000..0950da329f --- /dev/null +++ b/packages/newton/lib/src/spring_solution.dart @@ -0,0 +1,85 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +abstract class _SpringSolution implements Simulatable { + factory _SpringSolution( + SpringDesc desc, double initialPosition, double initialVelocity) { + double cmk = + desc.damping * desc.damping - 4 * desc.mass * desc.springConstant; + + if (cmk == 0.0) { + return new _CriticalSolution(desc, initialPosition, initialVelocity); + } else if (cmk > 0.0) { + return new _OverdampedSolution(desc, initialPosition, initialVelocity); + } else { + return new _UnderdampedSolution(desc, initialPosition, initialVelocity); + } + + return null; + } +} + +class _CriticalSolution implements _SpringSolution { + double r, c1, c2; + + _CriticalSolution(SpringDesc desc, double distance, double velocity) { + r = -desc.damping / (2.0 * desc.mass); + c1 = distance; + c2 = velocity / (r * distance); + } + + double x(double time) => (c1 + c2 * time) * Math.pow(Math.E, r * time); + + double dx(double time) { + final double power = Math.pow(Math.E, r * time); + return r * (c1 + c2 * time) * power + c2 * power; + } +} + +class _OverdampedSolution implements _SpringSolution { + double r1, r2, c1, c2; + + _OverdampedSolution(SpringDesc desc, double distance, double velocity) { + double cmk = + desc.damping * desc.damping - 4 * desc.mass * desc.springConstant; + + r1 = (-desc.damping - Math.sqrt(cmk)) / (2.0 * desc.mass); + r2 = (-desc.damping + Math.sqrt(cmk)) / (2.0 * desc.mass); + c2 = (velocity - r1 * distance) / (r2 - r1); + c1 = distance - c2; + } + + double x(double time) => + (c1 * Math.pow(Math.E, r1 * time) + c2 * Math.pow(Math.E, r2 * time)); + + double dx(double time) => (c1 * r1 * Math.pow(Math.E, r1 * time) + + c2 * r2 * Math.pow(Math.E, r2 * time)); +} + +class _UnderdampedSolution implements _SpringSolution { + double w, r, c1, c2; + + _UnderdampedSolution(SpringDesc desc, double distance, double velocity) { + w = Math.sqrt(4.0 * desc.mass * desc.springConstant - + desc.damping * desc.damping) / + (2.0 * desc.mass); + r = -(desc.damping / 2.0 * desc.mass); + c1 = distance; + c2 = (velocity - r * distance) / w; + } + + double x(double time) => Math.pow(Math.E, r * time) * + (c1 * Math.cos(w * time) + c2 * Math.sin(w * time)); + + double dx(double time) { + final double power = Math.pow(Math.E, r * time); + final double cosine = Math.cos(w * time); + final double sine = Math.sin(w * time); + + return power * (c2 * w * cosine - c1 * w * sine) + + r * power * (c2 * sine + c1 * cosine); + } +} diff --git a/packages/newton/lib/src/utils.dart b/packages/newton/lib/src/utils.dart new file mode 100644 index 0000000000..f3edfa2ca5 --- /dev/null +++ b/packages/newton/lib/src/utils.dart @@ -0,0 +1,12 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +const double _simulationEpsilon = 0.01; + +bool _nearEqual(double a, double b) => + (a > (b - _simulationEpsilon)) && (a < (b + _simulationEpsilon)); + +bool _nearZero(double a) => _nearEqual(a, 0.0); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart new file mode 100644 index 0000000000..fb3c343515 --- /dev/null +++ b/packages/newton/test/newton_test.dart @@ -0,0 +1,11 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +library simple_physics.test; + +import 'package:test/test.dart'; + +import 'package:newton/newton.dart'; + +void main() {} From 8c91b9b7c75b431144a3a936c0f0993452360e39 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 10:57:26 -0700 Subject: [PATCH 02/21] Implement simulation groups for kinetic scrolling --- packages/newton/lib/newton.dart | 2 +- packages/newton/lib/src/fall.dart | 8 -- packages/newton/lib/src/scroll.dart | 47 +++++++++- packages/newton/lib/src/simulation.dart | 4 +- packages/newton/lib/src/simulation_group.dart | 42 +++++++++ packages/newton/lib/src/spring_solution.dart | 88 ++++++++++++------- 6 files changed, 145 insertions(+), 46 deletions(-) delete mode 100644 packages/newton/lib/src/fall.dart create mode 100644 packages/newton/lib/src/simulation_group.dart diff --git a/packages/newton/lib/newton.dart b/packages/newton/lib/newton.dart index bc2de1a90f..744fe3135f 100644 --- a/packages/newton/lib/newton.dart +++ b/packages/newton/lib/newton.dart @@ -7,9 +7,9 @@ library newton; import 'dart:math' as Math; part 'src/simulation.dart'; +part 'src/simulation_group.dart'; part 'src/utils.dart'; -part 'src/fall.dart'; part 'src/friction.dart'; part 'src/gravity.dart'; part 'src/scroll.dart'; diff --git a/packages/newton/lib/src/fall.dart b/packages/newton/lib/src/fall.dart deleted file mode 100644 index ca5ec9556b..0000000000 --- a/packages/newton/lib/src/fall.dart +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -part of newton; - -// TODO(csg): Composite simulation -class Fall {} diff --git a/packages/newton/lib/src/scroll.dart b/packages/newton/lib/src/scroll.dart index 12f9f4957a..c4a9fe6a74 100644 --- a/packages/newton/lib/src/scroll.dart +++ b/packages/newton/lib/src/scroll.dart @@ -4,5 +4,48 @@ part of newton; -// TODO(csg): Composite simulation -class Scroll {} +/// Simulates kinetic scrolling behavior between a leading and trailing +/// boundary. Friction is applied within the extends and a spring action applied +/// at the boundaries. This simulation can only step forward. +class Scroll extends SimulationGroup { + final double _leadingExtent; + final double _trailingExtent; + final SpringDesc _springDesc; + final double _drag; + + bool _isSpringing = false; + Simulation _currentSimulation; + + Scroll(double position, double velocity, double leading, double trailing, + SpringDesc spring, double drag) + : _leadingExtent = leading, + _trailingExtent = trailing, + _springDesc = spring, + _drag = drag { + _chooseSimulation(position, velocity); + } + + @override + void step(double time) => _chooseSimulation( + _currentSimulation.x(time), _currentSimulation.dx(time)); + + @override + Simulation get currentSimulation => _currentSimulation; + + void _chooseSimulation(double position, double velocity) { + /// This simulation can only step forward + if (!_isSpringing) { + if (position > _trailingExtent) { + _isSpringing = true; + _currentSimulation = + new Spring(_springDesc, position, _trailingExtent, velocity); + } else if (position < _leadingExtent) { + _isSpringing = true; + _currentSimulation = + new Spring(_springDesc, position, _leadingExtent, velocity); + } + } else if (_currentSimulation == null) { + _currentSimulation = new Friction(_drag, position, velocity); + } + } +} diff --git a/packages/newton/lib/src/simulation.dart b/packages/newton/lib/src/simulation.dart index 12b6b926c2..ca06f4811a 100644 --- a/packages/newton/lib/src/simulation.dart +++ b/packages/newton/lib/src/simulation.dart @@ -16,9 +16,7 @@ abstract class Simulatable { /// instance of a simulation and query the same for the position and velocity /// of the body at a given interval. /// -/// Note: All operation on subclasses of Simulation are idempotent. Composite -/// simulations are not guaranteed to be idempotent however. FIXME(csg): How do -/// I make this apparent? +/// Note: All operations on subclasses of Simulation are idempotent. abstract class Simulation implements Simulatable { /// Returns if the simulation is done at a given time diff --git a/packages/newton/lib/src/simulation_group.dart b/packages/newton/lib/src/simulation_group.dart new file mode 100644 index 0000000000..29f9311b4f --- /dev/null +++ b/packages/newton/lib/src/simulation_group.dart @@ -0,0 +1,42 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +/// The abstract base class of all composite simulations. Concrete subclasses +/// must implement the appropriate methods to select the appropriate simulation +/// at a given time interval. The simulation group takes care to call the `step` +/// method at appropriate intervals. If more fine grained control over the the +/// step is necessary, subclasses may override the `Simulatable` methods. +abstract class SimulationGroup extends Simulation { + Simulation get currentSimulation; + + void step(double time); + + double x(double time) { + _stepIfNecessary(time); + return currentSimulation.x(time); + } + + double dx(double time) { + _stepIfNecessary(time); + return currentSimulation.dx(time); + } + + @override + bool isDone(double time) { + _stepIfNecessary(time); + return currentSimulation.isDone(time); + } + + double _lastStep = -1.0; + void _stepIfNecessary(double time) { + if (_nearEqual(_lastStep, time)) { + return; + } + + _lastStep = time; + step(time); + } +} diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart index 0950da329f..511b32a293 100644 --- a/packages/newton/lib/src/spring_solution.dart +++ b/packages/newton/lib/src/spring_solution.dart @@ -23,63 +23,87 @@ abstract class _SpringSolution implements Simulatable { } class _CriticalSolution implements _SpringSolution { - double r, c1, c2; + final double _r, _c1, _c2; - _CriticalSolution(SpringDesc desc, double distance, double velocity) { - r = -desc.damping / (2.0 * desc.mass); - c1 = distance; - c2 = velocity / (r * distance); + factory _CriticalSolution(SpringDesc desc, double distance, double velocity) { + final double r = -desc.damping / (2.0 * desc.mass); + final double c1 = distance; + final double c2 = velocity / (r * distance); + return new _CriticalSolution.withArgs(r, c1, c2); } - double x(double time) => (c1 + c2 * time) * Math.pow(Math.E, r * time); + _CriticalSolution.withArgs(double r, double c1, double c2) + : _r = r, + _c1 = c1, + _c2 = c2; + + double x(double time) => (_c1 + _c2 * time) * Math.pow(Math.E, _r * time); double dx(double time) { - final double power = Math.pow(Math.E, r * time); - return r * (c1 + c2 * time) * power + c2 * power; + final double power = Math.pow(Math.E, _r * time); + return _r * (_c1 + _c2 * time) * power + _c2 * power; } } class _OverdampedSolution implements _SpringSolution { - double r1, r2, c1, c2; + final double _r1, _r2, _c1, _c2; - _OverdampedSolution(SpringDesc desc, double distance, double velocity) { - double cmk = + factory _OverdampedSolution( + SpringDesc desc, double distance, double velocity) { + final double cmk = desc.damping * desc.damping - 4 * desc.mass * desc.springConstant; - r1 = (-desc.damping - Math.sqrt(cmk)) / (2.0 * desc.mass); - r2 = (-desc.damping + Math.sqrt(cmk)) / (2.0 * desc.mass); - c2 = (velocity - r1 * distance) / (r2 - r1); - c1 = distance - c2; + final double r1 = (-desc.damping - Math.sqrt(cmk)) / (2.0 * desc.mass); + final double r2 = (-desc.damping + Math.sqrt(cmk)) / (2.0 * desc.mass); + final double c2 = (velocity - r1 * distance) / (r2 - r1); + final double c1 = distance - c2; + + return new _OverdampedSolution.withArgs(r1, r2, c1, c2); } - double x(double time) => - (c1 * Math.pow(Math.E, r1 * time) + c2 * Math.pow(Math.E, r2 * time)); + _OverdampedSolution.withArgs(double r1, double r2, double c1, double c2) + : _r1 = r1, + _r2 = r2, + _c1 = c1, + _c2 = c2; - double dx(double time) => (c1 * r1 * Math.pow(Math.E, r1 * time) + - c2 * r2 * Math.pow(Math.E, r2 * time)); + double x(double time) => + (_c1 * Math.pow(Math.E, _r1 * time) + _c2 * Math.pow(Math.E, _r2 * time)); + + double dx(double time) => (_c1 * _r1 * Math.pow(Math.E, _r1 * time) + + _c2 * _r2 * Math.pow(Math.E, _r2 * time)); } class _UnderdampedSolution implements _SpringSolution { - double w, r, c1, c2; + final double _w, _r, _c1, _c2; - _UnderdampedSolution(SpringDesc desc, double distance, double velocity) { - w = Math.sqrt(4.0 * desc.mass * desc.springConstant - + factory _UnderdampedSolution( + SpringDesc desc, double distance, double velocity) { + final double w = Math.sqrt(4.0 * desc.mass * desc.springConstant - desc.damping * desc.damping) / (2.0 * desc.mass); - r = -(desc.damping / 2.0 * desc.mass); - c1 = distance; - c2 = (velocity - r * distance) / w; + final double r = -(desc.damping / 2.0 * desc.mass); + final double c1 = distance; + final double c2 = (velocity - r * distance) / w; + + return new _UnderdampedSolution.withArgs(w, r, c1, c2); } - double x(double time) => Math.pow(Math.E, r * time) * - (c1 * Math.cos(w * time) + c2 * Math.sin(w * time)); + _UnderdampedSolution.withArgs(double w, double r, double c1, double c2) + : _w = w, + _r = r, + _c1 = c1, + _c2 = c2; + + double x(double time) => Math.pow(Math.E, _r * time) * + (_c1 * Math.cos(_w * time) + _c2 * Math.sin(_w * time)); double dx(double time) { - final double power = Math.pow(Math.E, r * time); - final double cosine = Math.cos(w * time); - final double sine = Math.sin(w * time); + final double power = Math.pow(Math.E, _r * time); + final double cosine = Math.cos(_w * time); + final double sine = Math.sin(_w * time); - return power * (c2 * w * cosine - c1 * w * sine) + - r * power * (c2 * sine + c1 * cosine); + return power * (_c2 * _w * cosine - _c1 * _w * sine) + + _r * power * (_c2 * sine + _c1 * cosine); } } From 30965c09842fa04da9942f1479347fde1df9f84a Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 12:19:37 -0700 Subject: [PATCH 03/21] Test Friction simulation --- packages/newton/lib/src/friction.dart | 2 +- packages/newton/test/newton_test.dart | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/newton/lib/src/friction.dart b/packages/newton/lib/src/friction.dart index c0d2320982..a055ac7d03 100644 --- a/packages/newton/lib/src/friction.dart +++ b/packages/newton/lib/src/friction.dart @@ -17,7 +17,7 @@ class Friction extends Simulation { _v = velocity; double x(double time) => - _x + _v + Math.pow(_drag, time) / _dragNaturalLog - _v / _dragNaturalLog; + _x + _v * Math.pow(_drag, time) / _dragNaturalLog - _v / _dragNaturalLog; double dx(double time) => _v * Math.pow(_drag, time); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index fb3c343515..f3d94d2378 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -8,4 +8,24 @@ import 'package:test/test.dart'; import 'package:newton/newton.dart'; -void main() {} +typedef bool SimulationTestHandler(int millis); + +void main() { + test('test_friction', () { + var friction = new Friction(0.3, 100.0, 400.0); + + expect(friction.isDone(0.0), false); + expect(friction.x(0.0), 100); + expect(friction.dx(0.0), 400.0); + + expect(friction.x(1.0) > 330 && friction.x(1.0) < 335, true); + + expect(friction.dx(1.0), 120.0); + expect(friction.dx(2.0), 36.0); + expect(friction.dx(3.0), 10.8); + expect(friction.dx(4.0) < 3.5, true); + + expect(friction.isDone(5.0), true); + expect(friction.x(5.0) > 431 && friction.x(5.0) < 432, true); + }); +} From bcf1f8d05e3182360dc00abb9ae72306d874475f Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 12:33:21 -0700 Subject: [PATCH 04/21] Test gravity --- packages/newton/test/newton_test.dart | 32 +++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index f3d94d2378..de86bbe61d 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -8,8 +8,6 @@ import 'package:test/test.dart'; import 'package:newton/newton.dart'; -typedef bool SimulationTestHandler(int millis); - void main() { test('test_friction', () { var friction = new Friction(0.3, 100.0, 400.0); @@ -28,4 +26,34 @@ void main() { expect(friction.isDone(5.0), true); expect(friction.x(5.0) > 431 && friction.x(5.0) < 432, true); }); + + test('test_gravity', () { + var gravity = new Gravity(200.0, 100.0, 600.0, 0.0); + + expect(gravity.isDone(0.0), false); + expect(gravity.x(0.0), 100.0); + expect(gravity.dx(0.0), 0.0); + + // Starts at 100 + expect(gravity.x(0.25), 106.25); + expect(gravity.x(0.50), 125); + expect(gravity.x(0.75), 156.25); + expect(gravity.x(1.00), 200); + expect(gravity.x(1.25), 256.25); + expect(gravity.x(1.50), 325); + expect(gravity.x(1.75), 406.25); + + // Starts at 0.0 + expect(gravity.dx(0.25), 50.0); + expect(gravity.dx(0.50), 100); + expect(gravity.dx(0.75), 150.00); + expect(gravity.dx(1.00), 200.0); + expect(gravity.dx(1.25), 250.0); + expect(gravity.dx(1.50), 300); + expect(gravity.dx(1.75), 350); + + expect(gravity.isDone(2.5), true); + expect(gravity.x(2.5), 725); + expect(gravity.dx(2.5), 500.0); + }); } From ab7a6dd6c2e9d69cac19b4317a99a2823d067247 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 12:48:52 -0700 Subject: [PATCH 05/21] Add accessors for spring type --- packages/newton/lib/src/spring.dart | 9 +++++++++ packages/newton/lib/src/spring_solution.dart | 11 +++++++++++ packages/newton/test/newton_test.dart | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/packages/newton/lib/src/spring.dart b/packages/newton/lib/src/spring.dart index aee59baea9..f23ce27b13 100644 --- a/packages/newton/lib/src/spring.dart +++ b/packages/newton/lib/src/spring.dart @@ -17,12 +17,19 @@ class SpringDesc { final double damping; SpringDesc(this.mass, this.springConstant, this.damping); + + /// Create a spring given the mass, spring constant and the damping ratio. The + /// damping ratio is especially useful trying to determing the type of spring + /// to create. A ratio of 1.0 creates a critically damped spring, > 1.0 + /// creates an overdamped spring and < 1.0 an underdamped one. SpringDesc.withDampingRatio(double mass, double springConstant, double zeta) : this.mass = mass, this.springConstant = springConstant, this.damping = zeta * 2.0 * Math.sqrt(mass * springConstant); } +enum SpringType { unknown, criticallyDamped, underDamped, overDamped, } + /// Creates a spring simulation. Depending on the spring description, a /// critically, under or overdamped spring will be created. class Spring extends Simulation { @@ -36,6 +43,8 @@ class Spring extends Simulation { : this._endPosition = end, _solution = new _SpringSolution(desc, start - end, velocity); + SpringType get type => _solution.type; + double x(double time) => _endPosition + _solution.x(time); double dx(double time) => _solution.dx(time); diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart index 511b32a293..fe57f7c08b 100644 --- a/packages/newton/lib/src/spring_solution.dart +++ b/packages/newton/lib/src/spring_solution.dart @@ -20,6 +20,8 @@ abstract class _SpringSolution implements Simulatable { return null; } + + SpringType get type; } class _CriticalSolution implements _SpringSolution { @@ -32,6 +34,9 @@ class _CriticalSolution implements _SpringSolution { return new _CriticalSolution.withArgs(r, c1, c2); } + @override + SpringType get type => SpringType.criticallyDamped; + _CriticalSolution.withArgs(double r, double c1, double c2) : _r = r, _c1 = c1, @@ -67,6 +72,9 @@ class _OverdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; + @override + SpringType get type => SpringType.overDamped; + double x(double time) => (_c1 * Math.pow(Math.E, _r1 * time) + _c2 * Math.pow(Math.E, _r2 * time)); @@ -95,6 +103,9 @@ class _UnderdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; + @override + SpringType get type => SpringType.underDamped; + double x(double time) => Math.pow(Math.E, _r * time) * (_c1 * Math.cos(_w * time) + _c2 * Math.sin(_w * time)); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index de86bbe61d..54c7daa223 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -56,4 +56,22 @@ void main() { expect(gravity.x(2.5), 725); expect(gravity.dx(2.5), 500.0); }); + + test('spring_types', () { + var crit = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 300.0, 0.0); + expect(crit.type, SpringType.criticallyDamped); + + var under = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 0.75), 0.0, 300.0, 0.0); + expect(under.type, SpringType.underDamped); + + var over = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 300.0, 0.0); + expect(over.type, SpringType.overDamped); + + // Just so we don't forget how to create a desc without the ratio. + var other = new Spring(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); + expect(other.type, SpringType.criticallyDamped); + }); } From c3aaf8aa338d17c9b940dd8b75d8129a43aa0618 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 13:30:53 -0700 Subject: [PATCH 06/21] Test individial spring types --- packages/newton/lib/src/spring.dart | 2 +- packages/newton/test/newton_test.dart | 58 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/newton/lib/src/spring.dart b/packages/newton/lib/src/spring.dart index f23ce27b13..bda8242d20 100644 --- a/packages/newton/lib/src/spring.dart +++ b/packages/newton/lib/src/spring.dart @@ -12,7 +12,7 @@ class SpringDesc { final double springConstant; /// The damping coefficient. - /// Note: Not to be confused with the damping ratio (zeta). Use the separate + /// Note: Not to be confused with the damping ratio. Use the separate /// constructor provided for this purpose final double damping; diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index 54c7daa223..8ef74e357f 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -74,4 +74,62 @@ void main() { var other = new Spring(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); expect(other.type, SpringType.criticallyDamped); }); + + test('crit_spring', () { + var crit = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 500.0, 0.0); + expect(crit.type, SpringType.criticallyDamped); + + expect(crit.isDone(0.0), false); + expect(crit.x(0.0), 0.0); + expect(crit.dx(0.0), 5000.0); + + expect(crit.x(0.25).floor(), 458.0); + expect(crit.x(0.50).floor(), 496.0); + expect(crit.x(0.75).floor(), 499.0); + + expect(crit.dx(0.25).floor(), 410); + expect(crit.dx(0.50).floor(), 33); + expect(crit.dx(0.75).floor(), 2); + + expect(crit.isDone(1.50), true); + expect(crit.x(1.5) > 499.0 && crit.x(1.5) < 501.0, true); + expect(crit.dx(1.5) < 0.1, true /* basically within tolerance */); + }); + + test('overdamped_spring', () { + var over = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 500.0, 0.0); + expect(over.type, SpringType.overDamped); + + expect(over.isDone(0.0), false); + expect(over.x(0.0), 0.0); + + expect(over.x(0.5).floor(), 445.0); + expect(over.x(1.0).floor(), 495.0); + expect(over.x(1.5).floor(), 499.0); + + expect(over.dx(0.5).floor(), 273.0); + expect(over.dx(1.0).floor(), 22.0); + expect(over.dx(1.5).floor(), 1.0); + + expect(over.isDone(3.0), true); + }); + + test('underdamped_spring', () { + var under = new Spring( + new SpringDesc.withDampingRatio(1.0, 100.0, 0.25), 0.0, 300.0, 0.0); + expect(under.type, SpringType.underDamped); + + expect(under.isDone(0.0), false); + + // Overshot with negative velocity + expect(under.x(1.0).floor(), 325); + expect(under.dx(1.0).floor(), -65); + + expect(under.dx(6.0).floor(), 0.0); + expect(under.x(6.0).floor(), 299); + + expect(under.isDone(6.0), true); + }); } From 9932e9f03a6bdfdf39ab611bc8cd9701d039aca4 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 14:03:10 -0700 Subject: [PATCH 07/21] Test kinetic scrolling group simulation --- packages/newton/lib/src/scroll.dart | 24 +++++++++++++++---- packages/newton/lib/src/simulation_group.dart | 15 ++++++++---- packages/newton/lib/src/spring_solution.dart | 3 --- packages/newton/test/newton_test.dart | 12 ++++++++++ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/newton/lib/src/scroll.dart b/packages/newton/lib/src/scroll.dart index c4a9fe6a74..4b57b9f0af 100644 --- a/packages/newton/lib/src/scroll.dart +++ b/packages/newton/lib/src/scroll.dart @@ -15,6 +15,7 @@ class Scroll extends SimulationGroup { bool _isSpringing = false; Simulation _currentSimulation; + double _offset = 0.0; Scroll(double position, double velocity, double leading, double trailing, SpringDesc spring, double drag) @@ -22,30 +23,43 @@ class Scroll extends SimulationGroup { _trailingExtent = trailing, _springDesc = spring, _drag = drag { - _chooseSimulation(position, velocity); + _chooseSimulation(position, velocity, 0.0); } @override void step(double time) => _chooseSimulation( - _currentSimulation.x(time), _currentSimulation.dx(time)); + _currentSimulation.x(time - _offset), + _currentSimulation.dx(time - _offset), time); @override Simulation get currentSimulation => _currentSimulation; - void _chooseSimulation(double position, double velocity) { - /// This simulation can only step forward + @override + double get currentIntervalOffset => _offset; + + void _chooseSimulation( + double position, double velocity, double intervalOffset) { + + /// This simulation can only step forward. if (!_isSpringing) { if (position > _trailingExtent) { _isSpringing = true; + _offset = intervalOffset; _currentSimulation = new Spring(_springDesc, position, _trailingExtent, velocity); + return; } else if (position < _leadingExtent) { _isSpringing = true; + _offset = intervalOffset; _currentSimulation = new Spring(_springDesc, position, _leadingExtent, velocity); + return; } - } else if (_currentSimulation == null) { + } + + if (_currentSimulation == null) { _currentSimulation = new Friction(_drag, position, velocity); + return; } } } diff --git a/packages/newton/lib/src/simulation_group.dart b/packages/newton/lib/src/simulation_group.dart index 29f9311b4f..9fb6f9c365 100644 --- a/packages/newton/lib/src/simulation_group.dart +++ b/packages/newton/lib/src/simulation_group.dart @@ -4,24 +4,31 @@ part of newton; -/// The abstract base class of all composite simulations. Concrete subclasses +/// The abstract base class for all composite simulations. Concrete subclasses /// must implement the appropriate methods to select the appropriate simulation /// at a given time interval. The simulation group takes care to call the `step` /// method at appropriate intervals. If more fine grained control over the the -/// step is necessary, subclasses may override the `Simulatable` methods. +/// step is necessary, subclasses may override `Simulatable` methods. abstract class SimulationGroup extends Simulation { + + /// The currently active simulation Simulation get currentSimulation; + /// The time offset applied to the currently active simulation; + double get currentIntervalOffset; + + /// Called when a significant change in the interval is detected. Subclasses + /// must decide if the the current simulation must be switched (or updated). void step(double time); double x(double time) { _stepIfNecessary(time); - return currentSimulation.x(time); + return currentSimulation.x(time - currentIntervalOffset); } double dx(double time) { _stepIfNecessary(time); - return currentSimulation.dx(time); + return currentSimulation.dx(time - currentIntervalOffset); } @override diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart index fe57f7c08b..7363a5d784 100644 --- a/packages/newton/lib/src/spring_solution.dart +++ b/packages/newton/lib/src/spring_solution.dart @@ -34,7 +34,6 @@ class _CriticalSolution implements _SpringSolution { return new _CriticalSolution.withArgs(r, c1, c2); } - @override SpringType get type => SpringType.criticallyDamped; _CriticalSolution.withArgs(double r, double c1, double c2) @@ -72,7 +71,6 @@ class _OverdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; - @override SpringType get type => SpringType.overDamped; double x(double time) => @@ -103,7 +101,6 @@ class _UnderdampedSolution implements _SpringSolution { _c1 = c1, _c2 = c2; - @override SpringType get type => SpringType.underDamped; double x(double time) => Math.pow(Math.E, _r * time) * diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index 8ef74e357f..a568848eab 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -132,4 +132,16 @@ void main() { expect(under.isDone(6.0), true); }); + + test('test_kinetic_scroll', () { + var spring = new SpringDesc.withDampingRatio(1.0, 50.0, 0.5); + + var scroll = new Scroll(100.0, 800.0, 0.0, 300.0, spring, 0.3); + + expect(scroll.isDone(0.0), false); + expect(scroll.isDone(3.5), true); + + var scroll2 = new Scroll(100.0, -800.0, 0.0, 300.0, spring, 0.3); + expect(scroll2.isDone(4.5), true); + }); } From fc098d8c65d30721d988d6d440adf2a58ac99a57 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 14:11:41 -0700 Subject: [PATCH 08/21] Rename concrete simulation subclasses --- packages/newton/lib/newton.dart | 8 +++---- ...friction.dart => friction_simulation.dart} | 10 ++++---- .../{gravity.dart => gravity_simulation.dart} | 4 ++-- .../{scroll.dart => scroll_simulation.dart} | 16 ++++++------- .../{spring.dart => spring_simulation.dart} | 4 ++-- packages/newton/test/newton_test.dart | 23 ++++++++++--------- 6 files changed, 33 insertions(+), 32 deletions(-) rename packages/newton/lib/src/{friction.dart => friction_simulation.dart} (65%) rename packages/newton/lib/src/{gravity.dart => gravity_simulation.dart} (90%) rename packages/newton/lib/src/{scroll.dart => scroll_simulation.dart} (76%) rename packages/newton/lib/src/{spring.dart => spring_simulation.dart} (93%) diff --git a/packages/newton/lib/newton.dart b/packages/newton/lib/newton.dart index 744fe3135f..03eaa834b4 100644 --- a/packages/newton/lib/newton.dart +++ b/packages/newton/lib/newton.dart @@ -10,8 +10,8 @@ part 'src/simulation.dart'; part 'src/simulation_group.dart'; part 'src/utils.dart'; -part 'src/friction.dart'; -part 'src/gravity.dart'; -part 'src/scroll.dart'; -part 'src/spring.dart'; +part 'src/friction_simulation.dart'; +part 'src/gravity_simulation.dart'; +part 'src/scroll_simulation.dart'; +part 'src/spring_simulation.dart'; part 'src/spring_solution.dart'; diff --git a/packages/newton/lib/src/friction.dart b/packages/newton/lib/src/friction_simulation.dart similarity index 65% rename from packages/newton/lib/src/friction.dart rename to packages/newton/lib/src/friction_simulation.dart index a055ac7d03..3916456e2d 100644 --- a/packages/newton/lib/src/friction.dart +++ b/packages/newton/lib/src/friction_simulation.dart @@ -4,20 +4,20 @@ part of newton; -class Friction extends Simulation { +class FrictionSimulation extends Simulation { final double _drag; - final double _dragNaturalLog; + final double _dragLog; final double _x; final double _v; - Friction(double drag, double position, double velocity) + FrictionSimulation(double drag, double position, double velocity) : _drag = drag, - _dragNaturalLog = Math.log(drag), + _dragLog = Math.log(drag), _x = position, _v = velocity; double x(double time) => - _x + _v * Math.pow(_drag, time) / _dragNaturalLog - _v / _dragNaturalLog; + _x + _v * Math.pow(_drag, time) / _dragLog - _v / _dragLog; double dx(double time) => _v * Math.pow(_drag, time); diff --git a/packages/newton/lib/src/gravity.dart b/packages/newton/lib/src/gravity_simulation.dart similarity index 90% rename from packages/newton/lib/src/gravity.dart rename to packages/newton/lib/src/gravity_simulation.dart index 4eb11f69d4..2f002b74e0 100644 --- a/packages/newton/lib/src/gravity.dart +++ b/packages/newton/lib/src/gravity_simulation.dart @@ -4,13 +4,13 @@ part of newton; -class Gravity extends Simulation { +class GravitySimulation extends Simulation { final double _x; final double _v; final double _a; final double _end; - Gravity( + GravitySimulation( double acceleration, double distance, double endDistance, double velocity) : _a = acceleration, _x = distance, diff --git a/packages/newton/lib/src/scroll.dart b/packages/newton/lib/src/scroll_simulation.dart similarity index 76% rename from packages/newton/lib/src/scroll.dart rename to packages/newton/lib/src/scroll_simulation.dart index 4b57b9f0af..a1f7d59fb5 100644 --- a/packages/newton/lib/src/scroll.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -7,7 +7,7 @@ part of newton; /// Simulates kinetic scrolling behavior between a leading and trailing /// boundary. Friction is applied within the extends and a spring action applied /// at the boundaries. This simulation can only step forward. -class Scroll extends SimulationGroup { +class ScrollSimulation extends SimulationGroup { final double _leadingExtent; final double _trailingExtent; final SpringDesc _springDesc; @@ -17,8 +17,8 @@ class Scroll extends SimulationGroup { Simulation _currentSimulation; double _offset = 0.0; - Scroll(double position, double velocity, double leading, double trailing, - SpringDesc spring, double drag) + ScrollSimulation(double position, double velocity, double leading, + double trailing, SpringDesc spring, double drag) : _leadingExtent = leading, _trailingExtent = trailing, _springDesc = spring, @@ -45,20 +45,20 @@ class Scroll extends SimulationGroup { if (position > _trailingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = - new Spring(_springDesc, position, _trailingExtent, velocity); + _currentSimulation = new SpringSimulation( + _springDesc, position, _trailingExtent, velocity); return; } else if (position < _leadingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = - new Spring(_springDesc, position, _leadingExtent, velocity); + _currentSimulation = new SpringSimulation( + _springDesc, position, _leadingExtent, velocity); return; } } if (_currentSimulation == null) { - _currentSimulation = new Friction(_drag, position, velocity); + _currentSimulation = new FrictionSimulation(_drag, position, velocity); return; } } diff --git a/packages/newton/lib/src/spring.dart b/packages/newton/lib/src/spring_simulation.dart similarity index 93% rename from packages/newton/lib/src/spring.dart rename to packages/newton/lib/src/spring_simulation.dart index bda8242d20..9d80bf094d 100644 --- a/packages/newton/lib/src/spring.dart +++ b/packages/newton/lib/src/spring_simulation.dart @@ -32,14 +32,14 @@ enum SpringType { unknown, criticallyDamped, underDamped, overDamped, } /// Creates a spring simulation. Depending on the spring description, a /// critically, under or overdamped spring will be created. -class Spring extends Simulation { +class SpringSimulation extends Simulation { final double _endPosition; final _SpringSolution _solution; /// A spring description with the provided spring description, start distance, /// end distance and velocity. - Spring(SpringDesc desc, double start, double end, double velocity) + SpringSimulation(SpringDesc desc, double start, double end, double velocity) : this._endPosition = end, _solution = new _SpringSolution(desc, start - end, velocity); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index a568848eab..275aa81ca6 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -10,7 +10,7 @@ import 'package:newton/newton.dart'; void main() { test('test_friction', () { - var friction = new Friction(0.3, 100.0, 400.0); + var friction = new FrictionSimulation(0.3, 100.0, 400.0); expect(friction.isDone(0.0), false); expect(friction.x(0.0), 100); @@ -28,7 +28,7 @@ void main() { }); test('test_gravity', () { - var gravity = new Gravity(200.0, 100.0, 600.0, 0.0); + var gravity = new GravitySimulation(200.0, 100.0, 600.0, 0.0); expect(gravity.isDone(0.0), false); expect(gravity.x(0.0), 100.0); @@ -58,25 +58,26 @@ void main() { }); test('spring_types', () { - var crit = new Spring( + var crit = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 300.0, 0.0); expect(crit.type, SpringType.criticallyDamped); - var under = new Spring( + var under = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 0.75), 0.0, 300.0, 0.0); expect(under.type, SpringType.underDamped); - var over = new Spring( + var over = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 300.0, 0.0); expect(over.type, SpringType.overDamped); // Just so we don't forget how to create a desc without the ratio. - var other = new Spring(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); + var other = + new SpringSimulation(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); expect(other.type, SpringType.criticallyDamped); }); test('crit_spring', () { - var crit = new Spring( + var crit = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 500.0, 0.0); expect(crit.type, SpringType.criticallyDamped); @@ -98,7 +99,7 @@ void main() { }); test('overdamped_spring', () { - var over = new Spring( + var over = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 500.0, 0.0); expect(over.type, SpringType.overDamped); @@ -117,7 +118,7 @@ void main() { }); test('underdamped_spring', () { - var under = new Spring( + var under = new SpringSimulation( new SpringDesc.withDampingRatio(1.0, 100.0, 0.25), 0.0, 300.0, 0.0); expect(under.type, SpringType.underDamped); @@ -136,12 +137,12 @@ void main() { test('test_kinetic_scroll', () { var spring = new SpringDesc.withDampingRatio(1.0, 50.0, 0.5); - var scroll = new Scroll(100.0, 800.0, 0.0, 300.0, spring, 0.3); + var scroll = new ScrollSimulation(100.0, 800.0, 0.0, 300.0, spring, 0.3); expect(scroll.isDone(0.0), false); expect(scroll.isDone(3.5), true); - var scroll2 = new Scroll(100.0, -800.0, 0.0, 300.0, spring, 0.3); + var scroll2 = new ScrollSimulation(100.0, -800.0, 0.0, 300.0, spring, 0.3); expect(scroll2.isDone(4.5), true); }); } From be5e52bc7c79d798d61032820dae7464bb6ebd1a Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 6 Jul 2015 14:31:21 -0700 Subject: [PATCH 09/21] Add build status and coverage badges --- packages/newton/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/newton/README.md b/packages/newton/README.md index 0562d23ed7..731a9abcaa 100644 --- a/packages/newton/README.md +++ b/packages/newton/README.md @@ -1,3 +1,5 @@ # Newton +[![Build Status](https://travis-ci.org/domokit/newton.svg?branch=master)](https://travis-ci.org/domokit/newton) +[![Coverage Status](https://coveralls.io/repos/domokit/newton/badge.svg?branch=master)](https://coveralls.io/r/domokit/newton?branch=master) Simple Physics Simulations for Dart. Springs, friction, gravity, etc. From 7b77043eba73fd4eaddc16991ef1c6602a886be0 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 8 Jul 2015 17:56:14 -0700 Subject: [PATCH 10/21] Newton: Address initial code review concerns --- packages/newton/lib/newton.dart | 2 +- .../newton/lib/src/friction_simulation.dart | 6 ++-- .../newton/lib/src/scroll_simulation.dart | 4 +-- .../newton/lib/src/spring_simulation.dart | 23 ++++++++---- packages/newton/lib/src/spring_solution.dart | 35 +++++++++--------- packages/newton/test/newton_test.dart | 36 +++++++++++-------- 6 files changed, 61 insertions(+), 45 deletions(-) diff --git a/packages/newton/lib/newton.dart b/packages/newton/lib/newton.dart index 03eaa834b4..47de84a790 100644 --- a/packages/newton/lib/newton.dart +++ b/packages/newton/lib/newton.dart @@ -4,7 +4,7 @@ library newton; -import 'dart:math' as Math; +import 'dart:math' as math; part 'src/simulation.dart'; part 'src/simulation_group.dart'; diff --git a/packages/newton/lib/src/friction_simulation.dart b/packages/newton/lib/src/friction_simulation.dart index 3916456e2d..3aa9ce8d0f 100644 --- a/packages/newton/lib/src/friction_simulation.dart +++ b/packages/newton/lib/src/friction_simulation.dart @@ -12,14 +12,14 @@ class FrictionSimulation extends Simulation { FrictionSimulation(double drag, double position, double velocity) : _drag = drag, - _dragLog = Math.log(drag), + _dragLog = math.log(drag), _x = position, _v = velocity; double x(double time) => - _x + _v * Math.pow(_drag, time) / _dragLog - _v / _dragLog; + _x + _v * math.pow(_drag, time) / _dragLog - _v / _dragLog; - double dx(double time) => _v * Math.pow(_drag, time); + double dx(double time) => _v * math.pow(_drag, time); @override bool isDone(double time) => dx(time).abs() < 1.0; diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index a1f7d59fb5..123e8d1b41 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -10,7 +10,7 @@ part of newton; class ScrollSimulation extends SimulationGroup { final double _leadingExtent; final double _trailingExtent; - final SpringDesc _springDesc; + final SpringDescription _springDesc; final double _drag; bool _isSpringing = false; @@ -18,7 +18,7 @@ class ScrollSimulation extends SimulationGroup { double _offset = 0.0; ScrollSimulation(double position, double velocity, double leading, - double trailing, SpringDesc spring, double drag) + double trailing, SpringDescription spring, double drag) : _leadingExtent = leading, _trailingExtent = trailing, _springDesc = spring, diff --git a/packages/newton/lib/src/spring_simulation.dart b/packages/newton/lib/src/spring_simulation.dart index 9d80bf094d..3ca6a846f8 100644 --- a/packages/newton/lib/src/spring_simulation.dart +++ b/packages/newton/lib/src/spring_simulation.dart @@ -4,7 +4,7 @@ part of newton; -class SpringDesc { +class SpringDescription { /// The mass of the spring (m) final double mass; @@ -16,16 +16,24 @@ class SpringDesc { /// constructor provided for this purpose final double damping; - SpringDesc(this.mass, this.springConstant, this.damping); + SpringDescription({double mass, double springConstant, double damping}) + : mass = mass, + springConstant = springConstant, + damping = damping { + assert(mass != null); + assert(springConstant != null); + assert(damping != null); + } /// Create a spring given the mass, spring constant and the damping ratio. The /// damping ratio is especially useful trying to determing the type of spring /// to create. A ratio of 1.0 creates a critically damped spring, > 1.0 /// creates an overdamped spring and < 1.0 an underdamped one. - SpringDesc.withDampingRatio(double mass, double springConstant, double zeta) - : this.mass = mass, - this.springConstant = springConstant, - this.damping = zeta * 2.0 * Math.sqrt(mass * springConstant); + SpringDescription.withDampingRatio( + {double mass, double springConstant, double ratio: 1.0}) + : mass = mass, + springConstant = springConstant, + damping = ratio * 2.0 * math.sqrt(mass * springConstant); } enum SpringType { unknown, criticallyDamped, underDamped, overDamped, } @@ -39,7 +47,8 @@ class SpringSimulation extends Simulation { /// A spring description with the provided spring description, start distance, /// end distance and velocity. - SpringSimulation(SpringDesc desc, double start, double end, double velocity) + SpringSimulation( + SpringDescription desc, double start, double end, double velocity) : this._endPosition = end, _solution = new _SpringSolution(desc, start - end, velocity); diff --git a/packages/newton/lib/src/spring_solution.dart b/packages/newton/lib/src/spring_solution.dart index 7363a5d784..3241311c60 100644 --- a/packages/newton/lib/src/spring_solution.dart +++ b/packages/newton/lib/src/spring_solution.dart @@ -6,7 +6,7 @@ part of newton; abstract class _SpringSolution implements Simulatable { factory _SpringSolution( - SpringDesc desc, double initialPosition, double initialVelocity) { + SpringDescription desc, double initialPosition, double initialVelocity) { double cmk = desc.damping * desc.damping - 4 * desc.mass * desc.springConstant; @@ -27,7 +27,8 @@ abstract class _SpringSolution implements Simulatable { class _CriticalSolution implements _SpringSolution { final double _r, _c1, _c2; - factory _CriticalSolution(SpringDesc desc, double distance, double velocity) { + factory _CriticalSolution( + SpringDescription desc, double distance, double velocity) { final double r = -desc.damping / (2.0 * desc.mass); final double c1 = distance; final double c2 = velocity / (r * distance); @@ -41,10 +42,10 @@ class _CriticalSolution implements _SpringSolution { _c1 = c1, _c2 = c2; - double x(double time) => (_c1 + _c2 * time) * Math.pow(Math.E, _r * time); + double x(double time) => (_c1 + _c2 * time) * math.pow(math.E, _r * time); double dx(double time) { - final double power = Math.pow(Math.E, _r * time); + final double power = math.pow(math.E, _r * time); return _r * (_c1 + _c2 * time) * power + _c2 * power; } } @@ -53,12 +54,12 @@ class _OverdampedSolution implements _SpringSolution { final double _r1, _r2, _c1, _c2; factory _OverdampedSolution( - SpringDesc desc, double distance, double velocity) { + SpringDescription desc, double distance, double velocity) { final double cmk = desc.damping * desc.damping - 4 * desc.mass * desc.springConstant; - final double r1 = (-desc.damping - Math.sqrt(cmk)) / (2.0 * desc.mass); - final double r2 = (-desc.damping + Math.sqrt(cmk)) / (2.0 * desc.mass); + final double r1 = (-desc.damping - math.sqrt(cmk)) / (2.0 * desc.mass); + final double r2 = (-desc.damping + math.sqrt(cmk)) / (2.0 * desc.mass); final double c2 = (velocity - r1 * distance) / (r2 - r1); final double c1 = distance - c2; @@ -74,18 +75,18 @@ class _OverdampedSolution implements _SpringSolution { SpringType get type => SpringType.overDamped; double x(double time) => - (_c1 * Math.pow(Math.E, _r1 * time) + _c2 * Math.pow(Math.E, _r2 * time)); + (_c1 * math.pow(math.E, _r1 * time) + _c2 * math.pow(math.E, _r2 * time)); - double dx(double time) => (_c1 * _r1 * Math.pow(Math.E, _r1 * time) + - _c2 * _r2 * Math.pow(Math.E, _r2 * time)); + double dx(double time) => (_c1 * _r1 * math.pow(math.E, _r1 * time) + + _c2 * _r2 * math.pow(math.E, _r2 * time)); } class _UnderdampedSolution implements _SpringSolution { final double _w, _r, _c1, _c2; factory _UnderdampedSolution( - SpringDesc desc, double distance, double velocity) { - final double w = Math.sqrt(4.0 * desc.mass * desc.springConstant - + SpringDescription desc, double distance, double velocity) { + final double w = math.sqrt(4.0 * desc.mass * desc.springConstant - desc.damping * desc.damping) / (2.0 * desc.mass); final double r = -(desc.damping / 2.0 * desc.mass); @@ -103,13 +104,13 @@ class _UnderdampedSolution implements _SpringSolution { SpringType get type => SpringType.underDamped; - double x(double time) => Math.pow(Math.E, _r * time) * - (_c1 * Math.cos(_w * time) + _c2 * Math.sin(_w * time)); + double x(double time) => math.pow(math.E, _r * time) * + (_c1 * math.cos(_w * time) + _c2 * math.sin(_w * time)); double dx(double time) { - final double power = Math.pow(Math.E, _r * time); - final double cosine = Math.cos(_w * time); - final double sine = Math.sin(_w * time); + final double power = math.pow(math.E, _r * time); + final double cosine = math.cos(_w * time); + final double sine = math.sin(_w * time); return power * (_c2 * _w * cosine - _c1 * _w * sine) + _r * power * (_c2 * sine + _c1 * cosine); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index 275aa81ca6..323082f169 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -58,27 +58,32 @@ void main() { }); test('spring_types', () { - var crit = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 300.0, 0.0); + var crit = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0), 0.0, 300.0, 0.0); expect(crit.type, SpringType.criticallyDamped); - var under = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 0.75), 0.0, 300.0, 0.0); + crit = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 1.0), 0.0, 300.0, 0.0); + expect(crit.type, SpringType.criticallyDamped); + + var under = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 0.75), 0.0, 300.0, 0.0); expect(under.type, SpringType.underDamped); - var over = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 300.0, 0.0); + var over = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 1.25), 0.0, 300.0, 0.0); expect(over.type, SpringType.overDamped); // Just so we don't forget how to create a desc without the ratio. - var other = - new SpringSimulation(new SpringDesc(1.0, 100.0, 20.0), 0.0, 20.0, 20.0); + var other = new SpringSimulation( + new SpringDescription(mass: 1.0, springConstant: 100.0, damping: 20.0), + 0.0, 20.0, 20.0); expect(other.type, SpringType.criticallyDamped); }); test('crit_spring', () { - var crit = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 1.0), 0.0, 500.0, 0.0); + var crit = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 1.0), 0.0, 500.0, 0.0); expect(crit.type, SpringType.criticallyDamped); expect(crit.isDone(0.0), false); @@ -99,8 +104,8 @@ void main() { }); test('overdamped_spring', () { - var over = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 1.25), 0.0, 500.0, 0.0); + var over = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 1.25), 0.0, 500.0, 0.0); expect(over.type, SpringType.overDamped); expect(over.isDone(0.0), false); @@ -118,8 +123,8 @@ void main() { }); test('underdamped_spring', () { - var under = new SpringSimulation( - new SpringDesc.withDampingRatio(1.0, 100.0, 0.25), 0.0, 300.0, 0.0); + var under = new SpringSimulation(new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 100.0, ratio: 0.25), 0.0, 300.0, 0.0); expect(under.type, SpringType.underDamped); expect(under.isDone(0.0), false); @@ -135,7 +140,8 @@ void main() { }); test('test_kinetic_scroll', () { - var spring = new SpringDesc.withDampingRatio(1.0, 50.0, 0.5); + var spring = new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 50.0, ratio: 0.5); var scroll = new ScrollSimulation(100.0, 800.0, 0.0, 300.0, spring, 0.3); From 1633ac7f12028eac67a4745b3631bd08ea938935 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 8 Jul 2015 18:15:18 -0700 Subject: [PATCH 11/21] Use the more concise syntax for initialization of variables in the constructor --- packages/newton/lib/src/spring_simulation.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/newton/lib/src/spring_simulation.dart b/packages/newton/lib/src/spring_simulation.dart index 3ca6a846f8..0edf828284 100644 --- a/packages/newton/lib/src/spring_simulation.dart +++ b/packages/newton/lib/src/spring_simulation.dart @@ -16,10 +16,8 @@ class SpringDescription { /// constructor provided for this purpose final double damping; - SpringDescription({double mass, double springConstant, double damping}) - : mass = mass, - springConstant = springConstant, - damping = damping { + SpringDescription( + {double this.mass, double this.springConstant, double this.damping}) { assert(mass != null); assert(springConstant != null); assert(damping != null); From be7e02851a12f88667feb82be2649e2628b90413 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 10 Jul 2015 16:07:59 -0700 Subject: [PATCH 12/21] Add test to verify that composite scroll simulation ends correctly even if the spring was never initialized. --- packages/newton/test/newton_test.dart | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index 323082f169..c7b6323d72 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -151,4 +151,29 @@ void main() { var scroll2 = new ScrollSimulation(100.0, -800.0, 0.0, 300.0, spring, 0.3); expect(scroll2.isDone(4.5), true); }); + + test('scroll_with_inf_edge_ends', () { + var spring = new SpringDescription.withDampingRatio( + mass: 1.0, springConstant: 50.0, ratio: 0.5); + + var scroll = + new ScrollSimulation(100.0, 400.0, 0.0, double.INFINITY, spring, 0.3); + + expect(scroll.isDone(0.0), false); + expect(scroll.x(0.0), 100); + expect(scroll.dx(0.0), 400.0); + + expect(scroll.x(1.0) > 330 && scroll.x(1.0) < 335, true); + + expect(scroll.dx(1.0), 120.0); + expect(scroll.dx(2.0), 36.0); + expect(scroll.dx(3.0), 10.8); + expect(scroll.dx(4.0) < 3.5, true); + + expect(scroll.isDone(5.0), true); + expect(scroll.x(5.0) > 431 && scroll.x(5.0) < 432, true); + + // We should never switch + expect(scroll.currentIntervalOffset, 0.0); + }); } From 1f1cd6c279f93165132d332b0cac7b6292161c16 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 10 Jul 2015 15:54:39 -0700 Subject: [PATCH 13/21] Increase the simulation epsilon We don't need to compute simulations out to that many decimal places because we're working in pixels. R=chinmaygarde@google.com --- packages/newton/lib/src/utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/newton/lib/src/utils.dart b/packages/newton/lib/src/utils.dart index f3edfa2ca5..e6f997fe42 100644 --- a/packages/newton/lib/src/utils.dart +++ b/packages/newton/lib/src/utils.dart @@ -4,7 +4,7 @@ part of newton; -const double _simulationEpsilon = 0.01; +const double _simulationEpsilon = 0.2; bool _nearEqual(double a, double b) => (a > (b - _simulationEpsilon)) && (a < (b + _simulationEpsilon)); From 1ad857b53b95ffca1bd75b94bc84d159ea383e27 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 14 Jul 2015 16:00:00 -0700 Subject: [PATCH 14/21] Allow explicitly setting tolerances on simulations --- packages/newton/lib/newton.dart | 1 + .../newton/lib/src/friction_simulation.dart | 2 +- packages/newton/lib/src/scroll_simulation.dart | 12 +++++++----- packages/newton/lib/src/simulation.dart | 3 +-- packages/newton/lib/src/simulation_group.dart | 15 ++++++++++++--- packages/newton/lib/src/spring_simulation.dart | 3 ++- packages/newton/lib/src/tolerance.dart | 17 +++++++++++++++++ packages/newton/lib/src/utils.dart | 8 +++----- packages/newton/test/newton_test.dart | 12 ++++++++++++ 9 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 packages/newton/lib/src/tolerance.dart diff --git a/packages/newton/lib/newton.dart b/packages/newton/lib/newton.dart index 47de84a790..c70ca3cb02 100644 --- a/packages/newton/lib/newton.dart +++ b/packages/newton/lib/newton.dart @@ -8,6 +8,7 @@ import 'dart:math' as math; part 'src/simulation.dart'; part 'src/simulation_group.dart'; +part 'src/tolerance.dart'; part 'src/utils.dart'; part 'src/friction_simulation.dart'; diff --git a/packages/newton/lib/src/friction_simulation.dart b/packages/newton/lib/src/friction_simulation.dart index 3aa9ce8d0f..590c5e408d 100644 --- a/packages/newton/lib/src/friction_simulation.dart +++ b/packages/newton/lib/src/friction_simulation.dart @@ -22,5 +22,5 @@ class FrictionSimulation extends Simulation { double dx(double time) => _v * math.pow(_drag, time); @override - bool isDone(double time) => dx(time).abs() < 1.0; + bool isDone(double time) => dx(time).abs() < this.tolerance.velocity; } diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index 123e8d1b41..b322945414 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -27,7 +27,7 @@ class ScrollSimulation extends SimulationGroup { } @override - void step(double time) => _chooseSimulation( + bool step(double time) => _chooseSimulation( _currentSimulation.x(time - _offset), _currentSimulation.dx(time - _offset), time); @@ -37,7 +37,7 @@ class ScrollSimulation extends SimulationGroup { @override double get currentIntervalOffset => _offset; - void _chooseSimulation( + bool _chooseSimulation( double position, double velocity, double intervalOffset) { /// This simulation can only step forward. @@ -47,19 +47,21 @@ class ScrollSimulation extends SimulationGroup { _offset = intervalOffset; _currentSimulation = new SpringSimulation( _springDesc, position, _trailingExtent, velocity); - return; + return true; } else if (position < _leadingExtent) { _isSpringing = true; _offset = intervalOffset; _currentSimulation = new SpringSimulation( _springDesc, position, _leadingExtent, velocity); - return; + return true; } } if (_currentSimulation == null) { _currentSimulation = new FrictionSimulation(_drag, position, velocity); - return; + return true; } + + return false; } } diff --git a/packages/newton/lib/src/simulation.dart b/packages/newton/lib/src/simulation.dart index ca06f4811a..e374c9d5ed 100644 --- a/packages/newton/lib/src/simulation.dart +++ b/packages/newton/lib/src/simulation.dart @@ -15,9 +15,8 @@ abstract class Simulatable { /// The base class for all simulations. The user is meant to instantiate an /// instance of a simulation and query the same for the position and velocity /// of the body at a given interval. -/// -/// Note: All operations on subclasses of Simulation are idempotent. abstract class Simulation implements Simulatable { + Tolerance tolerance = toleranceDefault; /// Returns if the simulation is done at a given time bool isDone(double time); diff --git a/packages/newton/lib/src/simulation_group.dart b/packages/newton/lib/src/simulation_group.dart index 9fb6f9c365..44e7bb8673 100644 --- a/packages/newton/lib/src/simulation_group.dart +++ b/packages/newton/lib/src/simulation_group.dart @@ -19,7 +19,8 @@ abstract class SimulationGroup extends Simulation { /// Called when a significant change in the interval is detected. Subclasses /// must decide if the the current simulation must be switched (or updated). - void step(double time); + /// The result is whether the simulation was switched in this step. + bool step(double time); double x(double time) { _stepIfNecessary(time); @@ -31,6 +32,12 @@ abstract class SimulationGroup extends Simulation { return currentSimulation.dx(time - currentIntervalOffset); } + @override + void set tolerance(Tolerance t) { + this.currentSimulation.tolerance = t; + super.tolerance = t; + } + @override bool isDone(double time) { _stepIfNecessary(time); @@ -39,11 +46,13 @@ abstract class SimulationGroup extends Simulation { double _lastStep = -1.0; void _stepIfNecessary(double time) { - if (_nearEqual(_lastStep, time)) { + if (_nearEqual(_lastStep, time, toleranceDefault.time)) { return; } _lastStep = time; - step(time); + if (step(time)) { + this.currentSimulation.tolerance = this.tolerance; + } } } diff --git a/packages/newton/lib/src/spring_simulation.dart b/packages/newton/lib/src/spring_simulation.dart index 0edf828284..5b267b0daa 100644 --- a/packages/newton/lib/src/spring_simulation.dart +++ b/packages/newton/lib/src/spring_simulation.dart @@ -58,5 +58,6 @@ class SpringSimulation extends Simulation { @override bool isDone(double time) => - _nearEqual(x(time), _endPosition) && _nearZero(dx(time)); + _nearEqual(x(time), _endPosition, this.tolerance.distance) && + _nearZero(dx(time), this.tolerance.velocity); } diff --git a/packages/newton/lib/src/tolerance.dart b/packages/newton/lib/src/tolerance.dart new file mode 100644 index 0000000000..b52d075e9c --- /dev/null +++ b/packages/newton/lib/src/tolerance.dart @@ -0,0 +1,17 @@ +// Copyright (c) 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of newton; + +class Tolerance { + final double distance; + final double time; + final double velocity; + + const Tolerance({this.distance: epsilonDefault, this.time: epsilonDefault, + this.velocity: epsilonDefault}); +} + +const double epsilonDefault = 1e-3; +const Tolerance toleranceDefault = const Tolerance(); diff --git a/packages/newton/lib/src/utils.dart b/packages/newton/lib/src/utils.dart index e6f997fe42..75709a3a65 100644 --- a/packages/newton/lib/src/utils.dart +++ b/packages/newton/lib/src/utils.dart @@ -4,9 +4,7 @@ part of newton; -const double _simulationEpsilon = 0.2; +bool _nearEqual(double a, double b, double epsilon) => + (a > (b - epsilon)) && (a < (b + epsilon)); -bool _nearEqual(double a, double b) => - (a > (b - _simulationEpsilon)) && (a < (b + _simulationEpsilon)); - -bool _nearZero(double a) => _nearEqual(a, 0.0); +bool _nearZero(double a, double epsilon) => _nearEqual(a, 0.0, epsilon); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index c7b6323d72..44c8050114 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -12,6 +12,8 @@ void main() { test('test_friction', () { var friction = new FrictionSimulation(0.3, 100.0, 400.0); + friction.tolerance = const Tolerance(velocity: 1.0); + expect(friction.isDone(0.0), false); expect(friction.x(0.0), 100); expect(friction.dx(0.0), 400.0); @@ -84,6 +86,9 @@ void main() { test('crit_spring', () { var crit = new SpringSimulation(new SpringDescription.withDampingRatio( mass: 1.0, springConstant: 100.0, ratio: 1.0), 0.0, 500.0, 0.0); + + crit.tolerance = const Tolerance(distance: 0.01, velocity: 0.01); + expect(crit.type, SpringType.criticallyDamped); expect(crit.isDone(0.0), false); @@ -106,6 +111,9 @@ void main() { test('overdamped_spring', () { var over = new SpringSimulation(new SpringDescription.withDampingRatio( mass: 1.0, springConstant: 100.0, ratio: 1.25), 0.0, 500.0, 0.0); + + over.tolerance = const Tolerance(distance: 0.01, velocity: 0.01); + expect(over.type, SpringType.overDamped); expect(over.isDone(0.0), false); @@ -145,6 +153,8 @@ void main() { 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); + expect(scroll.isDone(0.0), false); expect(scroll.isDone(3.5), true); @@ -159,6 +169,8 @@ void main() { var scroll = new ScrollSimulation(100.0, 400.0, 0.0, double.INFINITY, spring, 0.3); + scroll.tolerance = const Tolerance(velocity: 1.0); + expect(scroll.isDone(0.0), false); expect(scroll.x(0.0), 100); expect(scroll.dx(0.0), 400.0); From 6c639ee389fe2c267dcf0150c87d728cc0e9499d Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 24 Jul 2015 15:15:19 -0700 Subject: [PATCH 15/21] optional spring description --- packages/newton/lib/src/scroll_simulation.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index b322945414..c71986148a 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -18,11 +18,12 @@ class ScrollSimulation extends SimulationGroup { double _offset = 0.0; ScrollSimulation(double position, double velocity, double leading, - double trailing, SpringDescription spring, double drag) + double trailing, [SpringDescription spring, double drag]) : _leadingExtent = leading, _trailingExtent = trailing, _springDesc = spring, _drag = drag { + assert((spring != null && drag != null) || (spring == null && drag == null)); _chooseSimulation(position, velocity, 0.0); } @@ -40,6 +41,10 @@ class ScrollSimulation extends SimulationGroup { bool _chooseSimulation( double position, double velocity, double intervalOffset) { + if (_springDesc == null && + (position > _trailingExtent || position < _leadingExtent)) + return false; + /// This simulation can only step forward. if (!_isSpringing) { if (position > _trailingExtent) { From 66f064eabf22d58d2827f4e318ef17e89cfe6cb1 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 24 Jul 2015 15:51:33 -0700 Subject: [PATCH 16/21] spring can be null drag must not be --- packages/newton/lib/src/scroll_simulation.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index c71986148a..9dece96207 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -18,12 +18,11 @@ class ScrollSimulation extends SimulationGroup { double _offset = 0.0; ScrollSimulation(double position, double velocity, double leading, - double trailing, [SpringDescription spring, double drag]) + double trailing, SpringDescription spring, double drag) : _leadingExtent = leading, _trailingExtent = trailing, _springDesc = spring, _drag = drag { - assert((spring != null && drag != null) || (spring == null && drag == null)); _chooseSimulation(position, velocity, 0.0); } From ea1a8ee3c2a2d896174b8289d1e2a7d4bd442cca Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Mon, 27 Jul 2015 09:07:06 -0700 Subject: [PATCH 17/21] added BoundedFrictionSimulation --- .../newton/lib/src/friction_simulation.dart | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/newton/lib/src/friction_simulation.dart b/packages/newton/lib/src/friction_simulation.dart index 590c5e408d..4cca70433b 100644 --- a/packages/newton/lib/src/friction_simulation.dart +++ b/packages/newton/lib/src/friction_simulation.dart @@ -24,3 +24,25 @@ class FrictionSimulation extends Simulation { @override bool isDone(double time) => dx(time).abs() < this.tolerance.velocity; } + +class BoundedFrictionSimulation extends FrictionSimulation { + BoundedFrictionSimulation( + double drag, + double position, + double velocity, + double this._minX, + double this._maxX) : super(drag, position, velocity); + + final double _minX; + final double _maxX; + + double x(double time) { + return super.x(time).clamp(_minX, _maxX); + } + + bool isDone(double time) { + return super.isDone(time) || + (x(time) - _minX).abs() < tolerance.distance || + (x(time) - _maxX).abs() < tolerance.distance; + } +} From 32ff3e59b46ff3e8f957f15ae921dfb339029ef4 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sat, 19 Sep 2015 08:41:31 -0700 Subject: [PATCH 18/21] Update README.md s/domokit/flutter/ --- packages/newton/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/newton/README.md b/packages/newton/README.md index 731a9abcaa..02942a74f9 100644 --- a/packages/newton/README.md +++ b/packages/newton/README.md @@ -1,5 +1,5 @@ # Newton -[![Build Status](https://travis-ci.org/domokit/newton.svg?branch=master)](https://travis-ci.org/domokit/newton) +[![Build Status](https://travis-ci.org/flutter/newton.svg?branch=master)](https://travis-ci.org/flutter/newton) [![Coverage Status](https://coveralls.io/repos/domokit/newton/badge.svg?branch=master)](https://coveralls.io/r/domokit/newton?branch=master) Simple Physics Simulations for Dart. Springs, friction, gravity, etc. From eaa72f6ef74ebd18d37c4e3984688c61c0c00b84 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 22 Sep 2015 12:55:55 -0700 Subject: [PATCH 19/21] No new functionality, just some gratuitous changes. --- packages/newton/README.md | 4 +++ .../newton/lib/src/scroll_simulation.dart | 34 ++++++++----------- packages/newton/test/newton_test.dart | 1 - 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/newton/README.md b/packages/newton/README.md index 02942a74f9..ccd16eaf98 100644 --- a/packages/newton/README.md +++ b/packages/newton/README.md @@ -3,3 +3,7 @@ [![Coverage Status](https://coveralls.io/repos/domokit/newton/badge.svg?branch=master)](https://coveralls.io/r/domokit/newton?branch=master) Simple Physics Simulations for Dart. Springs, friction, gravity, etc. + +To run the tests: +pub get +dart test/newton_test.dart diff --git a/packages/newton/lib/src/scroll_simulation.dart b/packages/newton/lib/src/scroll_simulation.dart index 9dece96207..f45b6724c5 100644 --- a/packages/newton/lib/src/scroll_simulation.dart +++ b/packages/newton/lib/src/scroll_simulation.dart @@ -8,24 +8,25 @@ part of newton; /// boundary. Friction is applied within the extends and a spring action applied /// at the boundaries. This simulation can only step forward. class ScrollSimulation extends SimulationGroup { + ScrollSimulation( + double position, + double velocity, + this._leadingExtent, + this._trailingExtent, + this._spring, + this._drag) { + _chooseSimulation(position, velocity, 0.0); + } + final double _leadingExtent; final double _trailingExtent; - final SpringDescription _springDesc; + final SpringDescription _spring; final double _drag; bool _isSpringing = false; Simulation _currentSimulation; double _offset = 0.0; - ScrollSimulation(double position, double velocity, double leading, - double trailing, SpringDescription spring, double drag) - : _leadingExtent = leading, - _trailingExtent = trailing, - _springDesc = spring, - _drag = drag { - _chooseSimulation(position, velocity, 0.0); - } - @override bool step(double time) => _chooseSimulation( _currentSimulation.x(time - _offset), @@ -37,11 +38,8 @@ class ScrollSimulation extends SimulationGroup { @override double get currentIntervalOffset => _offset; - bool _chooseSimulation( - double position, double velocity, double intervalOffset) { - - if (_springDesc == null && - (position > _trailingExtent || position < _leadingExtent)) + bool _chooseSimulation(double position, double velocity, double intervalOffset) { + if (_spring == null && (position > _trailingExtent || position < _leadingExtent)) return false; /// This simulation can only step forward. @@ -49,14 +47,12 @@ class ScrollSimulation extends SimulationGroup { if (position > _trailingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new SpringSimulation( - _springDesc, position, _trailingExtent, velocity); + _currentSimulation = new SpringSimulation(_spring, position, _trailingExtent, velocity); return true; } else if (position < _leadingExtent) { _isSpringing = true; _offset = intervalOffset; - _currentSimulation = new SpringSimulation( - _springDesc, position, _leadingExtent, velocity); + _currentSimulation = new SpringSimulation(_spring, position, _leadingExtent, velocity); return true; } } diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index 44c8050114..a70a9c7242 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -168,7 +168,6 @@ void main() { var scroll = new ScrollSimulation(100.0, 400.0, 0.0, double.INFINITY, spring, 0.3); - scroll.tolerance = const Tolerance(velocity: 1.0); expect(scroll.isDone(0.0), false); From 09099a170350356e93ed3974960d1d8ae431c33f Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 25 Sep 2015 10:20:16 -0700 Subject: [PATCH 20/21] Added FrictionSimulation.through() constructor --- .../newton/lib/src/friction_simulation.dart | 24 ++++++++++- packages/newton/test/newton_test.dart | 42 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/packages/newton/lib/src/friction_simulation.dart b/packages/newton/lib/src/friction_simulation.dart index 4cca70433b..38671b99d3 100644 --- a/packages/newton/lib/src/friction_simulation.dart +++ b/packages/newton/lib/src/friction_simulation.dart @@ -16,8 +16,28 @@ class FrictionSimulation extends Simulation { _x = position, _v = velocity; - double x(double time) => - _x + _v * math.pow(_drag, time) / _dragLog - _v / _dragLog; + // Return the drag value for a FrictionSimulation whose x() and dx() values pass + // through the specified start and end position/velocity values. + // + // Total time to reach endVelocity is just: (log(endVelocity) / log(startVelocity)) / log(_drag) + // or (log(v1) - log(v0)) / log(D), given v = v0 * D^t per the dx() function below. + // Solving for D given x(time) is trickier. Algebra courtesy of Wolfram Alpha: + // x1 = x0 + (v0 * D^((log(v1) - log(v0)) / log(D))) / log(D) - v0 / log(D), find D + static double _dragFor(double startPosition, double endPosition, double startVelocity, double endVelocity) { + return math.pow(math.E, (startVelocity - endVelocity) / (startPosition - endPosition)); + } + + // A friction simulation that starts and ends at the specified positions + // and velocities. + factory FrictionSimulation.through(double startPosition, double endPosition, double startVelocity, double endVelocity) { + return new FrictionSimulation( + _dragFor(startPosition, endPosition, startVelocity, endVelocity), + startPosition, + startVelocity) + .. tolerance = new Tolerance(velocity: endVelocity.abs()); + } + + double x(double time) => _x + _v * math.pow(_drag, time) / _dragLog - _v / _dragLog; double dx(double time) => _v * math.pow(_drag, time); diff --git a/packages/newton/test/newton_test.dart b/packages/newton/test/newton_test.dart index a70a9c7242..cd302c0e32 100644 --- a/packages/newton/test/newton_test.dart +++ b/packages/newton/test/newton_test.dart @@ -29,6 +29,48 @@ void main() { expect(friction.x(5.0) > 431 && friction.x(5.0) < 432, true); }); + test('test_friction_through', () { + // Use a normal FrictionSimulation to generate start and end + // velocity and positions with drag = 0.025. + var startPosition = 10.0; + var startVelocity = 600.0; + var f = new FrictionSimulation(0.025, startPosition, startVelocity); + var endPosition = f.x(1.0); + var endVelocity = f.dx(1.0); + expect(endPosition, greaterThan(startPosition)); + expect(endVelocity, lessThan(startVelocity)); + + // Verify that that the "through" FrictionSimulation ends up at + // endPosition and endVelocity; implies that it computed the right + // value for _drag. + var friction = new FrictionSimulation.through( + startPosition, endPosition, startVelocity, endVelocity); + expect(friction.isDone(0.0), false); + expect(friction.x(0.0), 10.0); + expect(friction.dx(0.0), 600.0); + + double epsilon = 1e-4; + expect(friction.isDone(1.0 + epsilon), true); + expect(friction.x(1.0), closeTo(endPosition, epsilon)); + expect(friction.dx(1.0), closeTo(endVelocity, epsilon)); + + // Same scenario as above except that the velocities are + // are negative. + startPosition = 1000.0; + startVelocity = -500.0; + f = new FrictionSimulation(0.025, 1000.0, -500.0); + endPosition = f.x(1.0); + endVelocity = f.dx(1.0); + expect(endPosition, lessThan(startPosition)); + expect(endVelocity, greaterThan(startVelocity)); + + friction = new FrictionSimulation.through( + startPosition, endPosition, startVelocity, endVelocity); + expect(friction.isDone(1.0 + epsilon), true); + expect(friction.x(1.0), closeTo(endPosition, epsilon)); + expect(friction.dx(1.0), closeTo(endVelocity, epsilon)); + }); + test('test_gravity', () { var gravity = new GravitySimulation(200.0, 100.0, 600.0, 0.0); From e5a96acac0c4d3d359b89cc623a20336293f456c Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 14 Oct 2015 16:32:26 -0700 Subject: [PATCH 21/21] 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 f45b6724c5..64e0a45ba1 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 44e7bb8673..a7359dac3f 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 5b267b0daa..143f96c77f 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 cd302c0e32..20efa211d2 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)); + }); }