Simulation
-
Animates changes to a value using a simulation function.
In most scenarios, physics-based animations are simply run to completion. For those situations,
Animator
makes it easy to run and use the results of an animation.In contrast,
See moreSimulator
is useful for scenarios where you need direct access to a running simulation. This might occur in a UI where the user’s scroll position drives changes to a spring’s tension, for example. It would be impractical to create and start a new animation every time the simulation needs to change. ASimulator
instance provides mutable access to thefunction
property (containing the underlying function that is driving the simulation), along with the current state of the simulation (value and velocity).Declaration
Swift
public class Simulator<Value, Function> where Value : VectorConvertible, Function : SimulationFunction, Value.VectorType == Function.VectorType
-
A specialized simulator that uses a spring function.
let spring = Spring(value: CGPoint.zero) spring.bind(to: view, keyPath: \.center) spring.target = CGPoint(x: 300, y: 200)
Declaration
Swift
public typealias Spring<T> = Simulator<T, SpringFunction<T.VectorType>>
-
Simulator
simulates changes to a value over time, based on a function that calculates acceleration after each time step.The RK4 method is used to integrate the acceleration function.
Constant time steps are not guaranteed elsewhere in the framework. Due to the nature of dynamic functions, however, it is desirable to maintain a constant update interval for a dynamic simulation.
See moreSimulation
instances maintain their own internal time state. When `advance(elapsed:) is called on an instance, it may run an arbitrary number of time steps internally (and call the underlying function as needed) in order tocatch up
to the outside time. It then uses linear interpolation to match the internal state to the required external time in order to return the most precise calculations.Declaration
Swift
public struct Simulation<F> : Advanceable where F : SimulationFunction
-
Conforming types implement a dynamic function that models changes to a vector over time.
See moreDeclaration
Swift
public protocol SimulationFunction
-
Implements a simple spring acceleration function.
See moreDeclaration
Swift
public struct SpringFunction<T> : SimulationFunction where T : Vector
-
Gradually reduces velocity until it equals
See moreVector.zero
.Declaration
Swift
public struct DecayFunction<T> : SimulationFunction where T : Vector
-
Implements a two-dimensional gravity simulation function.
See moreDeclaration
Swift
public struct GravityFunction : SimulationFunction