class MAGES::MeshDeformations::PhysicsWorld

Overview

A physics world that simulates particles and constraints. More…

class PhysicsWorld: public MonoBehaviour
{
public:
    // structs

    struct SimulationStepInfo;

    // fields

    NativeReference ContactPairsCount;
    NativeArray<ContactPair> ContactPairs;

    // properties

    bool Simulate;
    float TimeStep;
    float3 Gravity;
    float MaxVelocity;
    float SleepVelocityThreshold;
    float ParticleRadius;
    int SolverSubsteps;
    float FloorHeight;
    UnityEvent PhysicsUpdate;
    UnityEvent LatePhysicsUpdate;
    UnityEvent DetectorsUpdate;
    bool SolveCollisionConstraints;
    bool ContactFriction;
    int MaxCollisionPairs;
    int CollisionConstraintSolverIterations;
    float CollisionConstraintsRelaxationFactor;
    bool SolveDistanceConstraints;
    int DistanceConstraintSolverIterations;
    float DistanceConstraintsRelaxationFactor;
    bool SolveVolumeConstraints;
    int VolumeConstraintSolverIterations;
    float VolumeConstraintsRelaxationFactor;
    bool SolveCollinearConstraints;
    int CollinearConstraintSolverIterations;
    float CollinearConstraintsRelaxationFactor;
    bool SolveShapeMatchingConstraints;
    int ShapeMatchingConstraintSolverIterations;
    float ShapeMatchingConstraintsRelaxationFactor;
    bool SolveExtraConstraints;

    // methods

    static uint CreatePhase(uint layer, PhaseOptions options);
    static uint GetLayerFromPhase(uint phase);

    void AddSimulationMesh(
        SimulationMesh simulationMesh,
        float4x4 trsMatrix,
        uint phase
    );

    void RemoveSimulationMesh(SimulationMesh simulationMesh);
    SimulationMesh GetSimulationMesh(int simulationMeshDescriptorIndex);
    int GetSimulationMeshDescriptorIndex(SimulationMesh simulationMesh);
    void UpdatePhase(SimulationMesh simulationMesh, uint newPhase);
    void Step(float timeStep);
    void CompleteScheduledStep();

    int QueryParticles(
        float3 position,
        float maxDistance,
        ref NativeArray queryResults
    );
};

Detailed Documentation

A physics world that simulates particles and constraints.

Fields

NativeReference ContactPairsCount

The contact pairs count.

NativeArray<ContactPair> ContactPairs

The contact pairs.

Properties

bool Simulate

Gets or sets a value indicating whether to simulate physics or not.

float TimeStep

Gets or sets the physics time step.

float3 Gravity

Gets or sets the world’s gravity.

float MaxVelocity

Gets or sets the max velocity of particles.

Fast moving particles can pass through objects if time step is too large. This value can be used to limit their max velocity and mitigate this issue.

float SleepVelocityThreshold

Gets or sets the sleep velocity threshold. Particles with a speed lower than this will appear static.

This value can be tuned to make particles appear static when they are moving slowly or eliminate jittering.

float ParticleRadius

Gets or sets the particle radius, used for collisions.

int SolverSubsteps

Gets or sets the solver iterations. Each time step, the solver will solve constraints for this many iterations.

float FloorHeight

Gets or sets the floor height.

UnityEvent PhysicsUpdate

Gets the UnityEvent invoked after a physics update has been completed.

UnityEvent LatePhysicsUpdate

Gets the UnityEvent invoked after a physics update has been completed.

UnityEvent DetectorsUpdate

Gets the update event for all detectors. Called immediately after LatePhysicsUpdate. During this step no further modifications to the mesh should be done.

bool SolveCollisionConstraints

Gets or sets a value indicating whether to solve collision constraints or not.

bool ContactFriction

Gets or sets a value indicating whether to apply friction between colliding particles.

int MaxCollisionPairs

Gets or sets a value indicating the maximum number of collisions in a simulation step.

