GravitySim class

Simulates uniformly accelerated motion (gravity / projectile physics).

Position formula: x(t) = x₀ + v₀·t + ½·a·t² Velocity formula: v(t) = v₀ + a·t

Usage:

// Falling object from y=0 downward
final sim = GravitySim(position0: 0.0, velocity0: 0.0, acceleration: 9.81);

// Ball thrown upward
final sim = GravitySim(position0: 0.0, velocity0: -15.0, acceleration: 9.81);
Inheritance

Constructors

GravitySim({required double position0, required double velocity0, double acceleration = 9.81, double endPosition = double.maxFinite})
const

Properties

acceleration double
Acceleration (m/s²). Default: 9.81 (Earth gravity).
final
endPosition double
Position at which the simulation is considered done (floor/ceiling). Set to double.maxFinite to never stop.
final
hashCode int
The hash code for this object.
no setterinherited
id String
Unique identifier for this simulator type.
no setteroverride
peakHeight double?
Peak height above initial position.
no setter
peakTime double?
Time of flight peak (when velocity = 0) for upward throws. Returns null if the object never reverses direction.
no setter
position0 double
Initial position.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
velocity0 double
Initial velocity (positive = in direction of acceleration).
final

Methods

isDone(double t) bool
True when the simulation has effectively come to rest.
override
normalized(double t, {required double from, required double to}) double
Normalizes position to 0.0, 1.0 based on from and to. Useful for driving AnimationController.value.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
position(double t) double
Position at time t (seconds).
override
sample({double durationSec = 2.0, int sampleHz = 60}) List<double>
Samples the simulation at sampleHz for durationSec seconds.
inherited
settleDuration({double maxTime = 10.0, int resolution = 120}) double
Returns the time (seconds) at which isDone first becomes true, sampled at resolution steps per second. Returns maxTime if never done.
inherited
toString() String
A string representation of this object.
override
velocity(double t) double
Velocity at time t (units/second).
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

earthFall({double height = 0.0, double floor = 500.0}) GravitySim
Standard Earth gravity — falling object from rest.
moonFall({double floor = 500.0}) GravitySim
Moon gravity — dreamy, slow-motion falls.
throwUp({double initialVelocity = -300.0}) GravitySim
Upward throw — element shoots up and comes back down.