Skip to content

Commit 6578f74

Browse files
committed
[update] tests to check for closeness in raycast computations
1 parent 4b7506f commit 6578f74

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

crates/lambda-rs/tests/physics_2d/queries.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use lambda::physics::{
1313
RigidBodyType,
1414
};
1515

16+
const FLOAT_TOLERANCE: f32 = 1.0e-5;
17+
1618
/// Builds a static rectangle body with one rectangle collider.
1719
///
1820
/// # Arguments
@@ -188,8 +190,8 @@ fn physics_2d_queries_raycast_returns_nearest_hit() {
188190
let hit = world.raycast([0.0, 0.0], [1.0, 0.0], 10.0).unwrap();
189191

190192
assert_eq!(hit.body, near_circle);
191-
assert_eq!(hit.point, [1.5, 0.0]);
192-
assert_eq!(hit.distance, 1.5);
193+
assert_point_approximately_eq(hit.point, [1.5, 0.0]);
194+
assert_f32_approximately_eq(hit.distance, 1.5);
193195

194196
return;
195197
}
@@ -206,8 +208,8 @@ fn physics_2d_queries_raycast_distance_uses_world_units() {
206208
let hit = world.raycast([0.0, 0.0], [2.0, 0.0], 10.0).unwrap();
207209

208210
assert_eq!(hit.body, circle);
209-
assert_eq!(hit.point, [4.0, 0.0]);
210-
assert_eq!(hit.distance, 4.0);
211+
assert_point_approximately_eq(hit.point, [4.0, 0.0]);
212+
assert_f32_approximately_eq(hit.distance, 4.0);
211213

212214
return;
213215
}
@@ -240,13 +242,45 @@ fn physics_2d_queries_raycast_from_inside_reports_zero_distance() {
240242
let hit = world.raycast([0.0, 0.0], [1.0, 0.0], 10.0).unwrap();
241243

242244
assert_eq!(hit.body, rectangle);
243-
assert_eq!(hit.point, [0.0, 0.0]);
244-
assert_eq!(hit.distance, 0.0);
245+
assert_point_approximately_eq(hit.point, [0.0, 0.0]);
246+
assert_f32_approximately_eq(hit.distance, 0.0);
245247
assert_unit_normal(hit);
246248

247249
return;
248250
}
249251

252+
/// Asserts that two scalar values match within floating-point tolerance.
253+
///
254+
/// # Arguments
255+
/// - `actual`: The computed scalar value.
256+
/// - `expected`: The expected scalar value.
257+
///
258+
/// # Returns
259+
/// Returns `()` after validating the scalar difference.
260+
fn assert_f32_approximately_eq(actual: f32, expected: f32) {
261+
assert!(
262+
(actual - expected).abs() <= FLOAT_TOLERANCE,
263+
"expected approximately {expected}, got {actual}",
264+
);
265+
266+
return;
267+
}
268+
269+
/// Asserts that two world-space points match within floating-point tolerance.
270+
///
271+
/// # Arguments
272+
/// - `actual`: The computed point.
273+
/// - `expected`: The expected point.
274+
///
275+
/// # Returns
276+
/// Returns `()` after validating both coordinates.
277+
fn assert_point_approximately_eq(actual: [f32; 2], expected: [f32; 2]) {
278+
assert_f32_approximately_eq(actual[0], expected[0]);
279+
assert_f32_approximately_eq(actual[1], expected[1]);
280+
281+
return;
282+
}
283+
250284
/// Asserts that a raycast hit normal has unit length within tolerance.
251285
///
252286
/// # Arguments
@@ -258,7 +292,7 @@ fn assert_unit_normal(hit: RaycastHit) {
258292
let normal_length =
259293
(hit.normal[0] * hit.normal[0] + hit.normal[1] * hit.normal[1]).sqrt();
260294

261-
assert!((normal_length - 1.0).abs() <= 1.0e-5);
295+
assert!((normal_length - 1.0).abs() <= FLOAT_TOLERANCE);
262296

263297
return;
264298
}

0 commit comments

Comments
 (0)