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
//! Port of box3d-cpp-reference/src/sensor.c + sensor.h.
//!
//! SPDX-FileCopyrightText: 2025 Erin Catto
//! SPDX-License-Identifier: MIT

use crate::bitset::BitSet;
use crate::body::get_body_transform;
use crate::compound::overlap_compound;
use crate::constants::MAX_SHAPE_CAST_POINTS;
use crate::core::NULL_INDEX;
use crate::distance::{make_proxy, ShapeProxy};
use crate::events::{SensorBeginTouchEvent, SensorEndTouchEvent};
use crate::geometry::{overlap_capsule, overlap_sphere, ShapeType};
use crate::height_field::overlap_height_field;
use crate::hull::{get_hull_points, overlap_hull};
use crate::id::ShapeId;
use crate::math_functions::{
    inv_mul_transforms, min_int, to_relative_transform, transform_point, Transform, POS_ZERO,
    TRANSFORM_IDENTITY,
};
use crate::mesh::{overlap_mesh, Mesh};
use crate::shape::{shape_flags, should_shapes_collide, Shape, ShapeGeometry};
use crate::solver_set::DISABLED_SET;
use crate::types::BodyType;
use crate::world::World;

/// Used to track shapes that hit sensors using time of impact. (b3SensorHit)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SensorHit {
    pub sensor_id: i32,
    pub visitor_id: i32,
}

impl Default for SensorHit {
    fn default() -> Self {
        SensorHit {
            sensor_id: NULL_INDEX,
            visitor_id: NULL_INDEX,
        }
    }
}

/// (b3Visitor)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Visitor {
    pub shape_id: i32,
    pub generation: u16,
}

impl Default for Visitor {
    fn default() -> Self {
        Visitor {
            shape_id: NULL_INDEX,
            generation: 0,
        }
    }
}

/// Sensors are shapes that live in the broad-phase but never have contacts.
/// (b3Sensor)
#[derive(Debug, Clone, Default)]
pub struct Sensor {
    pub hits: Vec<Visitor>,
    pub overlaps1: Vec<Visitor>,
    pub overlaps2: Vec<Visitor>,
    pub shape_id: i32,
}

impl Sensor {
    pub fn new(shape_id: i32) -> Sensor {
        Sensor {
            hits: Vec::with_capacity(4),
            overlaps1: Vec::with_capacity(16),
            overlaps2: Vec::with_capacity(16),
            shape_id,
        }
    }
}

/// (b3SensorTaskContext)
#[derive(Debug, Clone, Default)]
pub struct SensorTaskContext {
    pub event_bits: BitSet,
}

/// Shape proxy for a convex visitor. (b3MakeShapeProxy)
fn make_shape_proxy(shape: &Shape) -> ShapeProxy {
    match &shape.geometry {
        ShapeGeometry::Capsule(capsule) => {
            make_proxy(&[capsule.center1, capsule.center2], capsule.radius)
        }
        ShapeGeometry::Sphere(sphere) => make_proxy(&[sphere.center], sphere.radius),
        ShapeGeometry::Hull(hull) => {
            let points = get_hull_points(hull);
            make_proxy(points, 0.0)
        }
        _ => {
            debug_assert!(false, "make_shape_proxy: unsupported visitor shape type");
            ShapeProxy::default()
        }
    }
}

