gravity_simulation.dart 2.66 KB
Newer Older
1
// Copyright 2016 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'simulation.dart';
6

Ian Hickson's avatar
Ian Hickson committed
7 8 9 10
/// A simulation that applies a constant accelerating force.
///
/// Models a particle that follows Newton's second law of motion. The simulation
/// ends when the position reaches a defined point.
11 12 13 14 15 16 17 18
///
/// ## Sample code
///
/// This method triggers an [AnimationController] (a previously constructed
/// `_controller` field) to simulate a fall of 300 pixels.
///
/// ```dart
/// void _startFall() {
19
///   _controller.animateWith(GravitySimulation(
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
///     10.0, // acceleration, pixels per second per second
///     0.0, // starting position, pixels
///     300.0, // ending position, pixels
///     0.0, // starting velocity, pixels per second
///   ));
/// }
/// ```
///
/// This [AnimationController] could be used with an [AnimatedBuilder] to
/// animate the position of a child as if it was falling.
///
/// See also:
///
///  * [Curves.bounceOut], a [Curve] that has a similar aesthetics but includes
///    a bouncing effect.
35
class GravitySimulation extends Simulation {
Ian Hickson's avatar
Ian Hickson committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  /// Creates a [GravitySimulation] using the given arguments, which are,
  /// respectively: an acceleration that is to be applied continually over time;
  /// an initial position relative to an origin; the magnitude of the distance
  /// from that origin beyond which (in either direction) to consider the
  /// simulation to be "done", which must be positive; and an initial velocity.
  ///
  /// The initial position and maximum distance are measured in arbitrary length
  /// units L from an arbitrary origin. The units will match those used for [x].
  ///
  /// The time unit T used for the arguments to [x], [dx], and [isDone],
  /// combined with the aforementioned length unit, together determine the units
  /// that must be used for the velocity and acceleration arguments: L/T and
  /// L/T² respectively. The same units of velocity are used for the velocity
  /// obtained from [dx].
  GravitySimulation(
    double acceleration,
    double distance,
    double endDistance,
    double velocity
55 56 57 58 59 60
  ) : assert(acceleration != null),
      assert(distance != null),
      assert(velocity != null),
      assert(endDistance != null),
      assert(endDistance >= 0),
      _a = acceleration,
Ian Hickson's avatar
Ian Hickson committed
61 62
      _x = distance,
      _v = velocity,
63
      _end = endDistance;
Ian Hickson's avatar
Ian Hickson committed
64

65 66 67 68 69
  final double _x;
  final double _v;
  final double _a;
  final double _end;

70
  @override
71 72
  double x(double time) => _x + _v * time + 0.5 * _a * time * time;

73
  @override
74 75 76 77 78
  double dx(double time) => _v + time * _a;

  @override
  bool isDone(double time) => x(time).abs() >= _end;
}