box2d-rust 1.2.0

Pure Rust port of the Box2D v3 2D 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// The b2Shape_* public API from shape.c: accessors, geometry get/set with
// proxy reset, event flags, contact/sensor introspection, queries, and wind.
//
// b2Shape_GetWorld is omitted: there is no world registry in the Rust port.
//
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

use super::{
    compute_shape_mass, get_shape_index, make_shape_distance_proxy, ray_cast_shape,
    update_shape_aabbs,
};
use crate::body::{
    get_body_transform, get_body_transform_quick, make_body_id, update_body_mass_data,
};
use crate::collision::{MassData, RayCastInput, ShapeGeometry, ShapeType, WorldCastOutput};
use crate::core::NULL_INDEX;
use crate::distance::{make_proxy, shape_distance, DistanceInput, SimplexCache};
use crate::events::ContactData;
use crate::geometry::{point_in_capsule, point_in_circle, point_in_polygon};
use crate::id::{BodyId, ContactId, ShapeId};
use crate::math_functions::{
    inv_mul_world_transforms, inv_transform_world_point, is_valid_float, is_valid_position,
    is_valid_vec2, offset_pos, to_relative_transform, transform_world_point, Aabb, Pos, Vec2,
    ROT_IDENTITY, VEC2_ZERO,
};
use crate::solver_set::AWAKE_SET;
use crate::types::{Filter, SurfaceMaterial};
use crate::world::World;

/// Shape id validity. (b2Shape_IsValid — the world-registry check collapses
/// to the index/generation check in the registry-less port)
pub fn shape_is_valid(world: &World, id: ShapeId) -> bool {
    let shape_id = id.index1 - 1;
    if shape_id < 0 || world.shapes.len() as i32 <= shape_id {
        return false;
    }

    let shape = &world.shapes[shape_id as usize];
    if shape.id == NULL_INDEX {
        // shape is free
        return false;
    }

    debug_assert!(shape.id == shape_id);

    id.generation == shape.generation
}

/// Get the id of the body that a shape is attached to. (b2Shape_GetBody)
pub fn shape_get_body(world: &World, shape_id: ShapeId) -> BodyId {
    let shape_index = get_shape_index(world, shape_id);
    make_body_id(world, world.shapes[shape_index as usize].body_id)
}

/// Set the user data for a shape. (b2Shape_SetUserData)
pub fn shape_set_user_data(world: &mut World, shape_id: ShapeId, user_data: u64) {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].user_data = user_data;
}

/// Get the user data for a shape. (b2Shape_GetUserData)
pub fn shape_get_user_data(world: &World, shape_id: ShapeId) -> u64 {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].user_data
}

/// Returns true if the shape is a sensor. (b2Shape_IsSensor)
pub fn shape_is_sensor(world: &World, shape_id: ShapeId) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].sensor_index != NULL_INDEX
}

/// Test a point for overlap with a shape. (b2Shape_TestPoint)
pub fn shape_test_point(world: &mut World, shape_id: ShapeId, point: Pos) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];

    let transform = get_body_transform(world, shape.body_id);
    let local_point = inv_transform_world_point(transform, point);

    let result = match &shape.geometry {
        ShapeGeometry::Capsule(capsule) => point_in_capsule(capsule, local_point),
        ShapeGeometry::Circle(circle) => point_in_circle(circle, local_point),
        ShapeGeometry::Polygon(polygon) => point_in_polygon(polygon, local_point),
        _ => false,
    };

    crate::recording::record_query_result(
        world,
        crate::recording::OP_SHAPE_TEST_POINT,
        |buf| {
            crate::recording::rec_w_shapeid(buf, shape_id);
            crate::recording::rec_w_position(buf, point);
        },
        |buf| crate::recording::rec_w_bool(buf, result),
    );

    result
}