/// Overlap test with the visitor expressed in the sensor's local frame.
/// (static b3OverlapSensor)
fn overlap_sensor(
    sensor_shape: &Shape,
    sensor_transform: Transform,
    visitor_shape: &Shape,
    visitor_transform: Transform,
) -> bool {
    let proxy = make_shape_proxy(visitor_shape);
    let relative_transform = inv_mul_transforms(sensor_transform, visitor_transform);

    let mut local_proxy = ShapeProxy {
        count: min_int(proxy.count, MAX_SHAPE_CAST_POINTS as i32),
        radius: proxy.radius,
        ..ShapeProxy::default()
    };
    for i in 0..local_proxy.count as usize {
        local_proxy.points[i] = transform_point(relative_transform, proxy.points[i]);
    }

    match &sensor_shape.geometry {
        ShapeGeometry::Capsule(capsule) => {
            overlap_capsule(capsule, TRANSFORM_IDENTITY, &local_proxy)
        }
        ShapeGeometry::Compound(compound) => {
            overlap_compound(compound, TRANSFORM_IDENTITY, &local_proxy)
        }
        ShapeGeometry::HeightField(hf) => {
            overlap_height_field(hf, TRANSFORM_IDENTITY, &local_proxy)
        }
        ShapeGeometry::Hull(hull) => overlap_hull(hull, TRANSFORM_IDENTITY, &local_proxy),
        ShapeGeometry::Mesh { data, scale } => {
            let mesh = Mesh::new(data, *scale);
            overlap_mesh(&mesh, TRANSFORM_IDENTITY, &local_proxy)
        }
        ShapeGeometry::Sphere(sphere) => overlap_sphere(sphere, TRANSFORM_IDENTITY, &local_proxy),
    }
}

/// Broad-phase visitor filter + exact overlap for one candidate.
fn sensor_accepts_visitor(
    world: &World,
    sensor_shape: &Shape,
    sensor_transform: Transform,
    visitor_shape_id: i32,
) -> bool {
    let sensor_shape_id = sensor_shape.id;
    if visitor_shape_id == sensor_shape_id {
        return false;
    }

    let other_shape = &world.shapes[visitor_shape_id as usize];
    let other_type = other_shape.shape_type();
    let sensor_type = sensor_shape.shape_type();
    if (other_type == ShapeType::Mesh || other_type == ShapeType::Height)
        && (sensor_type == ShapeType::Mesh || sensor_type == ShapeType::Height)
    {
        return false;
    }

    if (other_shape.flags & shape_flags::ENABLE_SENSOR_EVENTS) == 0 {
        return false;
    }
    if other_shape.body_id == sensor_shape.body_id {
        return false;
    }
    if !should_shapes_collide(sensor_shape.filter, other_shape.filter) {
        return false;
    }

    if (sensor_shape.flags & shape_flags::ENABLE_CUSTOM_FILTERING) != 0
        || (other_shape.flags & shape_flags::ENABLE_CUSTOM_FILTERING) != 0
    {
        if let Some(custom_filter_fcn) = world.custom_filter_fcn {
            let id_a = ShapeId {
                index1: sensor_shape_id + 1,
                world0: world.world_id,
                generation: sensor_shape.generation,
            };
            let id_b = ShapeId {
                index1: visitor_shape_id + 1,
                world0: world.world_id,
                generation: other_shape.generation,
            };
            if !custom_filter_fcn(world, id_a, id_b, world.custom_filter_context) {
                return false;
            }
        }
    }

    let other_transform =
        to_relative_transform(get_body_transform(world, other_shape.body_id), POS_ZERO);
    overlap_sensor(sensor_shape, sensor_transform, other_shape, other_transform)
}

