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
// Port of the world data model from box2d-cpp-reference/src/physics_world.h
// (b2World, b2TaskContext) plus b2Profile from types.h. Logic from
// physics_world.c lands across the remaining bring-up commits.
//
// Porting decisions:
// - C keeps a global world registry (b2_worlds[B2_MAX_WORLDS], ids into it).
//   The Rust `World` is an owned object instead; `world_id`/`generation` are
//   kept so body/shape/joint ids remain bit-compatible with C. A registry
//   would force global mutable state through every call; ownership is the
//   sound Rust equivalent with identical observable behavior inside a world.
// - The arena allocator (b2Stack) is per-step scratch for performance; the
//   Rust solver allocates its scratch as Vecs in the step, so there is no
//   stack field.
// - Scheduler/task-system fields (enqueueTask, finishTask, userTaskContext,
//   userTreeTask, scheduler, activeTaskCount, taskCount) are deferred with the
//   single-threaded port; worker_count is kept and clamped like C.
// - b2Recording is the snapshot/replay subsystem, ported later; no field yet.
// - Pre-solve and custom-filter callbacks keep the C shape as Option<fn> with
//   a u64 context.
//
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT

use crate::bitset::BitSet;
use crate::body::Body;
use crate::broad_phase::BroadPhase;
use crate::constraint_graph::ConstraintGraph;
use crate::contact::Contact;
use crate::events::{
    BodyMoveEvent, ContactBeginTouchEvent, ContactEndTouchEvent, ContactHitEvent, JointEvent,
    SensorBeginTouchEvent, SensorEndTouchEvent,
};
use crate::id::ShapeId;
use crate::id_pool::IdPool;
use crate::island::Island;
use crate::joint::Joint;
use crate::math_functions::{Pos, Vec2};
use crate::sensor::{Sensor, SensorHit, SensorTaskContext};
use crate::shape::{ChainShape, Shape};
use crate::solver_set::SolverSet;
use crate::types::{Capacity, FrictionCallback, RestitutionCallback};

/// Prototype for a contact filter callback. Called when a contact pair is
/// considered for collision, if one of the two shapes has custom filtering
/// enabled. Return false to disable the collision. (b2CustomFilterFcn)
pub type CustomFilterFcn = fn(ShapeId, ShapeId, u64) -> bool;

/// Prototype for a pre-solve callback. Called after a contact is updated, only
/// for awake dynamic bodies with pre-solve events enabled. Return false to
/// disable the contact this step. (b2PreSolveFcn)
pub type PreSolveFcn = fn(ShapeId, ShapeId, Pos, Vec2, u64) -> bool;

/// Profiling data. Times are in milliseconds. (b2Profile)
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Profile {
    pub step: f32,
    pub pairs: f32,
    pub collide: f32,
    pub solve: f32,
    pub solver_setup: f32,
    pub constraints: f32,
    pub prepare_constraints: f32,
    pub integrate_velocities: f32,
    pub warm_start: f32,
    pub solve_impulses: f32,
    pub integrate_positions: f32,
    pub relax_impulses: f32,
    pub apply_restitution: f32,
    pub store_impulses: f32,
    pub split_islands: f32,
    pub transforms: f32,
    pub sensor_hits: f32,
    pub joint_events: f32,
    pub hit_events: f32,
    pub refit: f32,
    pub bullets: f32,
    pub sleep_islands: f32,
    pub sensors: f32,
}

/// Per thread task storage. (b2TaskContext)
#[derive(Debug, Clone, Default)]
pub struct TaskContext {
    /// Collect per thread sensor continuous hit events.
    pub sensor_hits: Vec<SensorHit>,

    /// These bits align with the contact id capacity and signal a change in
    /// contact status.
    pub contact_state_bit_set: BitSet,

    /// These bits align with the contact id capacity and signal a hit event.
    pub hit_event_bit_set: BitSet,

    /// Fast-path flag: true when this worker set at least one bit in
    /// hit_event_bit_set this step.
    pub has_hit_events: bool,

    /// These bits align with the joint id capacity and signal a change in
    /// joint status.
    pub joint_state_bit_set: BitSet,

    /// Used to track bodies with shapes that have enlarged AABBs. This avoids
    /// a bit array that is very large when there are many static shapes.
    pub enlarged_sim_bit_set: BitSet,

