Skip to content

Commit 1d266e4

Browse files
committed
feat: add docs
1 parent 687403d commit 1d266e4

File tree

5 files changed

+216
-6
lines changed

5 files changed

+216
-6
lines changed

src/dynamics/body.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,35 @@ impl Default for BodyDesc {
119119
}
120120
}
121121

122+
/// Coupling mode between GPU and CPU physics simulations.
123+
///
124+
/// Determines how rigid body state is synchronized between GPU and CPU representations.
122125
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
123126
pub enum BodyCoupling {
127+
/// One-way coupling: CPU -> GPU only.
128+
///
129+
/// The GPU reads body state from CPU but doesn't write back. The body is treated
130+
/// as kinematic from the GPU's perspective (zero mass).
124131
OneWay,
132+
/// Two-way coupling: CPU <-> GPU.
133+
///
134+
/// The GPU both reads from and writes to the body state. The body is fully dynamic
135+
/// with its mass properties applied on the GPU.
125136
#[default]
126137
TwoWays,
127138
}
128139

140+
/// Associates a Rapier rigid body with a collider for GPU simulation.
141+
///
142+
/// Defines which Rapier rigid body and collider pair should be included in the
143+
/// GPU simulation and how they should be coupled.
129144
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
130145
pub struct BodyCouplingEntry {
146+
/// Handle to the Rapier rigid body
131147
pub body: RigidBodyHandle,
148+
/// Handle to the Rapier collider attached to this body
132149
pub collider: ColliderHandle,
150+
/// Coupling mode (one-way or two-way synchronization)
133151
pub mode: BodyCoupling,
134152
}
135153

@@ -144,6 +162,26 @@ impl<B: Backend> GpuBodySet<B> {
144162
self.len
145163
}
146164

