Contents

ForceEffectProtocol

A protocol that defines a custom force effect.

Declaration

protocol ForceEffectProtocol

Overview

A custom force effect is a function of a set of input rigid body attributes and returns force-like vector quantities. You can declare this custom force effect’s input types (parameterTypes) and output types (forceMode) by conforming to ForceEffectProtocol.

For example, you can declare a custom force effect that depends on rigid bodies’ position and mass, and computes acceleration for each rigid body.

struct MyCustomForce : ForceEffectProtocol {
    var parameterTypes: PhysicsBodyParameterTypes { [.position, .mass] }
    var forceMode: ForceMode = .acceleration
    func update(parameters: inout ForceEffectParameters) {
    }
}

Register your custom force effect to enable the physics system to compute forces affected by rigid bodies.

MyCustomForce.register()

At each physics update, the update(parameters:) method receives the declared inputs from ForceEffectParameters. You can set the output forces for each rigid body with setForce(_:index:).

Sometimes, you may need to access properties from both your custom force effect and the current context while computing forces in the update function. The register(_:) method accepts an optional closure that allows you to capture the necessary properties. If you provide this closure to the register method, the update method is not required.

struct MyCustomForceClosure: ForceEffectProtocol {
    var forceMode: RealityFoundation.ForceMode = .force
    var parameterTypes: PhysicsBodyParameterTypes { [.position, .mass] }
    let customProperty: Double = 1
}

let contextualProperty: Double = 1

MyCustomForceClosure.register { event in
    // Access the effect property via `event.effect.customProperty`.
    // Access the input rigid body attributes via `event.parameters`.
    // Access the contextual property directly by `contextualProperty`.
}

Topics

Updating effects

Instance Properties

Type Methods

See Also

Custom forces