Skip to content

Commit 8dc626c

Browse files
authored
feat(lambda-rs): 2D rigid body support (#185)
## Summary Add 2D rigid body support to `PhysicsWorld2D` with a backend- agnostic public API in `lambda-rs`, a Rapier-backed implementation in `lambda-rs-platform`, and supporting documentation + a runnable demo. This work adds: - Rigid body creation for `Static`, `Dynamic`, and `Kinematic` bodies. - Query + mutation of position, rotation, and linear velocity. - Force + impulse application for dynamic bodies. - A safe handle model that prevents world mismatches and stale body access. - A demo and tutorial showcasing usage without collision shapes/response. ## Related Issues - Resolves #119 ## Changes - Add `RigidBodyType`, `RigidBody2D`, and `RigidBody2DBuilder`. - Expose body operations as fallible APIs with actionable errors: - World scoping: `WorldMismatch`, `InvalidHandle` - Liveness: `BodyNotFound` - Input validation: `InvalidPosition`, `InvalidRotation`, `InvalidVelocity`, `InvalidForce`, `InvalidImpulse`, `InvalidMassKg` - Capability gating: `UnsupportedOperation { body_type }` - Implement fixed-timestep stepping semantics: - The backend advances once per substep. - Accumulated forces are cleared once per outer `PhysicsWorld2D::step()`. - Implement integration semantics: - Dynamic bodies: symplectic Euler with gravity + accumulated force/mass. - Kinematic bodies: position integrates from velocity. - Static bodies: no integration. - Store bodies in slot storage with a generation counter to prevent stale-handle aliasing when slots are reused. - Add `lambda-demos-physics` runnable demo: `physics_rigid_bodies_2d`. - Demonstrates dynamic bodies (gravity/forces/impulses), a kinematic body (programmatic motion), and a static reference body. - Uses manual floor/wall/ceiling constraints to keep bodies on-screen (no collision shapes/response). - Add spec: `docs/specs/physics/rigid-bodies-2d.md` and update spec index. - Add tutorial: `docs/tutorials/physics/basics/rigid-bodies-2d.md` and update tutorial index. - Update `docs/features.md` for the new physics-2D feature surface without pinning a specific Rapier version. ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [x] Feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation (updates to docs, specs, tutorials, or comments) - [ ] Refactor (code change that neither fixes a bug nor adds a feature) - [ ] Performance (change that improves performance) - [x] Test (adding or updating tests) - [ ] Build/CI (changes to build process or CI configuration) ## Affected Crates - [x] `lambda-rs` - [x] `lambda-rs-platform` - [ ] `lambda-rs-args` - [ ] `lambda-rs-logging` - [x] Other: `lambda-demos-physics` ## Checklist - [ ] Code follows the repository style guidelines (`cargo +nightly fmt --all`) - [ ] Code passes clippy (`cargo clippy --workspace --all-targets -- -D warnings`) - [ ] Tests pass (`cargo test --workspace`) - [x] New code includes appropriate documentation - [x] Public API changes are documented - [ ] Breaking changes are noted in this PR description ## Testing **Commands run:** ```bash cargo +nightly fmt --all cargo clippy -p lambda-demos-physics --all-targets --features physics-2d -- -D warnings ``` **Recommended additional verification:** ```bash cargo build --workspace cargo test --workspace cargo clippy --workspace --all-targets -- -D warnings ``` **Manual verification steps:** 1. `cargo run -p lambda-demos-physics --bin physics_rigid_bodies_2d --features physics-2d` 2. Observe: - Dynamic bodies fall under gravity and drift under wind force. - Space applies an upward impulse to the dynamic bodies. - Bodies remain constrained within floor/walls/ceiling bounds. - Kinematic body motion is driven by velocity and remains within wall bounds. ## Screenshots/Recordings ## Platform Testing - [x] macOS - [ ] Windows - [ ] Linux ## Additional Notes - Collision shapes/response are intentionally out of scope; the demo constraints are explicit logic to keep bodies visible.
2 parents 6f96052 + 1c810d1 commit 8dc626c

File tree

12 files changed

+3153
-17
lines changed

12 files changed

+3153
-17
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
name: Linting for lambda-rs & tools.
2323
types: [rust]
2424
- id: clippy
25-
entry: rustup run nightly cargo clippy --workspace --all-targets -- -D warnings
25+
entry: cargo clippy --workspace --all-targets -- -D warnings
2626
language: system
2727
name: Clippy for lambda-rs & tools.
2828
pass_filenames: false

crates/lambda-rs-platform/src/physics/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
77
pub mod rapier2d;
88

9-
pub use rapier2d::PhysicsBackend2D;
9+
pub use rapier2d::{
10+
PhysicsBackend2D,
11+
RigidBody2DBackendError,
12+
RigidBodyType2D,
13+
};

0 commit comments

Comments
 (0)