box3d-rust 0.2.1

Pure Rust port of the Box3D 3D physics engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Body-level cast / overlap / mover query tests from test_body_query.c.
//!
//! SPDX-FileCopyrightText: 2025 Erin Catto
//! SPDX-License-Identifier: MIT

use crate::body::{
    body_cast_ray, body_cast_shape, body_collide_mover, body_overlap_shape, create_body,
    BodyPlaneResult,
};
use crate::distance::make_proxy;
use crate::geometry::{Capsule, Sphere};
use crate::hull::make_box_hull;
use crate::id::BodyId;
use crate::math_functions::{
    is_normalized, make_quat_from_axis_angle, offset_pos, to_vec3, Pos, Vec3, WorldTransform, PI,
    POS_ZERO, QUAT_IDENTITY, VEC3_ZERO,
};
use crate::shape::{create_hull_shape, create_sphere_shape, shape_is_valid};
use crate::types::{default_body_def, default_query_filter, default_shape_def, default_world_def};
use crate::world::World;

fn create_query_world() -> (World, BodyId) {
    let mut world = World::new(&default_world_def());
    let body_id = create_body(&mut world, &default_body_def());
    (world, body_id)
}

fn identity_at(x: f32, y: f32, z: f32) -> WorldTransform {
    WorldTransform {
        p: Pos {
            x: x as _,
            y: y as _,
            z: z as _,
        },
        q: QUAT_IDENTITY,
    }
}

// CastRay ----------------------------------------------------------------------------------