/// Ray cast a shape directly. (b2Shape_RayCast)
pub fn shape_ray_cast(
    world: &mut World,
    shape_id: ShapeId,
    origin: Pos,
    translation: Vec2,
) -> WorldCastOutput {
    debug_assert!(is_valid_position(origin));
    debug_assert!(is_valid_vec2(translation));

    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];

    // Re-center on the origin so the cast runs in float precision
    let transform = to_relative_transform(get_body_transform(world, shape.body_id), origin);

    // The ray starts at the origin, so its origin in the re-centered frame is zero
    let input = RayCastInput {
        origin: VEC2_ZERO,
        translation,
        max_fraction: 1.0,
    };

    // Lift the re-centered float result back to a world position
    let local = ray_cast_shape(&input, shape, transform);
    let output = WorldCastOutput {
        normal: local.normal,
        point: offset_pos(origin, local.point),
        fraction: local.fraction,
        iterations: local.iterations,
        hit: local.hit,
    };

    crate::recording::record_query_result(
        world,
        crate::recording::OP_SHAPE_RAY_CAST,
        |buf| {
            crate::recording::rec_w_shapeid(buf, shape_id);
            crate::recording::rec_w_position(buf, origin);
            crate::recording::rec_w_vec2(buf, translation);
        },
        |buf| crate::recording::rec_w_worldcastoutput(buf, output),
    );

    output
}

/// Set the mass density of a shape, usually in kg/m^2. (b2Shape_SetDensity)
pub fn shape_set_density(
    world: &mut World,
    shape_id: ShapeId,
    density: f32,
    update_body_mass: bool,
) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_set_density(rec, shape_id, density, update_body_mass)
    });
    debug_assert!(is_valid_float(density) && density >= 0.0);

    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    let shape = &mut world.shapes[shape_index as usize];
    if density == shape.density {
        // early return to avoid expensive function
        return;
    }

    shape.density = density;

    if update_body_mass {
        let body_id = shape.body_id;
        update_body_mass_data(world, body_id);
    }
}

/// Get the density of a shape, usually in kg/m^2. (b2Shape_GetDensity)
pub fn shape_get_density(world: &World, shape_id: ShapeId) -> f32 {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].density
}

/// Set the friction on a shape. (b2Shape_SetFriction)
pub fn shape_set_friction(world: &mut World, shape_id: ShapeId, friction: f32) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_f32(
            rec,
            crate::recording::OP_SHAPE_SET_FRICTION,
            shape_id,
            friction,
        )
    });
    debug_assert!(is_valid_float(friction) && friction >= 0.0);

    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.friction = friction;
}

/// Get the friction of a shape. (b2Shape_GetFriction)
pub fn shape_get_friction(world: &World, shape_id: ShapeId) -> f32 {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.friction
}

/// Set the shape restitution. (b2Shape_SetRestitution)
pub fn shape_set_restitution(world: &mut World, shape_id: ShapeId, restitution: f32) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_f32(
            rec,
            crate::recording::OP_SHAPE_SET_RESTITUTION,
            shape_id,
            restitution,
        )
    });
    debug_assert!(is_valid_float(restitution) && restitution >= 0.0);

    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.restitution = restitution;
}

/// Get the shape restitution. (b2Shape_GetRestitution)
pub fn shape_get_restitution(world: &World, shape_id: ShapeId) -> f32 {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.restitution
}

/// Set the shape's user material identifier. (b2Shape_SetUserMaterial)
pub fn shape_set_user_material(world: &mut World, shape_id: ShapeId, material: u64) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_set_user_material(rec, shape_id, material)
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.user_material_id = material;
}

/// Get the shape's user material identifier. (b2Shape_GetUserMaterial)
pub fn shape_get_user_material(world: &World, shape_id: ShapeId) -> u64 {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material.user_material_id
}

/// Get the shape's surface material. (b2Shape_GetSurfaceMaterial)
pub fn shape_get_surface_material(world: &World, shape_id: ShapeId) -> SurfaceMaterial {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material
}

/// Set the shape's surface material. (b2Shape_SetSurfaceMaterial)
pub fn shape_set_surface_material(
    world: &mut World,
    shape_id: ShapeId,
    surface_material: SurfaceMaterial,
) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_set_surface_material(rec, shape_id, surface_material)
    });
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].material = surface_material;
}

