Skip to main content

waft_physics/
simulation.rs

1//! Flutter counterpart: `physics/simulation.dart`.
2
3use std::fmt::Debug;
4
5use crate::Tolerance;
6
7/// The base class for all simulations.
8///
9/// A simulation models an object, in a one-dimensional space, on which
10/// particular forces are being applied, and exposes:
11///
12///  * The object's position, [`x`](Simulation::x)
13///  * The object's velocity, [`dx`](Simulation::dx)
14///  * Whether the simulation is "done", [`is_done`](Simulation::is_done)
15///
16/// A simulation is generally "done" if the object has, to a given
17/// [`tolerance`](Simulation::tolerance), come to a complete rest.
18///
19/// The [`x`](Simulation::x), [`dx`](Simulation::dx), and
20/// [`is_done`](Simulation::is_done) functions take a time argument which
21/// specifies the time for which they are to be evaluated. In principle,
22/// simulations can be stateless, and thus can be queried with arbitrary times.
23/// In practice, however, some simulations are not, and calling any of these
24/// functions will advance the simulation to the given time.
25///
26/// As a general rule, therefore, a simulation should only be queried using
27/// times that are equal to or greater than all times previously used for that
28/// simulation.
29///
30/// Simulations do not specify units for distance, velocity, and time. Client
31/// should establish a convention and use that convention consistently with all
32/// related objects.
33pub trait Simulation: Debug {
34    /// The position of the object in the simulation at the given time.
35    fn x(&self, time: f64) -> f64;
36
37    /// The velocity of the object in the simulation at the given time.
38    fn dx(&self, time: f64) -> f64;
39
40    /// Whether the simulation is "done" at the given time.
41    fn is_done(&self, time: f64) -> bool;
42
43    /// How close to the actual end of the simulation a value at a particular
44    /// time must be before [`is_done`](Simulation::is_done) considers the
45    /// simulation to be "done".
46    ///
47    /// A simulation with an asymptotic curve would never technically be "done",
48    /// but once the difference from the value at a particular time and the
49    /// asymptote itself could not be seen, it would be pointless to continue.
50    /// The tolerance defines how to determine if the difference could not be
51    /// seen.
52    fn tolerance(&self) -> Tolerance;
53
54    /// Dart's `tolerance` setter.
55    fn set_tolerance(&mut self, tolerance: Tolerance);
56}