BepuPhysics v2 Documentation

repository·master·Indexed 25 days ago

https://github.com/bepu/bepuphysics2

A high-performance, C#-based 3D rigid body physics engine targeting .NET 8. It features a wide range of collision shapes (spheres, capsules, boxes, triangles, cylinders, convex hulls, compounds, and meshes), linear and angular continuous collision detection (CCD), and efficient scene-wide ray and sweep queries. The engine is designed for extreme speed, utilizing SIMD intrinsics via System.Numerics.Vectors and System.Runtime.Intrinsics for optimal performance on compatible hardware.

Tokens
8.7K
Snippets
7
Records
56
Agent score
83%

What's inside BepuPhysics v2

  1. Overview of BepuPhysics v2

    master
    BepuPhysics v2 is a high-performance 3D rigid body physics engine written in C#. It is a complete rewrite of BEPUphysics v1. The engine is designed for speed and supports a wide variety of collision shapes, constraints, and queries. It targets .NET 8 and is compatible with any supported platform.
  2. Core Features of BepuPhysics v2

    master

    BepuPhysics v2 provides the following capabilities:

    • Collision Shapes: Spheres, capsules, boxes, triangles, cylinders, convex hulls, compounds, and meshes.
    • Constraints: A wide variety of constraint types available in the BepuPhysics/Constraints/ directory.
    • Collision Detection: Linear and angular continuous collision detection (CCD).
    • Optimization: Extremely low-cost sleep states for resting bodies.
    • Queries: Efficient scene-wide ray and sweep queries.
    • Specialized Implementations: Includes a character controller example and support for 'Newts'.
    • Extensibility: An extensible collision pipeline (e.g., custom voxel collidables).
  3. Understand the design intent and API philosophy of BepuPhysics2

    master

    BepuPhysics2 is a low-level physics engine designed for maximum performance. It prioritizes performance over safety and ease of use, which means it exposes much of its underlying data directly to the user.

    Key characteristics to be aware of:

    • Low-level API: The engine provides raw access to data to allow developers to build their own high-level abstractions.
    • Performance-optimized storage: Object data is split and packed into specialized formats to leverage SIMD and cache efficiency.
    • Uncommon C# idioms: The engine makes heavy use of value types (struct), ref semantics, pointers, and SIMD instructions.
    • Manual management: The engine generally allows the user to 'break stuff' in exchange for speed; users are responsible for managing complexity and ensuring correctness.

    Developers using this engine should be comfortable with value type semantics, performance-oriented C# features, and memory management.

  4. Manage mobile bodies in v2

    master

    In v2, there is no explicit Body type. Instead, bodies are managed through Simulation.Bodies.

    • Allocation: Use Simulation.Bodies.Add to create a body. This returns a handle that uniquely identifies the body.
    • Access: Use Simulation.Bodies.HandleToLocation to find the current memory location of a body.
    • Properties: To access or modify properties like Pose or Velocity, create a BodyReference from the body handle. Alternatively, you can manually perform lookups into the Simulation.Bodies sets and their raw property buffers for performance.
  5. Understand BepuPhysics v2 versioning and breaking changes

    master

    BepuPhysics v2 does not follow semantic versioning. When upgrading to a newer version, you should expect breaking changes.

    • Compile Errors: Breaking changes are intended to be obvious and should appear as compile errors.
    • Behavioral Changes: While the project avoids 'sneaky' behavioral changes that don't cause compile errors, you should not expect different versions of the library to produce identical simulation results due to changes in determinism.
    • NuGet Limitations: NuGet packages are available but may not cover all possible features or conditional compilation configurations.
  6. Resolve constraint oscillations and bouncing

    master

    If your simulation exhibits unusual oscillations, bouncing, or explosions, it is likely due to constraint instability. This usually stems from either incomplete force propagation or excessive constraint stiffness.

    Incomplete Force Propagation

    Manifests as bouncing or mild wiggling, often in tall stacks of bodies. The solver fails to converge for all constraints simultaneously.

    • Solution: Increase Simulation.Solver.IterationCount (set this via Simulation.Create) or increase the frequency of Simulation.Timestep calls with a smaller dt.
    • Note on Mass Ratios: High mass ratios (e.g., a very heavy object depending on a very light one) make convergence difficult. To stabilize extreme mass ratios, try reducing lever arms, adjusting inertias, or adding more paths for impulse propagation.

    Excessive Constraint Stiffness

    Occurs when constraint frequency is too high relative to the solver update rate.

    • Guideline: Avoid using a constraint frequency greater than half of your solver update rate. For example, if the solver runs at 60Hz, keep constraint spring settings at 30Hz or below.
    • Solution: If you cannot reduce stiffness, increase the solver's execution rate using substepping or more frequent Simulation.Timestep calls.
  7. Build the BepuPhysics Library

    master

    To build the library, use the latest version of Visual Studio with the .NET desktop development workload installed. Open and build the Library.sln file.

    Requirements:

    • Target Framework: .NET 6
    • C# Version: Requires C# 9.0 or later.
    • Code Generation: BepuPhysics.csproj uses T4 templates. If you modify these templates, you must use a build pipeline capable of processing them (such as Visual Studio).
  8. Build a Simulation with Simulation.Create

    master

    To create a simulation, use Simulation.Create. This method requires two callback parameters that must be implemented as structs (not classes) to allow the compiler to inline them and avoid virtual dispatch overhead:

    1. TNarrowPhaseCallbacks: Handles collision-related logic such as collision filtering, contact manifolds, and materials. Implement INarrowPhaseCallbacks within this struct.
    2. TPoseIntegratorCallbacks: Controls per-body velocity integration (e.g., implementing gravity or damping). Implement IPoseIntegratorCallbacks within this struct.

    Additionally, you must provide a SolveDescription to configure the solver, which allows you to set the number of velocity iterations and substeps. Using more substeps is often more efficient for stabilizing difficult constraints than increasing velocity iterations.

    Optional parameters include initial allocation sizes for the resource pool and an ITimestepper (defaults to DefaultTimestepper).

  9. Ensure single-machine simulation determinism

    master

    To ensure that a simulation produces the same physical results on a single machine given the same inputs, you must:

    1. Maintain Order: Ensure every interaction with the physics simulation (including the order of adding and removing bodies) is reproduced in the exact same order.
    2. Configure Determinism: Meet one of the following conditions:
      • Run the simulation with a single thread (do not provide an IThreadDispatcher to the time step function).
      • Provide multiple threads and set the Simulation.Deterministic property to true.

    Note: The Deterministic property defaults to false and may impact performance in large or chaotic simulations.

  10. Include BepuPhysics v2 in your project

    master

    There are two primary ways to include BepuPhysics v2 in your project:

    1. Clone the Source (Recommended): Because the library uses various conditional compilation symbols and NuGet packages may not include all features, the recommended approach is to clone the source repository and reference the project directly in your solution. This ensures you have access to all features and conditional logic.
    2. NuGet Packages: You can use the official NuGet packages available on NuGet, but be aware they may not cover all possible features.
  11. Run partial performance benchmarks

    master

    To run a high-coverage subset of performance tests, use the -f filter flag with the following benchmark patterns. This is useful for a quicker execution of core components like collision batching, constraints, and shape tests.

    -f *CollisionBatcherTaskBenchmarks.* *GroupedCollisionTesterBenchmarks.* *GatherScatterBenchmarks.* *OneBodyConstraintBenchmarks.* *TwoBodyConstraintBenchmarks.* *ThreeBodyConstraintBenchmarks.* *FourBodyConstraintBenchmarks.* *SweepBenchmarks.* *ShapeRayBenchmarks.* *ShapePileBenchmark.* *RagdollTubeBenchmark.*
  12. Use substepping to increase solver stability

    master

    When increasing the full simulation update rate (Simulation.Timestep) is too expensive, use the solver's substepping feature to increase the frequency of the solver and integrator without increasing the frequency of collision detection.

    To implement substepping, pass a SolveDescription with the desired number of substeps when calling Simulation.Create.

    Example behavior: If Simulation.Timestep is called at 60Hz and you configure 4 substeps, the solver and integrator will effectively run at 240Hz.

    Optimization Tip: When using high substep counts, you can often reduce the number of solver velocity iterations to save performance. Using 1 velocity iteration with substepping is often a 'sweet spot'.