#[test]
fn cast_ray_hits_sphere() {
    let (mut world, body_id) = create_query_world();

    let sphere = Sphere {
        center: VEC3_ZERO,
        radius: 1.0,
    };
    create_sphere_shape(&mut world, body_id, &default_shape_def(), &sphere);

    // Body sphere at world (5,0,0), ray straight at it along +X.
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let result = body_cast_ray(
        &world,
        body_id,
        POS_ZERO,
        Vec3::new(10.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        body_transform,
    );

    assert!(result.hit);
    assert!(shape_is_valid(&world, result.shape_id));
    assert!((result.fraction - 0.4).abs() < 1e-5);
    assert!((result.normal.x + 1.0).abs() < 1e-5);
    assert!(result.normal.y.abs() < 1e-5);
    assert!(result.normal.z.abs() < 1e-5);

    let point = to_vec3(result.point);
    assert!((point.x - 4.0).abs() < 1e-4);
    assert!(point.y.abs() < 1e-4);
    assert!(point.z.abs() < 1e-4);
}

#[test]
fn cast_ray_miss() {
    let (mut world, body_id) = create_query_world();

    let sphere = Sphere {
        center: VEC3_ZERO,
        radius: 1.0,
    };
    create_sphere_shape(&mut world, body_id, &default_shape_def(), &sphere);

    // Ray runs parallel to the body, never reaching it.
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let result = body_cast_ray(
        &world,
        body_id,
        POS_ZERO,
        Vec3::new(0.0, 10.0, 0.0),
        &default_query_filter(),
        1.0,
        body_transform,
    );

    assert!(!result.hit);
}

#[test]
fn cast_ray_closest_shape() {
    let (mut world, body_id) = create_query_world();

    let shape_def = default_shape_def();
    let near_sphere = Sphere {
        center: VEC3_ZERO,
        radius: 1.0,
    };
    let far_sphere = Sphere {
        center: Vec3::new(4.0, 0.0, 0.0),
        radius: 1.0,
    };
    let near_id = create_sphere_shape(&mut world, body_id, &shape_def, &near_sphere);
    create_sphere_shape(&mut world, body_id, &shape_def, &far_sphere);

    // Ray crosses both spheres; the loop must shrink maxFraction to the nearer hit.
    let body_transform = identity_at(0.0, 0.0, 0.0);
    let result = body_cast_ray(
        &world,
        body_id,
        Pos {
            x: (-5.0) as _,
            y: 0.0 as _,
            z: 0.0 as _,
        },
        Vec3::new(10.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        body_transform,
    );

    assert!(result.hit);
    assert_eq!(result.shape_id.index1, near_id.index1);
    assert_eq!(result.shape_id.generation, near_id.generation);
    assert!((result.fraction - 0.4).abs() < 1e-5);
}

#[test]
fn cast_ray_rotated_body() {
    let (mut world, body_id) = create_query_world();

    // Local center (0,2,0) rotated +90 deg about Z lands at world (-2,0,0).
    let sphere = Sphere {
        center: Vec3::new(0.0, 2.0, 0.0),
        radius: 0.5,
    };
    create_sphere_shape(&mut world, body_id, &default_shape_def(), &sphere);

    let body_transform = WorldTransform {
        p: POS_ZERO,
        q: make_quat_from_axis_angle(Vec3::new(0.0, 0.0, 1.0), 0.5 * PI),
    };
    let result = body_cast_ray(
        &world,
        body_id,
        POS_ZERO,
        Vec3::new(-4.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        body_transform,
    );

    assert!(result.hit);
    assert!((result.fraction - 0.375).abs() < 1e-5);
    assert!((result.normal.x - 1.0).abs() < 1e-5);

    let point = to_vec3(result.point);
    assert!((point.x + 1.5).abs() < 1e-4);
}

#[test]
fn cast_ray_far_from_origin() {
    let (mut world, body_id) = create_query_world();

    let sphere = Sphere {
        center: VEC3_ZERO,
        radius: 1.0,
    };
    create_sphere_shape(&mut world, body_id, &default_shape_def(), &sphere);

    // Same geometry as CastRayHitsSphere shifted far from the world origin.
    let origin = Pos {
        x: 1.0e6 as _,
        y: (-2.0e6) as _,
        z: 5.0e5 as _,
    };
    let body_transform = WorldTransform {
        p: offset_pos(origin, Vec3::new(5.0, 0.0, 0.0)),
        q: QUAT_IDENTITY,
    };
    let result = body_cast_ray(
        &world,
        body_id,
        origin,
        Vec3::new(10.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        body_transform,
    );

    assert!(result.hit);
    assert!((result.fraction - 0.4).abs() < 1e-5);
    assert!((result.normal.x + 1.0).abs() < 1e-5);
    assert!(result.normal.y.abs() < 1e-5);
    assert!(result.normal.z.abs() < 1e-5);
}

// CastShape --------------------------------------------------------------------------------

#[test]
fn cast_shape_hits_box() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    // Sphere proxy of radius 0.5 cast along +X into a box whose front face is at world x = 4.
    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let result = body_cast_shape(
        &world,
        body_id,
        POS_ZERO,
        &proxy,
        Vec3::new(10.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        false,
        body_transform,
    );

    // Front face at world x = 4. The fraction carries a small shape-cast skin.
    assert!(result.hit);
    assert!(shape_is_valid(&world, result.shape_id));
    assert!((result.fraction - 0.35).abs() < 1e-2);
    assert!((result.normal.x + 1.0).abs() < 1e-4);

    let hit = to_vec3(result.point);
    assert!((hit.x - 4.0).abs() < 1e-3);
}

#[test]
fn cast_shape_miss() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let result = body_cast_shape(
        &world,
        body_id,
        POS_ZERO,
        &proxy,
        Vec3::new(0.0, 10.0, 0.0),
        &default_query_filter(),
        1.0,
        false,
        body_transform,
    );

    assert!(!result.hit);
}

#[test]
fn cast_shape_rotated_body() {
    let (mut world, body_id) = create_query_world();

    // Body sphere local center (0,2,0) rotated +90 deg about Z lands at world (-2,0,0).
    let sphere = Sphere {
        center: Vec3::new(0.0, 2.0, 0.0),
        radius: 1.0,
    };
    create_sphere_shape(&mut world, body_id, &default_shape_def(), &sphere);

    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = WorldTransform {
        p: POS_ZERO,
        q: make_quat_from_axis_angle(Vec3::new(0.0, 0.0, 1.0), 0.5 * PI),
    };
    let result = body_cast_shape(
        &world,
        body_id,
        POS_ZERO,
        &proxy,
        Vec3::new(-4.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        false,
        body_transform,
    );

    assert!(result.hit);
    assert!((result.fraction - 0.125).abs() < 1e-2);
    assert!((result.normal.x - 1.0).abs() < 1e-4);

    let hit = to_vec3(result.point);
    assert!((hit.x + 1.0).abs() < 1e-3);
}

#[test]
fn cast_shape_far_from_origin() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let origin = Pos {
        x: 1.0e6 as _,
        y: (-2.0e6) as _,
        z: 5.0e5 as _,
    };
    let body_transform = WorldTransform {
        p: offset_pos(origin, Vec3::new(5.0, 0.0, 0.0)),
        q: QUAT_IDENTITY,
    };
    let result = body_cast_shape(
        &world,
        body_id,
        origin,
        &proxy,
        Vec3::new(10.0, 0.0, 0.0),
        &default_query_filter(),
        1.0,
        false,
        body_transform,
    );

    assert!(result.hit);
    assert!((result.fraction - 0.35).abs() < 1e-2);
    assert!((result.normal.x + 1.0).abs() < 1e-4);
}

// OverlapShape -----------------------------------------------------------------------------

#[test]
fn overlap_true() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    // Proxy sits at the box center.
    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let overlaps = body_overlap_shape(
        &world,
        body_id,
        Pos {
            x: 5.0 as _,
            y: 0.0 as _,
            z: 0.0 as _,
        },
        &proxy,
        &default_query_filter(),
        body_transform,
    );

    assert!(overlaps);
}

#[test]
fn overlap_false() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = identity_at(5.0, 0.0, 0.0);
    let overlaps = body_overlap_shape(
        &world,
        body_id,
        Pos {
            x: 20.0 as _,
            y: 0.0 as _,
            z: 0.0 as _,
        },
        &proxy,
        &default_query_filter(),
        body_transform,
    );

    assert!(!overlaps);
}

#[test]
fn overlap_respects_body_transform() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    // Fixed proxy and origin: only the supplied transform decides the overlap.
    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let origin = POS_ZERO;

    assert!(body_overlap_shape(
        &world,
        body_id,
        origin,
        &proxy,
        &default_query_filter(),
        identity_at(0.0, 0.0, 0.0),
    ));
    assert!(!body_overlap_shape(
        &world,
        body_id,
        origin,
        &proxy,
        &default_query_filter(),
        identity_at(20.0, 0.0, 0.0),
    ));
}

