boxdd 0.6.0

Safe, ergonomic Rust bindings for Box2D v3
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
use super::reads::*;
use super::*;

#[inline]
fn check_world_step_args_valid(time_step: f32, sub_steps: i32) -> crate::error::Result<()> {
    if !crate::is_valid_float(time_step) || time_step < 0.0 {
        return Err(crate::error::Error::invalid_argument(
            "World::step",
            "time_step",
            "a finite value greater than or equal to zero",
        ));
    }
    if sub_steps <= 0 {
        return Err(crate::error::Error::invalid_argument(
            "World::step",
            "sub_steps",
            "an integer greater than zero",
        ));
    }
    Ok(())
}

pub(crate) fn world_set_gravity<V: Into<Vec2>>(
    world: crate::world::WorldCall<'_>,
    gravity: V,
) -> crate::error::Result<()> {
    let gravity = gravity.into();
    check_world_gravity_valid("World::set_gravity", "gravity", gravity)?;
    unsafe { ffi::b2World_SetGravity(world.raw_world(), gravity.into_raw()) };
    Ok(())
}

pub(crate) fn world_enable_sleeping(
    world: crate::world::WorldCall<'_>,
    flag: bool,
) -> crate::error::Result<()> {
    unsafe { ffi::b2World_EnableSleeping(world.raw_world(), flag) };
    Ok(())
}

pub(crate) fn world_enable_continuous(
    world: crate::world::WorldCall<'_>,
    flag: bool,
) -> crate::error::Result<()> {
    unsafe { ffi::b2World_EnableContinuous(world.raw_world(), flag) };
    Ok(())
}

pub(crate) fn world_enable_warm_starting(
    world: crate::world::WorldCall<'_>,
    flag: bool,
) -> crate::error::Result<()> {
    unsafe { ffi::b2World_EnableWarmStarting(world.raw_world(), flag) };
    Ok(())
}

pub(crate) fn world_set_restitution_threshold(
    world: crate::world::WorldCall<'_>,
    value: f32,
) -> crate::error::Result<()> {
    check_non_negative_finite_world_scalar("World::set_restitution_threshold", "value", value)?;
    unsafe { ffi::b2World_SetRestitutionThreshold(world.raw_world(), value) };
    Ok(())
}

pub(crate) fn world_set_hit_event_threshold(
    world: crate::world::WorldCall<'_>,
    value: f32,
) -> crate::error::Result<()> {
    check_non_negative_finite_world_scalar("World::set_hit_event_threshold", "value", value)?;
    unsafe { ffi::b2World_SetHitEventThreshold(world.raw_world(), value) };
    Ok(())
}

pub(crate) fn world_set_contact_tuning(
    world: crate::world::WorldCall<'_>,
    hertz: f32,
    damping_ratio: f32,
    push_speed: f32,
) -> crate::error::Result<()> {
    check_non_negative_finite_world_scalar("World::set_contact_tuning", "hertz", hertz)?;
    check_non_negative_finite_world_scalar(
        "World::set_contact_tuning",
        "damping_ratio",
        damping_ratio,
    )?;
    check_non_negative_finite_world_scalar("World::set_contact_tuning", "push_speed", push_speed)?;
    unsafe { ffi::b2World_SetContactTuning(world.raw_world(), hertz, damping_ratio, push_speed) };
    Ok(())
}

pub(crate) fn world_set_contact_recycle_distance(
    world: crate::world::WorldCall<'_>,
    distance: f32,
) -> crate::error::Result<()> {
    check_non_negative_finite_world_scalar(
        "World::set_contact_recycle_distance",
        "distance",
        distance,
    )?;
    unsafe { ffi::b2World_SetContactRecycleDistance(world.raw_world(), distance) };
    Ok(())
}

pub(crate) fn world_set_maximum_linear_speed(
    world: crate::world::WorldCall<'_>,
    value: f32,
) -> crate::error::Result<()> {
    check_positive_finite_world_linear_speed("World::set_maximum_linear_speed", "value", value)?;
    unsafe { ffi::b2World_SetMaximumLinearSpeed(world.raw_world(), value) };
    Ok(())
}