/// Get the shape filter. (b2Shape_GetFilter)
pub fn shape_get_filter(world: &World, shape_id: ShapeId) -> Filter {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].filter
}

/// Destroy this shape's contacts and refresh its broad-phase presence after a
/// filter or geometry change. (static b2ResetProxy)
pub(crate) fn reset_proxy(
    world: &mut World,
    shape_index: i32,
    wake_bodies: bool,
    destroy_proxy: bool,
) {
    let body_id = world.shapes[shape_index as usize].body_id;

    // destroy all contacts associated with this shape
    let mut contact_key = world.bodies[body_id as usize].head_contact_key;
    while contact_key != NULL_INDEX {
        let contact_id = contact_key >> 1;
        let edge_index = contact_key & 1;

        contact_key = world.contacts[contact_id as usize].edges[edge_index as usize].next_key;

        let (contact_shape_a, contact_shape_b) = {
            let contact = &world.contacts[contact_id as usize];
            (contact.shape_id_a, contact.shape_id_b)
        };
        if contact_shape_a == shape_index || contact_shape_b == shape_index {
            crate::contact::destroy_contact(world, contact_id, wake_bodies);
        }
    }

    let transform = get_body_transform_quick(world, &world.bodies[body_id as usize]);
    let proxy_key = world.shapes[shape_index as usize].proxy_key;
    if proxy_key != NULL_INDEX {
        let proxy_type = crate::broad_phase::proxy_type(proxy_key);
        update_shape_aabbs(
            &mut world.shapes[shape_index as usize],
            transform,
            proxy_type,
        );

        if destroy_proxy {
            world.broad_phase.destroy_proxy(proxy_key);

            let (fat_aabb, category_bits) = {
                let shape = &world.shapes[shape_index as usize];
                (shape.fat_aabb, shape.filter.category_bits)
            };
            let force_pair_creation = true;
            world.shapes[shape_index as usize].proxy_key = world.broad_phase.create_proxy(
                proxy_type,
                fat_aabb,
                category_bits,
                shape_index,
                force_pair_creation,
            );
        } else {
            let fat_aabb = world.shapes[shape_index as usize].fat_aabb;
            world.broad_phase.move_proxy(proxy_key, fat_aabb);
        }
    } else {
        let proxy_type = world.bodies[body_id as usize].type_;
        update_shape_aabbs(
            &mut world.shapes[shape_index as usize],
            transform,
            proxy_type,
        );
    }

    world.validate_solver_sets();
}

/// Set the current filter. This is almost as expensive as recreating the
/// shape. Sensor overlaps are not updated until the next time step.
/// (b2Shape_SetFilter)
pub fn shape_set_filter(world: &mut World, shape_id: ShapeId, filter: Filter) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_set_filter(rec, shape_id, filter)
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    {
        let shape = &world.shapes[shape_index as usize];
        if filter.mask_bits == shape.filter.mask_bits
            && filter.category_bits == shape.filter.category_bits
            && filter.group_index == shape.filter.group_index
        {
            return;
        }
    }

    // If the category bits change, I need to destroy the proxy because it
    // affects the tree sorting.
    let destroy_proxy =
        filter.category_bits != world.shapes[shape_index as usize].filter.category_bits;

    world.shapes[shape_index as usize].filter = filter;

    // need to wake bodies because a filter change may destroy contacts
    let wake_bodies = true;
    reset_proxy(world, shape_index, wake_bodies, destroy_proxy);

    // note: this does not immediately update sensor overlaps. Instead sensor
    // overlaps are updated the next time step
}

/// Enable sensor events for this shape. (b2Shape_EnableSensorEvents)
pub fn shape_enable_sensor_events(world: &mut World, shape_id: ShapeId, flag: bool) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_bool(
            rec,
            crate::recording::OP_SHAPE_ENABLE_SENSOR_EVENTS,
            shape_id,
            flag,
        )
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_sensor_events = flag;
}