#[test]
fn overlap_filter() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(1.0, 1.0, 1.0);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    let proxy = make_proxy(&[VEC3_ZERO], 0.5);
    let body_transform = identity_at(0.0, 0.0, 0.0);

    // Geometry overlaps, but a zero mask rejects every category.
    let mut filter = default_query_filter();
    filter.mask_bits = 0;
    let overlaps = body_overlap_shape(&world, body_id, POS_ZERO, &proxy, &filter, body_transform);

    assert!(!overlaps);
}

// CollideMover -----------------------------------------------------------------------------

#[test]
fn mover_touches_box() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(0.5, 0.5, 0.5);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    // Mover core runs above the +Y face; its 0.2 radius reaches 0.1 into it.
    let mover = Capsule {
        center1: Vec3::new(-0.3, 0.6, 0.0),
        center2: Vec3::new(0.3, 0.6, 0.0),
        radius: 0.2,
    };
    let mut planes = [BodyPlaneResult::default(); 4];
    let body_transform = identity_at(0.0, 0.0, 0.0);
    let count = body_collide_mover(
        &world,
        body_id,
        &mut planes,
        POS_ZERO,
        &mover,
        &default_query_filter(),
        body_transform,
    );

    assert_eq!(count, 1);
    assert!(shape_is_valid(&world, planes[0].shape_id));
    assert!(is_normalized(planes[0].result.plane.normal));
    assert!(planes[0].result.plane.normal.y > 0.99);
    assert!((planes[0].result.plane.offset - 0.1).abs() < 1e-4);
}

#[test]
fn mover_separated() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(0.5, 0.5, 0.5);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    let mover = Capsule {
        center1: Vec3::new(-0.3, 5.0, 0.0),
        center2: Vec3::new(0.3, 5.0, 0.0),
        radius: 0.2,
    };
    let mut planes = [BodyPlaneResult::default(); 4];
    let body_transform = identity_at(0.0, 0.0, 0.0);
    let count = body_collide_mover(
        &world,
        body_id,
        &mut planes,
        POS_ZERO,
        &mover,
        &default_query_filter(),
        body_transform,
    );

    assert_eq!(count, 0);
}

#[test]
fn mover_rotated_body() {
    let (mut world, body_id) = create_query_world();

    let box_hull = make_box_hull(0.5, 0.5, 0.5);
    create_hull_shape(&mut world, body_id, &default_shape_def(), &box_hull.base);

    // Rotating +90 deg about X turns the local +Y face toward world +Z.
    let mover = Capsule {
        center1: Vec3::new(-0.3, 0.0, 0.6),
        center2: Vec3::new(0.3, 0.0, 0.6),
        radius: 0.2,
    };
    let mut planes = [BodyPlaneResult::default(); 4];
    let body_transform = WorldTransform {
        p: POS_ZERO,
        q: make_quat_from_axis_angle(Vec3::new(1.0, 0.0, 0.0), 0.5 * PI),
    };
    let count = body_collide_mover(
        &world,
        body_id,
        &mut planes,
        POS_ZERO,
        &mover,
        &default_query_filter(),
        body_transform,
    );

    assert_eq!(count, 1);
    assert!(is_normalized(planes[0].result.plane.normal));
    assert!(planes[0].result.plane.normal.z > 0.99);
    assert!((planes[0].result.plane.offset - 0.1).abs() < 1e-4);
}

#[test]
fn mover_capacity() {
    let (mut world, body_id) = create_query_world();

    // Two spheres each touch a mover that runs between them along X at y = 0.
    let shape_def = default_shape_def();
    let left = Sphere {
        center: Vec3::new(-0.4, 0.6, 0.0),
        radius: 0.5,
    };
    let right = Sphere {
        center: Vec3::new(0.4, 0.6, 0.0),
        radius: 0.5,
    };
    create_sphere_shape(&mut world, body_id, &shape_def, &left);
    create_sphere_shape(&mut world, body_id, &shape_def, &right);

    let mover = Capsule {
        center1: Vec3::new(-1.0, 0.0, 0.0),
        center2: Vec3::new(1.0, 0.0, 0.0),
        radius: 0.2,
    };
    let mut planes = [BodyPlaneResult::default(); 4];
    let body_transform = identity_at(0.0, 0.0, 0.0);

    // Capacity caps the result and prevents writing past the buffer.
    let capped = body_collide_mover(
        &world,
        body_id,
        &mut planes[..1],
        POS_ZERO,
        &mover,
        &default_query_filter(),
        body_transform,
    );
    assert_eq!(capped, 1);

    let full = body_collide_mover(
        &world,
        body_id,
        &mut planes,
        POS_ZERO,
        &mover,
        &default_query_filter(),
        body_transform,
    );
    assert_eq!(full, 2);
}