    /// Used to put islands to sleep.
    pub awake_island_bit_set: BitSet,

    /// Per worker split island candidate.
    pub split_sleep_time: f32,
    pub split_island_id: i32,

    /// Number of contacts recycled this step (collide pass).
    pub recycled_contact_count: i32,
}

/// The world struct manages all physics entities, dynamic simulation, and
/// asynchronous queries. (b2World)
#[derive(Debug)]
pub struct World {
    pub broad_phase: BroadPhase,
    pub constraint_graph: ConstraintGraph,

    /// The body id pool allocates and recycles body ids. Body ids provide a
    /// stable identifier for users. Aligns with `bodies`.
    pub body_id_pool: IdPool,

    /// Sparse array mapping body ids to the body data stored in solver sets.
    pub bodies: Vec<Body>,

    /// Provides free list for solver sets.
    pub solver_set_id_pool: IdPool,

    /// Solver sets store sims in contiguous arrays. Set 0 is static, set 1 is
    /// disabled, set 2 is awake; the rest are sleeping islands.
    pub solver_sets: Vec<SolverSet>,

    /// Used to create stable ids for joints.
    pub joint_id_pool: IdPool,

    /// Sparse array mapping joint ids to joints in the constraint graph or
    /// solver sets.
    pub joints: Vec<Joint>,

    /// Used to create stable ids for contacts.
    pub contact_id_pool: IdPool,

    /// Sparse array mapping contact ids to contacts in the constraint graph
    /// or solver sets.
    pub contacts: Vec<Contact>,

    /// Used to create stable ids for islands.
    pub island_id_pool: IdPool,

    /// Persistent islands.
    pub islands: Vec<Island>,

    pub shape_id_pool: IdPool,
    pub chain_id_pool: IdPool,

    /// Sparse arrays that point into the pools above.
    pub shapes: Vec<Shape>,
    pub chain_shapes: Vec<ChainShape>,

    /// Dense array of sensor data.
    pub sensors: Vec<Sensor>,

    /// Per thread storage (one entry in the single-threaded port).
    pub task_contexts: Vec<TaskContext>,
    pub sensor_task_contexts: Vec<SensorTaskContext>,

    pub body_move_events: Vec<BodyMoveEvent>,
    pub sensor_begin_events: Vec<SensorBeginTouchEvent>,
    pub contact_begin_events: Vec<ContactBeginTouchEvent>,

    /// End events are double buffered so that the user doesn't need to flush
    /// events.
    pub sensor_end_events: [Vec<SensorEndTouchEvent>; 2],
    pub contact_end_events: [Vec<ContactEndTouchEvent>; 2],
    pub end_event_array_index: i32,

    pub contact_hit_events: Vec<ContactHitEvent>,
    pub joint_events: Vec<JointEvent>,

    /// Used to track debug draw.
    pub debug_body_set: BitSet,
    pub debug_joint_set: BitSet,
    pub debug_contact_set: BitSet,
    pub debug_island_set: BitSet,

    /// Id that is incremented every time step.
    pub step_index: u64,

    /// Identify islands for splitting.
    pub split_island_id: i32,

    pub gravity: Vec2,
    pub hit_event_threshold: f32,
    pub restitution_threshold: f32,
    pub max_linear_speed: f32,
    pub contact_speed: f32,
    pub contact_hertz: f32,
    pub contact_damping_ratio: f32,
    pub contact_recycle_distance: f32,

    pub friction_callback: Option<FrictionCallback>,
    pub restitution_callback: Option<RestitutionCallback>,

    pub generation: u16,

    pub profile: Profile,

    pub max_capacity: Capacity,

    pub pre_solve_fcn: Option<PreSolveFcn>,
    pub pre_solve_context: u64,

    pub custom_filter_fcn: Option<CustomFilterFcn>,
    pub custom_filter_context: u64,

    pub worker_count: i32,

    pub user_data: u64,

    /// Inverse sub-step, remembered for reporting forces and torques.
    pub inv_h: f32,

    /// Inverse full-step.
    pub inv_dt: f32,

    pub world_id: u16,

