Skip to content

Commit f62670b

Browse files
committed
[add] better documentation for all of the query code
1 parent 5553fba commit f62670b

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ impl PhysicsBackend2D {
641641

642642
/// Returns all rigid bodies whose colliders contain the provided point.
643643
///
644+
/// This walks the live collider set instead of Rapier's query pipeline
645+
/// because gameplay queries are expected to work immediately after collider
646+
/// creation. Before the world steps, broad-phase acceleration structures are
647+
/// not guaranteed to be synchronized with newly attached colliders.
648+
///
644649
/// # Arguments
645650
/// - `point`: The world-space point to test.
646651
///
@@ -673,6 +678,12 @@ impl PhysicsBackend2D {
673678

674679
/// Returns all rigid bodies whose colliders overlap the provided AABB.
675680
///
681+
/// This performs exact shape-vs-shape tests over the live collider set for
682+
/// the same reason as `query_point_2d()`: overlap queries need to be correct
683+
/// before the first simulation step, when broad-phase data may still be
684+
/// stale. Using exact tests here also avoids broad-phase false positives in
685+
/// the backend result.
686+
///
676687
/// # Arguments
677688
/// - `min`: The minimum world-space corner of the query box.
678689
/// - `max`: The maximum world-space corner of the query box.
@@ -695,6 +706,8 @@ impl PhysicsBackend2D {
695706
let mut body_slots = Vec::new();
696707

697708
for (collider_handle, collider) in self.colliders.iter() {
709+
// Express the query box in the collider's local frame because Parry's
710+
// exact intersection test compares one shape pose relative to the other.
698711
let shape_to_collider = query_pose.inv_mul(collider.position());
699712
let intersects = query_dispatcher.intersection_test(
700713
&shape_to_collider,
@@ -721,10 +734,10 @@ impl PhysicsBackend2D {
721734
/// Returns the nearest rigid body hit by the provided finite ray segment.
722735
///
723736
/// This iterates the live collider set directly instead of using Rapier's
724-
/// broad-phase query pipeline. The overlap-query work already established
725-
/// that direct iteration is the most reliable way to support read-only
726-
/// queries immediately after collider creation, before any simulation step
727-
/// has synchronized broad-phase acceleration structures.
737+
/// broad-phase query pipeline because raycasts are expected to see colliders
738+
/// that were just created or attached earlier in the frame. Keeping queries
739+
/// on the live collider set makes the result match gameplay expectations even
740+
/// before the world has advanced.
728741
///
729742
/// # Arguments
730743
/// - `origin`: The world-space ray origin.

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ impl PhysicsWorld2D {
156156

157157
/// Returns all bodies whose colliders contain the provided point.
158158
///
159+
/// Point queries are intended for gameplay checks that can be called freely
160+
/// during update code. Treating invalid floating-point input as a miss keeps
161+
/// the public API simple for callers and avoids forcing game code to thread
162+
/// backend validation errors through every query site.
163+
///
159164
/// # Arguments
160165
/// - `point`: The world-space point to test.
161166
///
@@ -166,12 +171,22 @@ impl PhysicsWorld2D {
166171
return Vec::new();
167172
}
168173

174+
// Backend queries operate in collider space, but the public API reports
175+
// owning bodies. Rebuild body handles and collapse duplicate hits from
176+
// compound colliders before returning the result.
169177
let body_slots = self.backend.query_point_2d(point);
170178
return self.deduplicate_query_body_hits(body_slots);
171179
}
172180

173181
/// Returns all bodies whose colliders overlap the provided axis-aligned box.
174182
///
183+
/// AABB queries are meant to be tolerant of how gameplay code produces
184+
/// bounds. The world normalizes `min` and `max` before delegating so callers
185+
/// can pass corners in either order without adding their own pre-processing.
186+
/// Invalid floating-point inputs are treated as a miss for the same reason as
187+
/// `query_point()`: query call sites stay straightforward and do not need to
188+
/// handle backend-specific error types.
189+
///
175190
/// # Arguments
176191
/// - `min`: The minimum world-space corner of the query box.
177192
/// - `max`: The maximum world-space corner of the query box.
@@ -183,6 +198,7 @@ impl PhysicsWorld2D {
183198
return Vec::new();
184199
}
185200

201+
// Normalize the query box so callers do not need to sort the bounds first.
186202
let normalized_min = [min[0].min(max[0]), min[1].min(max[1])];
187203
let normalized_max = [min[0].max(max[0]), min[1].max(max[1])];
188204
let body_slots = self.backend.query_aabb_2d(normalized_min, normalized_max);
@@ -192,17 +208,19 @@ impl PhysicsWorld2D {
192208

193209
/// Returns the nearest rigid body hit by the provided ray.
194210
///
211+
/// Raycasts are exposed as a lightweight gameplay query rather than a
212+
/// fallible backend operation. Inputs that cannot represent a meaningful
213+
/// finite segment are treated as a miss, and the backend hit is rebound to
214+
/// this world's public body handles before returning so the high-level API
215+
/// stays vendor-free.
216+
///
195217
/// # Arguments
196218
/// - `origin`: The ray origin in world space.
197219
/// - `dir`: The ray direction in world space.
198220
/// - `max_dist`: The maximum query distance.
199221
///
200222
/// # Returns
201223
/// Returns the nearest hit, if one exists.
202-
///
203-
/// This method preserves an infallible query contract for games: invalid
204-
/// inputs return `None` instead of surfacing backend-specific validation
205-
/// errors through the high-level API.
206224
pub fn raycast(
207225
&self,
208226
origin: [f32; 2],
@@ -245,6 +263,8 @@ impl PhysicsWorld2D {
245263
slot_generation,
246264
);
247265

266+
// Spatial queries match colliders internally, but the public surface is
267+
// body-oriented. Compound bodies therefore collapse to one handle.
248268
if seen_bodies.insert(body) {
249269
bodies.push(body);
250270
}

0 commit comments

Comments
 (0)