/// Returns true if sensor events are enabled. (b2Shape_AreSensorEventsEnabled)
pub fn shape_are_sensor_events_enabled(world: &World, shape_id: ShapeId) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_sensor_events
}

/// Enable contact events for this shape. (b2Shape_EnableContactEvents)
pub fn shape_enable_contact_events(world: &mut World, shape_id: ShapeId, flag: bool) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_bool(
            rec,
            crate::recording::OP_SHAPE_ENABLE_CONTACT_EVENTS,
            shape_id,
            flag,
        )
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_contact_events = flag;
}

/// Returns true if contact events are enabled.
/// (b2Shape_AreContactEventsEnabled)
pub fn shape_are_contact_events_enabled(world: &World, shape_id: ShapeId) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_contact_events
}

/// Enable pre-solve contact events for this shape. Only applies to dynamic
/// bodies. These are expensive and must be carefully handled due to
/// multithreading. (b2Shape_EnablePreSolveEvents)
pub fn shape_enable_pre_solve_events(world: &mut World, shape_id: ShapeId, flag: bool) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_bool(
            rec,
            crate::recording::OP_SHAPE_ENABLE_PRE_SOLVE_EVENTS,
            shape_id,
            flag,
        )
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_pre_solve_events = flag;
}

/// Returns true if pre-solve events are enabled.
/// (b2Shape_ArePreSolveEventsEnabled)
pub fn shape_are_pre_solve_events_enabled(world: &World, shape_id: ShapeId) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_pre_solve_events
}

/// Enable contact hit events for this shape. (b2Shape_EnableHitEvents)
pub fn shape_enable_hit_events(world: &mut World, shape_id: ShapeId, flag: bool) {
    crate::recording::record_op(world, |rec, _| {
        crate::recording::write_shape_bool(
            rec,
            crate::recording::OP_SHAPE_ENABLE_HIT_EVENTS,
            shape_id,
            flag,
        )
    });
    debug_assert!(!world.locked);
    if world.locked {
        return;
    }

    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_hit_events = flag;
}

/// Returns true if hit events are enabled. (b2Shape_AreHitEventsEnabled)
pub fn shape_are_hit_events_enabled(world: &World, shape_id: ShapeId) -> bool {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].enable_hit_events
}

/// Get the type of a shape. (b2Shape_GetType)
pub fn shape_get_type(world: &World, shape_id: ShapeId) -> ShapeType {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].shape_type()
}

/// Get the maximum capacity required for retrieving all the touching contacts
/// on a shape. (b2Shape_GetContactCapacity)
pub fn shape_get_contact_capacity(world: &World, shape_id: ShapeId) -> i32 {
    debug_assert!(!world.locked);
    if world.locked {
        return 0;
    }

    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];
    if shape.sensor_index != NULL_INDEX {
        return 0;
    }

    // Conservative and fast
    world.bodies[shape.body_id as usize].contact_count
}

/// Get the touching contact data for a shape. (b2Shape_GetContactData —
/// returns a Vec instead of filling a caller array)
pub fn shape_get_contact_data(
    world: &World,
    shape_id: ShapeId,
    capacity: usize,
) -> Vec<ContactData> {
    debug_assert!(!world.locked);
    if world.locked {
        return Vec::new();
    }

    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];
    if shape.sensor_index != NULL_INDEX {
        return Vec::new();
    }

    let mut out = Vec::new();
    let mut contact_key = world.bodies[shape.body_id as usize].head_contact_key;
    while contact_key != NULL_INDEX && out.len() < capacity {
        let contact_id = contact_key >> 1;
        let edge_index = contact_key & 1;

        let contact = &world.contacts[contact_id as usize];
        contact_key = contact.edges[edge_index as usize].next_key;

        // Does contact involve this shape and is it touching?
        if (contact.shape_id_a == shape_index || contact.shape_id_b == shape_index)
            && (contact.flags & crate::contact::contact_flags::TOUCHING) != 0
        {
            let shape_a = &world.shapes[contact.shape_id_a as usize];
            let shape_b = &world.shapes[contact.shape_id_b as usize];

            let contact_sim = if contact.set_index == AWAKE_SET && contact.color_index != NULL_INDEX
            {
                &world.constraint_graph.colors[contact.color_index as usize].contact_sims
                    [contact.local_index as usize]
            } else {
                &world.solver_sets[contact.set_index as usize].contact_sims
                    [contact.local_index as usize]
            };

            out.push(ContactData {
                contact_id: ContactId {
                    index1: contact_id + 1,
                    world0: world.world_id,
                    padding: 0,
                    generation: contact.generation,
                },
                shape_id_a: ShapeId {
                    index1: shape_a.id + 1,
                    world0: world.world_id,
                    generation: shape_a.generation,
                },
                shape_id_b: ShapeId {
                    index1: shape_b.id + 1,
                    world0: world.world_id,
                    generation: shape_b.generation,
                },
                manifold: contact_sim.manifold,
            });
        }
    }

    out
}