/// Serial sensor overlap pass for `[start, end)`. (static b3SensorTask)
fn sensor_task(world: &mut World, start_index: usize, end_index: usize) {
    debug_assert!(start_index < end_index);

    for sensor_index in start_index..end_index {
        {
            let sensor = &mut world.sensors[sensor_index];
            std::mem::swap(&mut sensor.overlaps1, &mut sensor.overlaps2);
            sensor.overlaps2.clear();
            sensor.overlaps2.append(&mut sensor.hits);
        }

        let shape_id = world.sensors[sensor_index].shape_id;
        let body_id = world.shapes[shape_id as usize].body_id;
        let body_set_index = world.bodies[body_id as usize].set_index;
        let sensor_events_enabled =
            (world.shapes[shape_id as usize].flags & shape_flags::ENABLE_SENSOR_EVENTS) != 0;

        if body_set_index == DISABLED_SET || !sensor_events_enabled {
            if !world.sensors[sensor_index].overlaps1.is_empty() {
                world.sensor_task_contexts[0]
                    .event_bits
                    .set_bit(sensor_index as u32);
            }
            continue;
        }

        let transform = to_relative_transform(get_body_transform(world, body_id), POS_ZERO);
        debug_assert!(world.shapes[shape_id as usize].sensor_index == sensor_index as i32);

        let query_bounds = world.shapes[shape_id as usize].aabb;
        let mask_bits = world.shapes[shape_id as usize].filter.mask_bits;

        let mut candidates = Vec::new();
        for tree_index in 0..BodyType::Dynamic as usize + 1 {
            world.broad_phase.trees[tree_index].query(
                query_bounds,
                mask_bits,
                false,
                |_proxy_id, user_data| {
                    candidates.push(user_data as i32);
                    true
                },
            );
        }

        for visitor_shape_id in candidates {
            let accepted = {
                let sensor_shape = &world.shapes[shape_id as usize];
                sensor_accepts_visitor(world, sensor_shape, transform, visitor_shape_id)
            };
            if accepted {
                let generation = world.shapes[visitor_shape_id as usize].generation;
                world.sensors[sensor_index].overlaps2.push(Visitor {
                    shape_id: visitor_shape_id,
                    generation,
                });
            }
        }

        world.sensors[sensor_index]
            .overlaps2
            .sort_unstable_by_key(|a| a.shape_id);

        {
            let overlaps = &mut world.sensors[sensor_index].overlaps2;
            let mut unique_count = 0usize;
            for i in 0..overlaps.len() {
                if unique_count == 0 || overlaps[i].shape_id != overlaps[unique_count - 1].shape_id
                {
                    overlaps[unique_count] = overlaps[i];
                    unique_count += 1;
                }
            }
            overlaps.truncate(unique_count);
        }

        let count1 = world.sensors[sensor_index].overlaps1.len();
        let count2 = world.sensors[sensor_index].overlaps2.len();
        if count1 != count2 {
            world.sensor_task_contexts[0]
                .event_bits
                .set_bit(sensor_index as u32);
        } else {
            let changed = (0..count1).any(|i| {
                let s1 = world.sensors[sensor_index].overlaps1[i];
                let s2 = world.sensors[sensor_index].overlaps2[i];
                s1.shape_id != s2.shape_id || s1.generation != s2.generation
            });
            if changed {
                world.sensor_task_contexts[0]
                    .event_bits
                    .set_bit(sensor_index as u32);
            }
        }
    }
}