    pub enable_sleep: bool,
    pub locked: bool,
    pub enable_warm_starting: bool,
    pub enable_contact_softening: bool,
    pub enable_continuous: bool,
    pub enable_speculative: bool,
    pub in_use: bool,

    /// Active recording session; owned by the world between
    /// world_start_recording and world_stop_recording. (C: b2Recording*)
    pub recording: Option<crate::recording::Recording>,
}

/// Default friction mixing: `sqrt(frictionA * frictionB)`.
/// (static b2DefaultFrictionCallback)
pub fn default_friction_callback(
    friction_a: f32,
    _material_a: u64,
    friction_b: f32,
    _material_b: u64,
) -> f32 {
    (friction_a * friction_b).sqrt()
}

/// Default restitution mixing: `max(restitutionA, restitutionB)`.
/// (static b2DefaultRestitutionCallback)
pub fn default_restitution_callback(
    restitution_a: f32,
    _material_a: u64,
    restitution_b: f32,
    _material_b: u64,
) -> f32 {
    crate::math_functions::max_float(restitution_a, restitution_b)
}

impl World {
    /// Create a world. (b2CreateWorld)
    ///
    /// Differences from C, all documented in the module header: there is no
    /// global world registry (the returned World is owned; `world_id` stays 0
    /// unless the embedder assigns one), no arena stack, and the serial task
    /// path is always used (worker_count = 1 with one task context), which is
    /// the C fallback when no task system is supplied.
    pub fn new(def: &crate::types::WorldDef) -> World {
        use crate::constants::contact_recycle_distance;
        use crate::constraint_graph::ConstraintGraph;
        use crate::math_functions::max_int;

        debug_assert!(def.internal_value == crate::core::SECRET_COOKIE);

        let body_capacity = max_int(
            16,
            def.capacity.static_body_count + def.capacity.dynamic_body_count,
        ) as usize;
        let shape_capacity = max_int(
            16,
            def.capacity.static_shape_count + def.capacity.dynamic_shape_count,
        ) as usize;
        let contact_capacity = max_int(16, def.capacity.contact_count) as usize;

        let mut solver_set_id_pool = IdPool::new();
        let mut solver_sets: Vec<SolverSet> = Vec::with_capacity(8);

        // add empty static, disabled, and awake body sets
        // static set
        let mut set = SolverSet {
            set_index: solver_set_id_pool.alloc_id(),
            ..Default::default()
        };
        set.body_sims
            .reserve(max_int(16, def.capacity.static_body_count) as usize);
        solver_sets.push(set);
        debug_assert!(
            solver_sets[crate::solver_set::STATIC_SET as usize].set_index
                == crate::solver_set::STATIC_SET
        );

        // disabled set
        solver_sets.push(SolverSet {
            set_index: solver_set_id_pool.alloc_id(),
            ..Default::default()
        });
        debug_assert!(
            solver_sets[crate::solver_set::DISABLED_SET as usize].set_index
                == crate::solver_set::DISABLED_SET
        );

        // awake set
        let mut awake = SolverSet {
            set_index: solver_set_id_pool.alloc_id(),
            ..Default::default()
        };
        awake
            .body_sims
            .reserve(max_int(16, def.capacity.dynamic_body_count) as usize);
        awake
            .body_states
            .reserve(max_int(16, def.capacity.dynamic_body_count) as usize);
        awake.contact_sims.reserve(contact_capacity);
        solver_sets.push(awake);
        debug_assert!(
            solver_sets[crate::solver_set::AWAKE_SET as usize].set_index
                == crate::solver_set::AWAKE_SET
        );

        World {
            broad_phase: BroadPhase::new(&def.capacity),
            constraint_graph: ConstraintGraph::new(&def.capacity),
            body_id_pool: IdPool::new(),
            bodies: Vec::with_capacity(body_capacity),
            solver_set_id_pool,
            solver_sets,
            joint_id_pool: IdPool::new(),
            joints: Vec::with_capacity(16),
            contact_id_pool: IdPool::new(),
            contacts: Vec::with_capacity(contact_capacity),
            island_id_pool: IdPool::new(),
            islands: Vec::with_capacity(max_int(16, def.capacity.dynamic_body_count) as usize),
            shape_id_pool: IdPool::new(),
            chain_id_pool: IdPool::new(),
            shapes: Vec::with_capacity(shape_capacity),
            chain_shapes: Vec::with_capacity(4),
            sensors: Vec::with_capacity(4),
            // Serial fallback: one worker context. (b2CreateWorkerContexts)
            task_contexts: vec![TaskContext::default()],
            sensor_task_contexts: vec![SensorTaskContext::default()],
            body_move_events: Vec::with_capacity(4),
            sensor_begin_events: Vec::with_capacity(4),
            contact_begin_events: Vec::with_capacity(4),
            sensor_end_events: [Vec::with_capacity(4), Vec::with_capacity(4)],
            contact_end_events: [Vec::with_capacity(4), Vec::with_capacity(4)],
            end_event_array_index: 0,
            contact_hit_events: Vec::with_capacity(4),
            joint_events: Vec::with_capacity(4),
            debug_body_set: BitSet::new(256),
            debug_joint_set: BitSet::new(256),
            debug_contact_set: BitSet::new(256),
            debug_island_set: BitSet::new(256),
            step_index: 0,
            split_island_id: crate::core::NULL_INDEX,
            gravity: def.gravity,
            hit_event_threshold: def.hit_event_threshold,
            restitution_threshold: def.restitution_threshold,
            max_linear_speed: def.maximum_linear_speed,
            contact_speed: def.contact_speed,
            contact_hertz: def.contact_hertz,
            contact_damping_ratio: def.contact_damping_ratio,
            contact_recycle_distance: contact_recycle_distance(),
            friction_callback: Some(def.friction_callback.unwrap_or(default_friction_callback)),
            restitution_callback: Some(
                def.restitution_callback
                    .unwrap_or(default_restitution_callback),
            ),
            generation: 0,
            profile: Profile::default(),
            max_capacity: def.capacity,
            pre_solve_fcn: None,
            pre_solve_context: 0,
            custom_filter_fcn: None,
            custom_filter_context: 0,
            worker_count: 1,
            user_data: def.user_data,
            inv_h: 0.0,
            inv_dt: 0.0,
            world_id: 0,
            enable_sleep: def.enable_sleep,
            locked: false,
            enable_warm_starting: true,
            enable_contact_softening: def.enable_contact_softening,
            enable_continuous: def.enable_continuous,
            enable_speculative: true,
            in_use: true,
            recording: None,
        }
    }

