1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 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.
import 'package:newton/newton.dart';
export 'package:newton/newton.dart' show SpringDescription;
/// A factory for simulations.
abstract class Force {
const Force();
/// Creates a new physics simulation with the given initial conditions.
Simulation release(double position, double velocity);
}
/// A factory for spring-based physics simulations.
class SpringForce extends Force {
const SpringForce(this.spring, { this.left: 0.0, this.right: 1.0 });
/// The description of the spring to be used in the created simulations.
final SpringDescription spring;
/// Where to put the spring's resting point when releasing left.
final double left;
/// Where to put the spring's resting point when releasing right.
final double right;
/// How pricely to terminate the simulation.
///
/// We overshoot the target by this distance, but stop the simulation when
/// the spring gets within this distance (regardless of how fast it's moving).
/// This causes the spring to settle a bit faster than it otherwise would.
static const Tolerance tolerance = const Tolerance(
velocity: double.INFINITY,
distance: 0.01
);
Simulation release(double position, double velocity) {
double target = velocity < 0.0 ? this.left - tolerance.distance
: this.right + tolerance.distance;
return new SpringSimulation(spring, position, target, velocity)
..tolerance = tolerance;
}
}
final SpringDescription _kDefaultSpringDesc = new SpringDescription.withDampingRatio(
mass: 1.0,
springConstant: 500.0,
ratio: 1.0
);
/// A spring force with reasonable default values.
final SpringForce kDefaultSpringForce = new SpringForce(_kDefaultSpringDesc);