Simulation

  • Animates changes to a value using a simulation function.

    In most scenarios, physics-based animations are simply run to completion. For those situations, PropertyAnimator makes it easy to run and use the results of an animation.

    In contrast, Simulator 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. A Simulator instance provides mutable access to the function property (containing the underlying function that is driving the simulation), along with the current state of the simulation (value and velocity).

    See more

    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>> where T: VectorConvertible
  • 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. Simulation 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 to catch 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.

    See more

    Declaration

    Swift

    public struct Simulation<F: SimulationFunction>: Advanceable
  • Conforming types implement a dynamic function that models changes to a vector over time.

    See more

    Declaration

    Swift

    public protocol SimulationFunction
  • Returned by a simulation function to indicate whether a simulation should converge (come to rest) for a given state.

    See more

    Declaration

    Swift

    public enum Convergence<T> where T: Vector