    /// Validate the solver-set bookkeeping. (b2ValidateSolverSets)
    ///
    /// bring-up: the C version (physics_world.c, compiled only with
    /// B2_ENABLE_VALIDATION) also cross-checks contacts, joints, and graph
    /// colors; those checks are added as their slices land. This subset
    /// validates the body <-> sim <-> set <-> island mapping.
    pub fn validate_solver_sets(&self) {
        use crate::core::NULL_INDEX;

        let mut active_body_count = 0;
        for (set_index, set) in self.solver_sets.iter().enumerate() {
            if set.set_index == NULL_INDEX {
                // free slot
                debug_assert!(set.body_sims.is_empty());
                debug_assert!(set.body_states.is_empty());
                debug_assert!(set.island_sims.is_empty());
                continue;
            }

            debug_assert!(set.set_index == set_index as i32);

            if set_index == crate::solver_set::AWAKE_SET as usize {
                debug_assert!(set.body_sims.len() == set.body_states.len());
            } else {
                debug_assert!(set.body_states.is_empty());
            }

            for (local_index, sim) in set.body_sims.iter().enumerate() {
                let body = &self.bodies[sim.body_id as usize];
                debug_assert!(body.set_index == set_index as i32);
                debug_assert!(body.local_index == local_index as i32);
                debug_assert!(body.id == sim.body_id);
                let _ = (body, local_index);
            }
            active_body_count += set.body_sims.len() as i32;

            for (local_index, island_sim) in set.island_sims.iter().enumerate() {
                let island = &self.islands[island_sim.island_id as usize];
                debug_assert!(island.set_index == set_index as i32);
                debug_assert!(island.local_index == local_index as i32);
                let _ = (island, local_index);
            }
        }

        debug_assert!(active_body_count == self.body_id_pool.id_count());
        let _ = active_body_count;
    }
}

mod api;
mod draw;
mod query;
mod step;

pub use api::*;
pub use draw::*;
pub use query::*;
pub use step::*;