int CollisionConstraintSolverIterations

Gets or sets the sub steps for collision constraints.

float CollisionConstraintsRelaxationFactor

Gets or sets the relaxation factor for collision constraints.

bool SolveDistanceConstraints

Gets or sets a value indicating whether to solve distance constraints or not.

int DistanceConstraintSolverIterations

Gets or sets the sub steps for distance constraints.

float DistanceConstraintsRelaxationFactor

Gets or sets the relaxation factor for distance constraints.

bool SolveVolumeConstraints

Gets or sets a value indicating whether to solve volume constraints or not.

int VolumeConstraintSolverIterations

Gets or sets the sub steps for volume constraints.

float VolumeConstraintsRelaxationFactor

Gets or sets the relaxation factor for volume constraints.

bool SolveCollinearConstraints

Gets or sets a value indicating whether to solve bending/collinear constraints or not.

int CollinearConstraintSolverIterations

Gets or sets the sub steps for bending/collinear constraints.

float CollinearConstraintsRelaxationFactor

Gets or sets the relaxation factor for bending/collinear constraints.

bool SolveShapeMatchingConstraints

Gets or sets a value indicating whether to solve shape matching constraints or not.

int ShapeMatchingConstraintSolverIterations

Gets or sets the sub steps for shape matching constraints.

float ShapeMatchingConstraintsRelaxationFactor

Gets or sets the relaxation factor for shape matching constraints.

bool SolveExtraConstraints

Gets or sets a value indicating whether to solve any extra registered constraint or not.

Extra constraints are user-defined constraints that are solved after the built-in constraints and implement the IConstraintSolver interface.

Methods

static uint CreatePhase(uint layer, PhaseOptions options)

Creates a new phase for the physics world.

Parameters:

layer

The collision layer.

options

The phase options.

Returns:

The created phase.

static uint GetLayerFromPhase(uint phase)

Extracts the layer from the given phase.

Parameters:

phase

The phase.

Returns:

The layer.

void AddSimulationMesh(
    SimulationMesh simulationMesh,
    float4x4 trsMatrix,
    uint phase
)

Adds a simulation mesh to the physics world.

This operation is slow and should not be used in a per frame basis.

Parameters:

simulationMesh

The simulation mesh to add.

trsMatrix

The TRS matrix for the simulation mesh.

phase

The partilces’ phase.

void RemoveSimulationMesh(SimulationMesh simulationMesh)

Removes a simulation mesh from the physics world.

This operation is slow and should not be used in a per frame basis.

Parameters:

simulationMesh

The simulation mesh to remove.

SimulationMesh GetSimulationMesh(int simulationMeshDescriptorIndex)

Gets the simulation mesh for the given simulation mesh descriptor index.

Parameters:

simulationMeshDescriptorIndex

The simulation mesh descriptor index.

Returns:

The simulation mesh.

int GetSimulationMeshDescriptorIndex(SimulationMesh simulationMesh)

Gets the simulation mesh descriptor index of the simulationMesh.

Parameters:

simulationMesh

The simulation mesh to get the descriptor index for.

Returns:

The descriptor index or -1 if the simulationMesh is not part of the physics world.

void UpdatePhase(SimulationMesh simulationMesh, uint newPhase)

Updates the phase of the particles on the given simulation mesh.

Parameters:

simulationMesh

The simulation mesh.

newPhase

The new phase to change particles to.

void Step(float timeStep)

Moves forward the physics simulation by the given timeStep.

Parameters:

timeStep

The time step.

void CompleteScheduledStep()

Completes pending physics jobs and finishes the current asynchronous physics step, if one exists.

int QueryParticles(
    float3 position,
    float maxDistance,
    ref NativeArray queryResults
)

Spatially queries for particles in the physics world.

Parameters:

position

The position.

maxDistance

The max distance from this position.

queryResults

Referece to the array in which to store the results. The array should be sufficient size to store them.

Returns:

The number of the results.