/// Get the maximum capacity required for retrieving all the overlapped shapes
/// on a sensor shape. Returns 0 if the shape is not a sensor.
/// (b2Shape_GetSensorCapacity)
pub fn shape_get_sensor_capacity(world: &World, shape_id: ShapeId) -> i32 {
    debug_assert!(!world.locked);
    if world.locked {
        return 0;
    }

    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];
    if shape.sensor_index == NULL_INDEX {
        return 0;
    }

    world.sensors[shape.sensor_index as usize].overlaps2.len() as i32
}

/// Get the overlapped shapes for a sensor shape. Overlaps may contain
/// destroyed shapes, so use [`shape_is_valid`] to confirm each overlap.
/// (b2Shape_GetSensorData — returns a Vec instead of filling a caller array)
pub fn shape_get_sensor_data(world: &World, shape_id: ShapeId, capacity: usize) -> Vec<ShapeId> {
    debug_assert!(!world.locked);
    if world.locked {
        return Vec::new();
    }

    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];
    if shape.sensor_index == NULL_INDEX {
        return Vec::new();
    }

    let sensor = &world.sensors[shape.sensor_index as usize];
    let count = sensor.overlaps2.len().min(capacity);
    sensor.overlaps2[..count]
        .iter()
        .map(|visitor| ShapeId {
            index1: visitor.shape_id + 1,
            world0: world.world_id,
            generation: visitor.generation,
        })
        .collect()
}

/// Get the current world AABB. This is the axis-aligned bounding box in world
/// coordinates. (b2Shape_GetAABB)
pub fn shape_get_aabb(world: &World, shape_id: ShapeId) -> Aabb {
    let shape_index = get_shape_index(world, shape_id);
    world.shapes[shape_index as usize].aabb
}

/// Compute the mass data for a shape. (b2Shape_ComputeMassData)
pub fn shape_compute_mass_data(world: &World, shape_id: ShapeId) -> MassData {
    let shape_index = get_shape_index(world, shape_id);
    compute_shape_mass(&world.shapes[shape_index as usize])
}

/// Get the closest point on a shape to a target point. Target and result are
/// in world space. (b2Shape_GetClosestPoint)
pub fn shape_get_closest_point(world: &World, shape_id: ShapeId, target: Pos) -> Pos {
    let shape_index = get_shape_index(world, shape_id);
    let shape = &world.shapes[shape_index as usize];
    let body = &world.bodies[shape.body_id as usize];
    let transform = get_body_transform_quick(world, body);

    // The target rides in as the frame of proxy B, so the relative pose is
    // differenced in double and the result stays exact far from the origin
    let zero = VEC2_ZERO;
    let target_transform = crate::math_functions::WorldTransform {
        p: target,
        q: ROT_IDENTITY,
    };

    let input = DistanceInput {
        proxy_a: make_shape_distance_proxy(shape),
        proxy_b: make_proxy(&[zero], 0.0),
        transform: inv_mul_world_transforms(transform, target_transform),
        use_radii: true,
    };

    let mut cache = SimplexCache::default();
    let output = shape_distance(&input, &mut cache, None);

    transform_world_point(transform, output.point_a)
}