165+
/// Create a GPU body set from Rapier bodies and colliders.
166+
///
167+
/// Converts Rapier rigid bodies and their associated colliders into GPU-compatible
168+
/// representations. The coupling entries determine which body-collider pairs are
169+
/// included and how they synchronize with the CPU simulation.
170+
///
171+
/// # Arguments
172+
/// * `backend` - The GPU backend to allocate buffers on
173+
/// * `bodies` - The Rapier rigid body set
174+
/// * `colliders` - The Rapier collider set
175+
/// * `coupling` - List of body-collider pairs to include with their coupling modes
176+
///
177+
/// # Returns
178+
/// A new `GpuBodySet` containing GPU representations of the specified bodies
179+
///
180+
/// # Errors
181+
/// Returns an error if GPU buffer allocation fails
182+
///
183+
/// # Panics
184+
/// Panics if a collider has an unsupported shape type
147185
pub fn from_rapier(
148186
backend: &B,
149187
bodies: &RigidBodySet,
@@ -276,18 +314,34 @@ impl<B: Backend> GpuBodySet<B> {
276314
&self.shapes
277315
}
278316

317+
/// GPU storage buffer containing world-space vertices for complex shapes.
318+
///
319+
/// Contains vertices for polylines and trimeshes in world-space coordinates.
320+
/// Updated when body poses change.
279321
pub fn shapes_vertex_buffers(&self) -> &GpuTensor<Point<f32>, B> {
280322
&self.shapes_vertex_buffers
281323
}
282324

325+
/// GPU storage buffer containing local-space vertices for complex shapes.
326+
///
327+
/// Contains vertices for polylines and trimeshes in body-local coordinates.
328+
/// These are the original untransformed vertices.
283329
pub fn shapes_local_vertex_buffers(&self) -> &GpuTensor<Point<f32>, B> {
284330
&self.shapes_local_vertex_buffers
285331
}
286332

333+
/// GPU storage buffer mapping each vertex to its collider ID.
334+
///
335+
/// For each vertex in the vertex buffers, stores which collider (body index) it belongs to.
336+
/// Used for collision detection and response.
287337
pub fn shapes_vertex_collider_id(&self) -> &GpuTensor<u32, B> {
288338
&self.shapes_vertex_collider_id
289339
}
290340

341+
/// CPU copy of shape data for all bodies.
342+
///
343+
/// Returns a slice containing the [`GpuShape`] for each body in the set.
344+
/// Primarily used for convenience in particle-based simulations.
291345
pub fn shapes_data(&self) -> &[GpuShape] {
292346
&self.shapes_data
293347
}

src/dynamics/integrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::dynamics::{GpuMassProperties, GpuVelocity};
55
use crate::math::GpuSim;
66
use slang_hal::backend::Backend;
77
use slang_hal::function::GpuFunction;
8-
use stensor::tensor::GpuTensor;
98
use slang_hal::Shader;
109
use slang_hal::ShaderArgs;
10+
use stensor::tensor::GpuTensor;
1111

1212
#[derive(Shader)]
1313
#[shader(module = "nexus::dynamics::integrate")]

src/dynamics/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Rigid-body dynamics (forces, velocities, etc.)
22
3-
pub use body::{BodyDesc, GpuBodySet, GpuForce, GpuMassProperties, GpuVelocity};
3+
pub use body::{
4+
BodyCoupling, BodyCouplingEntry, BodyDesc, GpuBodySet, GpuForce, GpuMassProperties, GpuVelocity,
5+
};
46

7+
/// Rigid body definitions and GPU body set management.
58
pub mod body;
9+
/// Physics integration routines (position, velocity updates).
610
pub mod integrate;

src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
11
#![doc = include_str!("../README.md")]
2-
// #![warn(missing_docs)]
2+
#![warn(missing_docs)]
33

44
extern crate nalgebra as na;
5+
/// Re-export of the Rapier 2D physics engine.
6+
///
7+
/// This is available when the `dim2` feature is enabled.
58
#[cfg(feature = "dim2")]
69
pub extern crate rapier2d as rapier;
10+
/// Re-export of the Rapier 3D physics engine.
11+
///
12+
/// This is available when the `dim3` feature is enabled.
713
#[cfg(feature = "dim3")]
814
pub extern crate rapier3d as rapier;
915

1016
use slang_hal::re_exports::include_dir;
1117
use slang_hal::re_exports::minislang::SlangCompiler;
1218

19+
/// GPU-accelerated rigid body dynamics simulation.
20+
///
21+
/// This module provides structures and methods for managing physics bodies
22+
/// on the GPU, including body state, integration, and coupling with colliders.
1323
pub mod dynamics;
24+
/// GPU-compatible shape representations.
25+
///
26+
/// This module defines shape types and utilities for converting Rapier/Parry shapes
27+
/// to GPU-friendly formats with vertex buffers.
1428
pub mod shapes;
1529

30+
/// Mathematical types and utilities for physics simulation.
31+
///
32+
/// Re-exports Rapier's math types and defines dimension-specific type aliases
33+
/// for GPU simulation and angular inertia calculations.
1634
pub mod math {
35+
/// Re-export all mathematical types from Rapier (vectors, matrices, etc.)
1736
pub use rapier::math::*;
1837

38+
/// GPU similarity transformation for 2D simulations (translation + rotation).
1939
#[cfg(feature = "dim2")]
2040
pub type GpuSim = stensor::geometry::GpuSim2;
41+
/// GPU similarity transformation for 3D simulations (translation + rotation).
2142
#[cfg(feature = "dim3")]
2243
pub type GpuSim = stensor::geometry::GpuSim3;
2344

45+
/// Angular inertia type for 2D simulations (scalar).
2446
#[cfg(feature = "dim2")]
2547
pub type AngularInertia<N> = N;
48+
/// Angular inertia type for 3D simulations (3x3 matrix).
2649
#[cfg(feature = "dim3")]
2750
pub type AngularInertia<N> = na::Matrix3<N>;
2851
}
2952

53+
/// Directory containing the Slang shader source files.
54+
///
55+
/// This includes all shader code needed for GPU-accelerated physics simulation.
3056
pub const SLANG_SRC_DIR: include_dir::Dir<'_> =
3157
include_dir::include_dir!("$CARGO_MANIFEST_DIR/../../shaders");
58+
59+
/// Register all required shaders with a Slang compiler.
60+
///
61+
/// This function registers both stensor shaders and Nexus-specific shader sources
62+
/// needed for GPU physics simulation.
63+
///
64+
/// # Arguments
65+
/// * `compiler` - The Slang compiler instance to register shaders with
3266
pub fn register_shaders(compiler: &mut SlangCompiler) {
3367
stensor::register_shaders(compiler);
3468
compiler.add_dir(SLANG_SRC_DIR.clone());

0 commit comments

Comments
 (0)