Commit 8dc626c
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.File tree
12 files changed
+3153
-17
lines changed- crates
- lambda-rs-platform/src/physics
- lambda-rs/src/physics
- demos/physics
- src/bin
- docs
- specs
- physics
- tutorials
- physics/basics
12 files changed
+3153
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
0 commit comments