impl World {
    fn step_validated<'world>(
        &'world mut self,
        time_step: f32,
        sub_steps: i32,
    ) -> crate::error::Result<(
        crate::events::PendingStepPublication<'world>,
        Option<crate::Error>,
    )> {
        // Surface any failure left by an exceptional prior worker boundary before changing the
        // contact epoch or completed-step publication state.
        #[cfg(not(target_arch = "wasm32"))]
        {
            self.core.worker_callbacks.begin_call()?;
        }

        // Prepare every worker-visible callback artifact before advancing any Rust epoch or
        // mutating native state. A failed identity snapshot therefore leaves the complete step
        // transaction untouched. Worlds without shape callbacks skip the snapshot entirely.
        #[cfg(not(target_arch = "wasm32"))]
        let prepared_callbacks = self.prepare_step_callbacks()?;

        // Advance before entering Box2D so every contact produced by this step receives one epoch.
        // Exhaustion poisons the world and returns before any native mutation occurs.
        let contact_epoch = crate::core::world_core::WorldCore::advance_contact_epoch(&self.core)?;
        self.completed_step_state().begin_step();
        let world = self.raw();
        let publication = crate::core::callback_state::run_world_step_boundary(
            crate::core::callback_state::CallbackOwnerToken::world(self.core.brand.token()),
            || {
                #[cfg(not(target_arch = "wasm32"))]
                let active_callbacks = prepared_callbacks.install(world);
                unsafe { ffi::b2World_Step(world, time_step, sub_steps) };
                // Box2D has synchronously finished every task before returning. Unpublish the
                // native pointers before owner-side cleanup or any later world mutation can begin.
                #[cfg(not(target_arch = "wasm32"))]
                active_callbacks.finish();

                // Publish before owner cleanup so callback-time destruction of primary-world
                // objects is deferred until the completed-step capability ends. The guard retires
                // the publication if cleanup resumes a panic before a capability can be returned.
                self.completed_step_state()
                    .publish_pending(&self.core, contact_epoch)
            },
            |publication, _panic| {
                #[cfg(not(target_arch = "wasm32"))]
                self.core.worker_callbacks.drain_panics(_panic);
                publication
            },
        );
        #[cfg(not(target_arch = "wasm32"))]
        let post_step_error = self.core.worker_callbacks.take_error();
        #[cfg(target_arch = "wasm32")]
        let post_step_error = None;
        ::core::result::Result::Ok((publication, post_step_error))
    }

    /// Step the simulation by `time_step` seconds using `sub_steps` sub-steps.
    ///
    /// The returned capability proves native simulation advanced and keeps the world exclusively
    /// borrowed. Native event getters and ID mapping run only for event families requested through
    /// that capability. Inspect [`crate::CompletedStep::post_step_error`] for callback or task
    /// failures discovered after native advancement; an outer error means Box2D was not called.
    pub fn step(
        &mut self,
        time_step: f32,
        sub_steps: i32,
    ) -> crate::error::Result<crate::events::CompletedStep<'_>> {
        check_world_available(self)?;
        check_world_step_args_valid(time_step, sub_steps)?;
        let (publication, post_step_error) = self.step_validated(time_step, sub_steps)?;
        let contact_epoch = publication.commit();
        core::result::Result::Ok(crate::events::CompletedStep::after_validated_step(
            self,
            contact_epoch,
            post_step_error,
        ))
    }

    pub(crate) fn step_while_recording_after_preflight(
        &mut self,
        time_step: f32,
        sub_steps: i32,
    ) -> crate::error::Result<(
        crate::events::PendingStepPublication<'_>,
        Option<crate::Error>,
    )> {
        check_world_step_args_valid(time_step, sub_steps)?;
        self.step_validated(time_step, sub_steps)
    }

    /// Set gravity vector.
    pub fn set_gravity<V: Into<Vec2>>(&mut self, g: V) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_set_gravity(world, g))
    }

    /// Get current gravity vector.
    pub fn gravity(&self) -> crate::error::Result<Vec2> {
        check_world_available(self)?;
        world_gravity_impl(self.raw())
    }

    /// World counters snapshot (sizes, tree heights, etc.).
    pub fn counters(&self) -> crate::error::Result<Counters> {
        check_world_available(self)?;
        world_counters_impl(self.raw())
    }

    /// World profile snapshot with per-stage timing in milliseconds from the last completed step.
    pub fn profile(&self) -> crate::error::Result<Profile> {
        check_world_available(self)?;
        world_profile_impl(self.raw())
    }

    /// Get number of awake bodies.
    pub fn awake_body_count(&self) -> crate::error::Result<i32> {
        check_world_available(self)?;
        world_awake_body_count_impl(self.raw())
    }

    // Runtime configuration helpers mirroring WorldDef fields
    pub fn enable_sleeping(&mut self, flag: bool) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_enable_sleeping(world, flag))
    }

    pub fn enable_continuous(&mut self, flag: bool) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_enable_continuous(world, flag))
    }

    /// Enable or disable constraint warm starting at runtime.
    ///
    /// Warm starting seeds the solver with accumulated impulses from the previous
    /// step to improve stability and convergence. Disabling this is only useful
    /// for experiments and will significantly reduce stability in most scenes.
    pub fn enable_warm_starting(&mut self, flag: bool) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_enable_warm_starting(world, flag))
    }

    pub fn set_restitution_threshold(&mut self, value: f32) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_set_restitution_threshold(world, value))
    }

    pub fn set_hit_event_threshold(&mut self, value: f32) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_set_hit_event_threshold(world, value))
    }

    pub fn set_contact_tuning(
        &mut self,
        hertz: f32,
        damping_ratio: f32,
        push_speed: f32,
    ) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| {
            world_set_contact_tuning(world, hertz, damping_ratio, push_speed)
        })
    }

    pub fn set_maximum_linear_speed(&mut self, v: f32) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| world_set_maximum_linear_speed(world, v))
    }

    pub fn is_sleeping_enabled(&self) -> crate::error::Result<bool> {
        check_world_available(self)?;
        Ok(world_is_sleeping_enabled_impl(self.raw()))
    }

    pub fn is_continuous_enabled(&self) -> crate::error::Result<bool> {
        check_world_available(self)?;
        Ok(world_is_continuous_enabled_impl(self.raw()))
    }

    /// Returns true if constraint warm starting is enabled.
    pub fn is_warm_starting_enabled(&self) -> crate::error::Result<bool> {
        check_world_available(self)?;
        Ok(world_is_warm_starting_enabled_impl(self.raw()))
    }

    pub fn restitution_threshold(&self) -> crate::error::Result<f32> {
        check_world_available(self)?;
        world_restitution_threshold_impl(self.raw())
    }

    pub fn hit_event_threshold(&self) -> crate::error::Result<f32> {
        check_world_available(self)?;
        world_hit_event_threshold_impl(self.raw())
    }

    pub fn maximum_linear_speed(&self) -> crate::error::Result<f32> {
        check_world_available(self)?;
        world_maximum_linear_speed_impl(self.raw())
    }

    /// Return the union of all current shape bounds in Box2D's local `f32` frame.
    pub fn bounds(&self) -> crate::error::Result<Aabb> {
        check_world_available(self)?;
        world_bounds_impl(self.raw())
    }

    /// Return the largest capacities observed by this world so far.
    pub fn maximum_capacity(&self) -> crate::error::Result<WorldCapacity> {
        check_world_available(self)?;
        world_max_capacity_impl(self.raw())
    }

    /// Return the contact point recycling threshold in meters. Zero disables recycling.
    pub fn contact_recycle_distance(&self) -> crate::error::Result<f32> {
        check_world_available(self)?;
        world_contact_recycle_distance_impl(self.raw())
    }

    /// Set the contact point recycling threshold in meters. Zero disables recycling.
    pub fn set_contact_recycle_distance(&mut self, distance: f32) -> crate::error::Result<()> {
        crate::world::run_owner_call(self, |world| {
            world_set_contact_recycle_distance(world, distance)
        })
    }

    /// Change the worker count at a step boundary.
    ///
    /// Box2D rebuilds its per-worker simulation contexts synchronously before
    /// this call returns. The scheduler wiring and number of native scheduler
    /// threads remain the ones selected when the world was created. In
    /// particular, a world created with one worker keeps the serial scheduler
    /// when its logical worker count is later increased. The `&mut self`
    /// receiver prevents a concurrent owner call.
    ///
    pub fn set_worker_count(&mut self, count: WorkerCount) -> crate::error::Result<()> {
        check_world_available(self)?;
        unsafe { ffi::b2World_SetWorkerCount(self.raw(), count.as_i32()) };
        Ok(())
    }

    pub fn worker_count(&self) -> crate::error::Result<WorkerCount> {
        check_world_available(self)?;
        world_worker_count_impl(self.raw())
    }
}

