Class Simulation
- Namespace
- BepuPhysics
- Assembly
- BepuPhysics.dll
Orchestrates the bookkeeping and execution of a full dynamic simulation.
public class Simulation : IDisposable
- Inheritance
-
Simulation
- Implements
- Inherited Members
Properties
Awakener
Gets the system responsible for awakening bodies within the simulation.
public IslandAwakener Awakener { get; }
Property Value
Bodies
Gets the collection of bodies in the simulation.
public Bodies Bodies { get; }
Property Value
BroadPhase
Gets the broad phase used by the simulation. Supports accelerated ray and volume queries.
public BroadPhase BroadPhase { get; }
Property Value
BroadPhaseOverlapFinder
Gets the system used to find overlapping pairs within the simulation.
public CollidableOverlapFinder BroadPhaseOverlapFinder { get; }
Property Value
BufferPool
Gets the main memory pool used to fill persistent structures and main thread ephemeral resources across the engine.
public BufferPool BufferPool { get; }
Property Value
- BufferPool
Deterministic
Gets or sets whether to use a deterministic time step when using multithreading. When set to true, additional time is spent sorting constraint additions and transfers. Note that this can only affect determinism locally- different processor architectures may implement instructions differently.
public bool Deterministic { get; set; }
Property Value
NarrowPhase
Gets the system used to identify contacts in colliding pairs of shapes and to update contact constraint data.
public NarrowPhase NarrowPhase { get; }
Property Value
PoseIntegrator
Gets the integrator used to update velocities and poses within the simulation.
public IPoseIntegrator PoseIntegrator { get; }
Property Value
Profiler
Gets the simulation profiler. Note that the SimulationProfiler implementation only exists when the library is compiled with the PROFILE compilation symbol; if not defined, returned times are undefined.
public SimulationProfiler Profiler { get; }
Property Value
Shapes
Gets or sets the collection of shapes used by the simulation.
public Shapes Shapes { get; set; }
Property Value
Remarks
While Shapes instances can be shared between multiple Simulation instances, there are no guarantees of thread safety. Further, setting the Shapes property to another collection while there are any outstanding references in the simulation to shapes within the old collection will result in fatal errors if the simulation continues to be used.
Sleeper
Gets the system responsible for putting bodies to sleep within the simulation.
public IslandSleeper Sleeper { get; }
Property Value
Solver
Gets the solver used to solved constraints within the simulation.
public Solver Solver { get; }
Property Value
SolverBatchCompressor
Gets the batch compressor used to compact constraints into fewer solver batches.
public BatchCompressor SolverBatchCompressor { get; }
Property Value
Statics
Gets the collection of statics within the simulation.
public Statics Statics { get; }
Property Value
Timestepper
Gets the timestepper used to update the simulation state.
public ITimestepper Timestepper { get; }
Property Value
Methods
Clear()
Clears the simulation of every object, only returning memory to the pool that would be returned by sequential removes. Other persistent allocations, like those in the Bodies set, will remain.
public void Clear()
CollisionDetection(float, IThreadDispatcher)
Updates the broad phase structure for the current body bounding boxes, finds potentially colliding pairs, and then executes the narrow phase for all such pairs. Generates contact constraints for the solver.
public void CollisionDetection(float dt, IThreadDispatcher threadDispatcher = null)
Parameters
dt
floatDuration of the time step.
threadDispatcher
IThreadDispatcherThread dispatcher to use for execution, if any.
Create<TNarrowPhaseCallbacks, TPoseIntegratorCallbacks>(BufferPool, TNarrowPhaseCallbacks, TPoseIntegratorCallbacks, SolveDescription, ITimestepper, SimulationAllocationSizes?, Shapes)
Constructs a simulation supporting dynamic movement and constraints with the specified narrow phase callbacks.
public static Simulation Create<TNarrowPhaseCallbacks, TPoseIntegratorCallbacks>(BufferPool bufferPool, TNarrowPhaseCallbacks narrowPhaseCallbacks, TPoseIntegratorCallbacks poseIntegratorCallbacks, SolveDescription solveDescription, ITimestepper timestepper = null, SimulationAllocationSizes? initialAllocationSizes = null, Shapes shapes = null) where TNarrowPhaseCallbacks : struct, INarrowPhaseCallbacks where TPoseIntegratorCallbacks : struct, IPoseIntegratorCallbacks
Parameters
bufferPool
BufferPoolBuffer pool used to fill persistent structures and main thread ephemeral resources across the engine.
narrowPhaseCallbacks
TNarrowPhaseCallbacksCallbacks to use in the narrow phase.
poseIntegratorCallbacks
TPoseIntegratorCallbacksCallbacks to use in the pose integrator.
solveDescription
SolveDescriptionDescribes how the solver should execute, including the number of substeps and the number of velocity iterations per substep.
timestepper
ITimestepperTimestepper that defines how the simulation state should be updated. If null, DefaultTimestepper is used.
initialAllocationSizes
SimulationAllocationSizes?Allocation sizes to initialize the simulation with. If left null, default values are chosen.
shapes
ShapesCollection of shapes to use in the simulation, if any. If null, a new Shapes collection will be created for this simulation.
Returns
- Simulation
New simulation.
Type Parameters
TNarrowPhaseCallbacks
TPoseIntegratorCallbacks
Dispose()
Clears the simulation of every object and returns all pooled memory to the buffer pool. Leaves the simulation in an unusable state.
public void Dispose()
EnsureCapacity(SimulationAllocationSizes)
Increases the allocation size of any buffers too small to hold the allocation target.
public void EnsureCapacity(SimulationAllocationSizes allocationTarget)
Parameters
allocationTarget
SimulationAllocationSizesAllocation sizes to guarantee sufficient size for.
Remarks
The final size of the allocated buffers are constrained by the allocator. It is not guaranteed to be exactly equal to the target, but it is guaranteed to be at least as large.
This is primarily a convenience function. Everything it does internally can be done externally. For example, if only type batches need to be resized, the solver's own functions can be used directly.
IncrementallyOptimizeDataStructures(IThreadDispatcher)
Incrementally improves body and constraint storage for better performance.
public void IncrementallyOptimizeDataStructures(IThreadDispatcher threadDispatcher = null)
Parameters
threadDispatcher
IThreadDispatcherThread dispatcher to use for execution, if any.
PredictBoundingBoxes(float, IThreadDispatcher)
Predicts the bounding boxes of active bodies by speculatively integrating velocity. Does not actually modify body velocities. Updates deactivation candidacy.
public void PredictBoundingBoxes(float dt, IThreadDispatcher threadDispatcher = null)
Parameters
dt
floatDuration of the time step.
threadDispatcher
IThreadDispatcherThread dispatcher to use for execution, if any.
RayCast<THitHandler>(Vector3, Vector3, float, ref THitHandler, int)
Intersects a ray against the simulation.
public void RayCast<THitHandler>(Vector3 origin, Vector3 direction, float maximumT, ref THitHandler hitHandler, int id = 0) where THitHandler : IRayHitHandler
Parameters
origin
Vector3Origin of the ray to cast.
direction
Vector3Direction of the ray to cast.
maximumT
floatMaximum length of the ray traversal in units of the direction's length.
hitHandler
THitHandlercallbacks to execute on ray-object intersections.
id
intUser specified id of the ray.
Type Parameters
THitHandler
Type of the callbacks to execute on ray-object intersections.
Resize(SimulationAllocationSizes)
Increases the allocation size of any buffers too small to hold the allocation target, and decreases the allocation size of any buffers that are unnecessarily large.
public void Resize(SimulationAllocationSizes allocationTarget)
Parameters
allocationTarget
SimulationAllocationSizesAllocation sizes to guarantee sufficient size for.
Remarks
The final size of the allocated buffers are constrained by the allocator. It is not guaranteed to be exactly equal to the target, but it is guaranteed to be at least as large.
This is primarily a convenience function. Everything it does internally can be done externally. For example, if only type batches need to be resized, the solver's own functions can be used directly.
Sleep(IThreadDispatcher)
Executes the sleep stage, moving candidate
public void Sleep(IThreadDispatcher threadDispatcher = null)
Parameters
threadDispatcher
IThreadDispatcherThread dispatcher to use for the sleeper execution, if any.
Solve(float, IThreadDispatcher)
Updates the broad phase structure for the current body bounding boxes, finds potentially colliding pairs, and then executes the narrow phase for all such pairs. Generates contact constraints for the solver.
public void Solve(float dt, IThreadDispatcher threadDispatcher = null)
Parameters
dt
floatDuration of the time step.
threadDispatcher
IThreadDispatcherThread dispatcher to use for execution, if any.
Sweep<TShape, TSweepHitHandler>(TShape, in RigidPose, in BodyVelocity, float, BufferPool, ref TSweepHitHandler, float, float, int)
Sweeps a shape against the simulation.
public void Sweep<TShape, TSweepHitHandler>(TShape shape, in RigidPose pose, in BodyVelocity velocity, float maximumT, BufferPool pool, ref TSweepHitHandler hitHandler, float minimumProgression, float convergenceThreshold, int maximumIterationCount) where TShape : unmanaged, IConvexShape where TSweepHitHandler : ISweepHitHandler
Parameters
shape
TShapeShape to sweep.
pose
RigidPoseStarting pose of the sweep.
velocity
BodyVelocityVelocity of the swept shape.
maximumT
floatMaximum length of the sweep in units of time used to integrate the velocity.
pool
BufferPoolPool to allocate any temporary resources in during execution.
hitHandler
TSweepHitHandlerCallbacks executed when a sweep impacts an object in the scene.
minimumProgression
floatMinimum amount of progress in terms of t parameter that any iterative sweep tests should make for each sample.
convergenceThreshold
floatThreshold in terms of t parameter under which iterative sweep tests are permitted to exit in collision.
maximumIterationCount
intMaximum number of iterations to use in iterative sweep tests.
Type Parameters
TShape
Type of the shape to sweep.
TSweepHitHandler
Type of the callbacks executed when a sweep impacts an object in the scene.
Remarks
Simulation objects are treated as stationary during the sweep.
Sweep<TShape, TSweepHitHandler>(in TShape, in RigidPose, in BodyVelocity, float, BufferPool, ref TSweepHitHandler)
Sweeps a shape against the simulation.
public void Sweep<TShape, TSweepHitHandler>(in TShape shape, in RigidPose pose, in BodyVelocity velocity, float maximumT, BufferPool pool, ref TSweepHitHandler hitHandler) where TShape : unmanaged, IConvexShape where TSweepHitHandler : ISweepHitHandler
Parameters
shape
TShapeShape to sweep.
pose
RigidPoseStarting pose of the sweep.
velocity
BodyVelocityVelocity of the swept shape.
maximumT
floatMaximum length of the sweep in units of time used to integrate the velocity.
pool
BufferPoolPool to allocate any temporary resources in during execution.
hitHandler
TSweepHitHandlerCallbacks executed when a sweep impacts an object in the scene.
Type Parameters
TShape
Type of the shape to sweep.
TSweepHitHandler
Type of the callbacks executed when a sweep impacts an object in the scene.
Remarks
Simulation objects are treated as stationary during the sweep.
Timestep(float, IThreadDispatcher)
Performs one timestep of the given length.
public void Timestep(float dt, IThreadDispatcher threadDispatcher = null)
Parameters
dt
floatDuration of the time step.
threadDispatcher
IThreadDispatcherThread dispatcher to use for execution, if any.
Remarks
Be wary of variable timesteps. They can harm stability. Whenever possible, keep the timestep the same across multiple frames unless you have a specific reason not to.