fn publish_sensor_events(world: &mut World) {
    let bits: Vec<u64> = {
        let bit_set = &world.sensor_task_contexts[0].event_bits;
        (0..bit_set.block_count())
            .map(|k| bit_set.block(k))
            .collect()
    };
    let world_id = world.world_id;
    let end_index = world.end_event_array_index as usize;

    for (k, mut word) in bits.into_iter().enumerate() {
        while word != 0 {
            let ctz = word.trailing_zeros();
            let sensor_index = (64 * k as u32 + ctz) as usize;

            let shape_id = world.sensors[sensor_index].shape_id;
            let sensor_generation = world.shapes[shape_id as usize].generation;
            let sensor_id = ShapeId {
                index1: shape_id + 1,
                world0: world_id,
                generation: sensor_generation,
            };

            let count1 = world.sensors[sensor_index].overlaps1.len();
            let count2 = world.sensors[sensor_index].overlaps2.len();

            let mut index1 = 0usize;
            let mut index2 = 0usize;
            while index1 < count1 && index2 < count2 {
                let r1 = world.sensors[sensor_index].overlaps1[index1];
                let r2 = world.sensors[sensor_index].overlaps2[index2];
                if r1.shape_id == r2.shape_id {
                    if r1.generation < r2.generation {
                        world.sensor_end_events[end_index].push(SensorEndTouchEvent {
                            sensor_shape_id: sensor_id,
                            visitor_shape_id: ShapeId {
                                index1: r1.shape_id + 1,
                                world0: world_id,
                                generation: r1.generation,
                            },
                        });
                        index1 += 1;
                    } else if r1.generation > r2.generation {
                        world.sensor_begin_events.push(SensorBeginTouchEvent {
                            sensor_shape_id: sensor_id,
                            visitor_shape_id: ShapeId {
                                index1: r2.shape_id + 1,
                                world0: world_id,
                                generation: r2.generation,
                            },
                        });
                        index2 += 1;
                    } else {
                        index1 += 1;
                        index2 += 1;
                    }
                } else if r1.shape_id < r2.shape_id {
                    world.sensor_end_events[end_index].push(SensorEndTouchEvent {
                        sensor_shape_id: sensor_id,
                        visitor_shape_id: ShapeId {
                            index1: r1.shape_id + 1,
                            world0: world_id,
                            generation: r1.generation,
                        },
                    });
                    index1 += 1;
                } else {
                    world.sensor_begin_events.push(SensorBeginTouchEvent {
                        sensor_shape_id: sensor_id,
                        visitor_shape_id: ShapeId {
                            index1: r2.shape_id + 1,
                            world0: world_id,
                            generation: r2.generation,
                        },
                    });
                    index2 += 1;
                }
            }

            while index1 < count1 {
                let r1 = world.sensors[sensor_index].overlaps1[index1];
                world.sensor_end_events[end_index].push(SensorEndTouchEvent {
                    sensor_shape_id: sensor_id,
                    visitor_shape_id: ShapeId {
                        index1: r1.shape_id + 1,
                        world0: world_id,
                        generation: r1.generation,
                    },
                });
                index1 += 1;
            }

            while index2 < count2 {
                let r2 = world.sensors[sensor_index].overlaps2[index2];
                world.sensor_begin_events.push(SensorBeginTouchEvent {
                    sensor_shape_id: sensor_id,
                    visitor_shape_id: ShapeId {
                        index1: r2.shape_id + 1,
                        world0: world_id,
                        generation: r2.generation,
                    },
                });
                index2 += 1;
            }

            word &= word - 1;
        }
    }
}

/// Query all sensors for overlaps and emit begin/end events. (b3OverlapSensors)
pub fn overlap_sensors(world: &mut World) {
    let sensor_count = world.sensors.len();
    if sensor_count == 0 {
        return;
    }

    debug_assert!(!world.sensor_task_contexts.is_empty());
    world.sensor_task_contexts[0]
        .event_bits
        .set_bit_count_and_clear(sensor_count as u32);

    sensor_task(world, 0, sensor_count);
    publish_sensor_events(world);
}

/// Flush pending end events and remove a sensor from the dense array.
/// (b3DestroySensor)
pub fn destroy_sensor(world: &mut World, sensor_shape_id: i32) {
    let sensor_index = world.shapes[sensor_shape_id as usize].sensor_index;
    debug_assert!(sensor_index != NULL_INDEX);

    let world_id = world.world_id;
    let generation = world.shapes[sensor_shape_id as usize].generation;
    let end_index = world.end_event_array_index as usize;

    let overlaps: Vec<_> = world.sensors[sensor_index as usize].overlaps2.clone();
    for visitor in overlaps {
        world.sensor_end_events[end_index].push(SensorEndTouchEvent {
            sensor_shape_id: ShapeId {
                index1: sensor_shape_id + 1,
                world0: world_id,
                generation,
            },
            visitor_shape_id: ShapeId {
                index1: visitor.shape_id + 1,
                world0: world_id,
                generation: visitor.generation,
            },
        });
    }

    world.sensors[sensor_index as usize].hits.clear();
    world.sensors[sensor_index as usize].overlaps1.clear();
    world.sensors[sensor_index as usize].overlaps2.clear();

    let last = world.sensors.len() as i32 - 1;
    world.sensors.swap_remove(sensor_index as usize);
    if sensor_index < last {
        let moved_shape_id = world.sensors[sensor_index as usize].shape_id;
        world.shapes[moved_shape_id as usize].sensor_index = sensor_index;
    }
}