#[cfg(test)]
mod tests {
    use crate::Error;

    #[test]
    fn maximum_linear_speed_rejects_values_whose_square_overflows() {
        let mut world = crate::Foundation::initialize_default()
            .unwrap()
            .create_world(
                crate::Foundation::get()
                    .expect("Foundation must be initialized before constructing a WorldDef")
                    .world_def(),
            )
            .unwrap();

        assert_eq!(
            world.set_maximum_linear_speed(f32::MAX),
            Err(Error::invalid_argument(
                "World::set_maximum_linear_speed",
                "value",
                "a positive finite value whose square is finite",
            ))
        );
        assert_eq!(world.maximum_linear_speed().unwrap(), 400.0);
    }

    #[test]
    fn invalid_step_arguments_do_not_advance_the_contact_epoch() {
        let mut world = crate::Foundation::initialize_default()
            .unwrap()
            .create_world(
                crate::Foundation::get()
                    .expect("Foundation must be initialized before constructing a WorldDef")
                    .world_def(),
            )
            .unwrap();
        let initial_epoch = world.core().contact_epoch();

        assert_eq!(
            world.step(f32::NAN, 4).unwrap_err(),
            Error::invalid_argument(
                "World::step",
                "time_step",
                "a finite value greater than or equal to zero",
            )
        );
        assert_eq!(world.core().contact_epoch(), initial_epoch);
        assert_eq!(
            world.step(1.0 / 60.0, 0).unwrap_err(),
            Error::invalid_argument("World::step", "sub_steps", "an integer greater than zero",)
        );
        assert_eq!(world.core().contact_epoch(), initial_epoch);

        world.step(-1.0 / 60.0, 4).unwrap_err();
        assert_eq!(world.core().contact_epoch(), initial_epoch);

        drop(world.step(1.0 / 60.0, 4).unwrap());
        assert_ne!(world.core().contact_epoch(), initial_epoch);
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn shape_snapshot_failure_does_not_begin_the_step_transaction() {
        let mut world = crate::Foundation::initialize_default()
            .unwrap()
            .create_world(
                crate::Foundation::get()
                    .expect("Foundation must be initialized before constructing a WorldDef")
                    .world_def(),
            )
            .unwrap();
        world.set_custom_filter(|_, _| true).unwrap();
        let completed = world.step(1.0 / 60.0, 4).unwrap();
        completed.body_events().unwrap();
        drop(completed);
        world
            .create_body(
                crate::Foundation::get()
                    .expect("Foundation must be initialized before constructing a BodyDef")
                    .body_def(),
            )
            .unwrap();
        let initial_epoch = world.core().contact_epoch();
        let initial_getter_calls = world.event_storage().getter_calls_for_test();
        world.core().fail_next_step_shape_snapshot_for_test();

        assert_eq!(
            world.step(1.0 / 60.0, 4).unwrap_err(),
            Error::IdentityTrackingAllocationFailed
        );
        assert_eq!(world.core().contact_epoch(), initial_epoch);
        assert_eq!(
            world.event_storage().getter_calls_for_test(),
            initial_getter_calls
        );

        drop(world.step(1.0 / 60.0, 4).unwrap());
        assert_ne!(world.core().contact_epoch(), initial_epoch);
    }

    #[test]
    #[cfg(not(target_arch = "wasm32"))]
    fn undrained_worker_error_does_not_begin_the_step_transaction() {
        let mut world = crate::Foundation::initialize_default()
            .unwrap()
            .create_world(
                crate::Foundation::get()
                    .expect("Foundation must be initialized before constructing a WorldDef")
                    .world_def(),
            )
            .unwrap();
        let initial_epoch = world.core().contact_epoch();
        let expected = Error::IdentityTrackingAllocationFailed;
        world.core().worker_callbacks.record_error(expected);

        assert_eq!(world.step(1.0 / 60.0, 4).unwrap_err(), expected);
        assert_eq!(world.core().contact_epoch(), initial_epoch);

        drop(world.step(1.0 / 60.0, 4).unwrap());
        assert_ne!(world.core().contact_epoch(), initial_epoch);
    }
}