boxddd 0.4.0

Safe, ergonomic Rust bindings for Box3D
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
use crate::TaskSystem;
use crate::body::{BodyDef, BodyType};
use crate::callbacks::WorldCallbacks;
use crate::core::foundation::{Foundation, OrdinaryLease};
use crate::core::{callback_state, debug_checks, ffi_vec, task_system, validation};
use crate::debug_draw::DebugShapeRegistry;
#[cfg(all(target_arch = "wasm32", boxddd_wasm_provider))]
use crate::debug_draw::take_provider_debug_error;
use crate::error::{Error, Result};
use crate::events::EventScratch;
use crate::recording::RecordingActivity;
use crate::shapes::{
    BoxHull, Capsule, Compound, HeightField, Hull, MeshData, PreparedShapeDef, ShapeDef,
    ShapeHeightField, ShapeHull, ShapeMaterialUsage, ShapeMesh, ShapeType, Sphere, SurfaceMaterial,
    validate_mesh_scale,
};
use crate::types::{
    Aabb, BodyId, Capacity, ContactData, ContactId, Counters, Filter, JointId, MassData, Matrix3,
    MotionLocks, Pos, Profile, Quat, ShapeId, Vec3, Version, WorldTransform,
};
use boxddd_sys::ffi;
use std::cell::Cell;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::rc::Rc;

/// Configuration used when creating a Box3D world.
#[derive(Clone, Debug)]
pub struct WorldDef {
    /// World gravity vector.
    pub gravity: Vec3,
    /// Minimum relative speed that enables restitution.
    pub restitution_threshold: f32,
    /// Minimum relative speed that produces hit events.
    pub hit_event_threshold: f32,
    /// Contact constraint frequency in hertz.
    pub contact_hertz: f32,
    /// Non-negative contact damping ratio.
    pub contact_damping_ratio: f32,
    /// Maximum contact overlap recovery speed.
    pub contact_speed: f32,
    /// Positive maximum body linear speed.
    pub maximum_linear_speed: f32,
    /// Whether bodies may sleep.
    pub enable_sleep: bool,
    /// Whether continuous collision detection is enabled.
    pub enable_continuous: bool,
    /// Number of worker slots; zero selects Box3D's default single worker.
    pub worker_count: u32,
    /// Optional initial native allocation capacities.
    pub capacity: Capacity,
    /// Optional Rust task-system adapter retained by the world.
    pub task_system: Option<TaskSystem>,
}

impl WorldDef {
    pub(crate) fn with_length_units_per_meter(length_units: f32) -> Self {
        Self {
            gravity: Vec3::new(0.0, -10.0, 0.0),
            restitution_threshold: length_units,
            hit_event_threshold: length_units,
            contact_hertz: 30.0,
            contact_damping_ratio: 10.0,
            contact_speed: 3.0 * length_units,
            maximum_linear_speed: 400.0 * length_units,
            enable_sleep: true,
            enable_continuous: true,
            worker_count: 0,
            capacity: Capacity::default(),
            task_system: None,
        }
    }

    /// Validates the value before it is passed to Box3D.
    pub fn validate(&self) -> Result<()> {
        validation::vec3("world.gravity", self.gravity)?;
        validation::nonnegative("world.restitution_threshold", self.restitution_threshold)?;
        validation::nonnegative("world.hit_event_threshold", self.hit_event_threshold)?;
        validation::nonnegative("world.contact_hertz", self.contact_hertz)?;
        validation::nonnegative("world.contact_damping_ratio", self.contact_damping_ratio)?;
        validation::nonnegative("world.contact_speed", self.contact_speed)?;
        validation::positive("world.maximum_linear_speed", self.maximum_linear_speed)?;
        if self.worker_count > ffi::B3_MAX_WORKERS {
            return Err(validation::invalid(
                "world.worker_count",
                crate::error::InvalidValueReason::OutOfRange,
            ));
        }
        if self.task_system.is_some() && self.worker_count == 0 {
            return Err(validation::invalid(
                "world.task_system",
                crate::error::InvalidValueReason::InvalidCombination,
            ));
        }
        for (context, value) in [
            (
                "world.capacity.static_shape_count",
                self.capacity.static_shape_count,
            ),
            (
                "world.capacity.dynamic_shape_count",
                self.capacity.dynamic_shape_count,
            ),
            (
                "world.capacity.static_body_count",
                self.capacity.static_body_count,
            ),
            (
                "world.capacity.dynamic_body_count",
                self.capacity.dynamic_body_count,
            ),
            ("world.capacity.contact_count", self.capacity.contact_count),
        ] {
            if value < 0 {
                return Err(validation::invalid(
                    context,
                    crate::error::InvalidValueReason::OutOfRange,
                ));
            }
        }
        self.capacity
            .static_body_count
            .checked_add(self.capacity.dynamic_body_count)
            .ok_or_else(|| {
                validation::invalid(
                    "world.capacity.body_count",
                    crate::error::InvalidValueReason::OutOfRange,
                )
            })?;
        self.capacity
            .static_shape_count
            .checked_add(self.capacity.dynamic_shape_count)
            .ok_or_else(|| {
                validation::invalid(
                    "world.capacity.shape_count",
                    crate::error::InvalidValueReason::OutOfRange,
                )
            })?;
        self.capacity.contact_count.checked_mul(2).ok_or_else(|| {
            validation::invalid(
                "world.capacity.contact_count",
                crate::error::InvalidValueReason::OutOfRange,
            )
        })?;
        Ok(())
    }

    fn validate_platform(&self) -> Result<()> {
        #[cfg(target_arch = "wasm32")]
        if self.worker_count > 1 || self.task_system.is_some() {
            return Err(Error::UnsupportedOnWasm);
        }
        Ok(())
    }
}

/// Builder for `WorldDef`.
#[derive(Clone, Debug)]
pub struct WorldDefBuilder {
    def: WorldDef,
}

impl WorldDefBuilder {
    pub(crate) fn from_def(def: WorldDef) -> Self {
        Self { def }
    }

    /// Sets the gravity vector used by the world, usually in meters per second squared.
    #[inline]
    pub fn gravity(mut self, gravity: impl Into<Vec3>) -> Self {
        self.def.gravity = gravity.into();
        self
    }

    /// Sets the number of worker slots Box3D may use while stepping the world.
    #[inline]
    pub fn worker_count(mut self, worker_count: u32) -> Self {
        self.def.worker_count = if self.def.task_system.is_some() {
            worker_count.max(1)
        } else {
            worker_count
        };
        self
    }

    /// Installs the task-system adapter used by Box3D during `World::step`.
    #[inline]
    pub fn task_system(mut self, task_system: TaskSystem) -> Self {
        if self.def.worker_count == 0 {
            self.def.worker_count = 1;
        }
        self.def.task_system = Some(task_system);
        self
    }

    /// Sets the restitution speed threshold.
    pub fn restitution_threshold(mut self, threshold: f32) -> Self {
        self.def.restitution_threshold = threshold;
        self
    }

    /// Sets the hit-event speed threshold.
    pub fn hit_event_threshold(mut self, threshold: f32) -> Self {
        self.def.hit_event_threshold = threshold;
        self
    }

    /// Sets contact solver tuning and maximum recovery speed.
    pub fn contact_tuning(mut self, hertz: f32, damping_ratio: f32, speed: f32) -> Self {
        self.def.contact_hertz = hertz;
        self.def.contact_damping_ratio = damping_ratio;
        self.def.contact_speed = speed;
        self
    }

    /// Sets the maximum linear speed for bodies.
    pub fn maximum_linear_speed(mut self, speed: f32) -> Self {
        self.def.maximum_linear_speed = speed;
        self
    }

    /// Enables or disables body sleeping.
    pub fn enable_sleep(mut self, enabled: bool) -> Self {
        self.def.enable_sleep = enabled;
        self
    }

    /// Enables or disables continuous collision detection.
    pub fn enable_continuous(mut self, enabled: bool) -> Self {
        self.def.enable_continuous = enabled;
        self
    }

    /// Sets optional initial allocation capacities.
    pub fn capacity(mut self, capacity: Capacity) -> Self {
        self.def.capacity = capacity;
        self
    }

    /// Validates and finishes the world definition.
    #[inline]
    pub fn build(self) -> Result<WorldDef> {
        self.def.validate()?;
        Ok(self.def)
    }
}

/// Configuration for applying an explosion impulse in a world.
#[derive(Clone, Debug)]
pub struct ExplosionDef {
    /// Collision mask selecting affected shapes.
    pub mask_bits: u64,
    /// Explosion center in world space.
    pub position: Pos,
    /// Non-negative inner radius receiving full impulse.
    pub radius: f32,
    /// Non-negative falloff distance beyond the radius.
    pub falloff: f32,
    /// Impulse per unit facing area; negative values produce implosions.
    pub impulse_per_area: f32,
}

impl Default for ExplosionDef {
    fn default() -> Self {
        Self {
            mask_bits: u64::MAX,
            position: Pos::ZERO,
            radius: 0.0,
            falloff: 0.0,
            impulse_per_area: 0.0,
        }
    }
}

impl ExplosionDef {
    /// Starts a builder with Box3D defaults.
    #[inline]
    pub fn builder() -> ExplosionDefBuilder {
        ExplosionDefBuilder::new()
    }

    /// Validates the value before it is passed to Box3D.
    pub fn validate(&self) -> Result<()> {
        validation::position("explosion.position", self.position)?;
        validation::nonnegative("explosion.radius", self.radius)?;
        validation::nonnegative("explosion.falloff", self.falloff)?;
        validation::finite("explosion.extent", self.radius + self.falloff)?;
        validation::finite("explosion.impulse_per_area", self.impulse_per_area)
    }

    pub(crate) fn lower(&self) -> Result<ffi::b3ExplosionDef> {
        self.validate()?;
        Ok(ffi::b3ExplosionDef {
            maskBits: self.mask_bits,
            position: self.position.into_raw(),
            radius: self.radius,
            falloff: self.falloff,
            impulsePerArea: self.impulse_per_area,
        })
    }
}

/// Builder for `ExplosionDef`.
#[derive(Clone, Debug)]
pub struct ExplosionDefBuilder {
    def: ExplosionDef,
}

impl ExplosionDefBuilder {
    /// Creates a new value with default settings.
    #[inline]
    pub fn new() -> Self {
        Self {
            def: ExplosionDef::default(),
        }
    }

    /// Sets the collision mask bits matched by the query.
    #[inline]
    pub fn mask_bits(mut self, mask_bits: u64) -> Self {
        self.def.mask_bits = mask_bits;
        self
    }

    /// Sets the world-space center of the explosion.
    #[inline]
    pub fn position(mut self, position: impl Into<Pos>) -> Self {
        self.def.position = position.into();
        self
    }

    /// Sets the explosion radius.
    #[inline]
    pub fn radius(mut self, radius: f32) -> Self {
        self.def.radius = radius;
        self
    }

    /// Sets how quickly the explosion impulse decays over distance.
    #[inline]
    pub fn falloff(mut self, falloff: f32) -> Self {
        self.def.falloff = falloff;
        self
    }

    /// Sets the impulse applied per affected surface area.
    #[inline]
    pub fn impulse_per_area(mut self, impulse_per_area: f32) -> Self {
        self.def.impulse_per_area = impulse_per_area;
        self
    }

    /// Validates and finishes the explosion definition.
    #[inline]
    pub fn build(self) -> Result<ExplosionDef> {
        self.def.validate()?;
        Ok(self.def)
    }
}

impl Default for ExplosionDefBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of a World step that reached and advanced the native simulation.
///
/// A value of this type proves that Box3D was called and Rust-side post-step
/// finalizers committed. Callback or task failures discovered after native
/// advancement are retained separately and can be inspected or consumed.
#[must_use]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StepOutcome {
    post_step_error: Option<Error>,
}

impl StepOutcome {
    pub(crate) fn from_post_step_result(result: Result<()>) -> Self {
        Self {
            post_step_error: result.err(),
        }
    }

    /// Returns the callback or task failure reported after native advancement.
    #[inline]
    pub const fn post_step_error(&self) -> Option<&Error> {
        self.post_step_error.as_ref()
    }

    /// Projects the advanced outcome to the traditional result-first contract.
    #[inline]
    pub fn into_result(self) -> Result<()> {
        match self.post_step_error {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }
}

/// Owning handle for a Box3D world.
#[derive(Debug)]
pub struct World {
    owner: Option<Box<WorldOwner>>,
}

#[derive(Debug)]
struct WorldOwner {
    raw: ffi::b3WorldId,
    pub(crate) state: WorldState,
    _foundation_lease: OrdinaryLease,
    _not_send_sync: PhantomData<Rc<()>>,
}

#[derive(Debug)]
pub(crate) struct WorldState {
    phase: WorldPhase,
    pub(crate) poisoned: Cell<bool>,
    pub(crate) ledger: WorldLedger,
    pub(crate) backing_quarantine: Vec<ShapeResource>,
    pub(crate) callbacks: WorldCallbacks,
    pub(crate) event_scratch: EventScratch,
    _task_context: Option<Box<task_system::InstalledTaskContext>>,
    pub(crate) active_recording: Option<Rc<RecordingActivity>>,
    pub(crate) debug_shapes: Box<DebugShapeRegistry>,
    #[cfg(all(target_arch = "wasm32", boxddd_wasm_provider))]
    pub(crate) provider_debug_shapes_token: u32,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum WorldPhase {
    Live,
    Dropping,
    Destroyed,
}

#[derive(Debug)]
pub(crate) enum ShapeResource {
    Mesh { _data: MeshData },
    HeightField { _data: HeightField },
    Compound { _data: Compound },
}

mod body_api;
mod creation;
pub(crate) mod creation_transaction;
pub(crate) mod ledger;
mod lifecycle;
mod runtime;
mod shape_api;

use ledger::WorldLedger;

impl World {
    #[inline]
    fn owner(&self) -> &WorldOwner {
        self.owner
            .as_deref()
            .expect("World owner is present outside destruction")
    }

    #[inline]
    fn owner_mut(&mut self) -> &mut WorldOwner {
        self.owner
            .as_deref_mut()
            .expect("World owner is present outside destruction")
    }

    #[inline]
    pub(crate) fn state(&self) -> &WorldState {
        &self.owner().state
    }

    #[inline]
    pub(crate) fn state_mut(&mut self) -> &mut WorldState {
        &mut self.owner_mut().state
    }

    #[inline]
    pub(crate) fn raw(&self) -> ffi::b3WorldId {
        self.owner().raw
    }

    /// Returns whether `body_id` currently belongs to this world.
    ///
    /// Stale and foreign IDs return `Ok(false)`. Callback reentry and terminal
    /// owner poison remain visible as errors.
    pub fn contains_body(&self, body_id: BodyId) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        self.check_owner_healthy()?;
        match self.state().ledger.authorize_body(body_id) {
            Ok(_) => Ok(true),
            Err(Error::ForeignHandle { .. } | Error::StaleHandle { .. }) => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Returns whether `shape_id` currently belongs to this world.
    ///
    /// Stale and foreign IDs return `Ok(false)`. Callback reentry and terminal
    /// owner poison remain visible as errors.
    pub fn contains_shape(&self, shape_id: ShapeId) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        self.check_owner_healthy()?;
        match self.state().ledger.authorize_shape(shape_id) {
            Ok(_) => Ok(true),
            Err(Error::ForeignHandle { .. } | Error::StaleHandle { .. }) => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Returns whether `joint_id` currently belongs to this world.
    ///
    /// Stale and foreign IDs return `Ok(false)`. Callback reentry and terminal
    /// owner poison remain visible as errors.
    pub fn contains_joint(&self, joint_id: JointId) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        self.check_owner_healthy()?;
        match self.state().ledger.authorize_joint(joint_id) {
            Ok(_) => Ok(true),
            Err(Error::ForeignHandle { .. } | Error::StaleHandle { .. }) => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Returns whether `contact_id` is live in the current contact epoch.
    pub fn contains_contact(&self, contact_id: ContactId) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        self.check_owner_healthy()?;
        let Ok(raw) = self.state().ledger.authorize_contact(contact_id) else {
            return Ok(false);
        };
        let _call = self.enter_call()?;
        self.check_world_valid()?;
        Ok(unsafe { ffi::b3Contact_IsValid(raw) })
    }

    pub(crate) fn enter_call(&self) -> Result<callback_state::OwnerCallFrame> {
        self.owner()._foundation_lease.enter_call()
    }

    pub(crate) fn check_world_valid(&self) -> Result<()> {
        self.check_owner_healthy()?;
        #[cfg(all(target_arch = "wasm32", boxddd_wasm_provider))]
        if let Some(error) = take_provider_debug_error(self.state().provider_debug_shapes_token) {
            self.state().poisoned.set(true);
            return Err(error);
        }
        if self.state().phase == WorldPhase::Live && unsafe { ffi::b3World_IsValid(self.raw()) } {
            Ok(())
        } else {
            Err(Error::NativeFailure)
        }
    }

    pub(crate) fn enter_world_call(&self) -> Result<callback_state::OwnerCallFrame> {
        let call = self.enter_call()?;
        self.check_world_valid()?;
        Ok(call)
    }

    pub(crate) fn check_owner_healthy(&self) -> Result<()> {
        self.owner()
            ._foundation_lease
            .foundation()
            .ensure_healthy()?;
        if self.state().poisoned.get() || self.state().debug_shapes.is_poisoned() {
            Err(Error::OwnerPoisoned)
        } else {
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn enter_body_call(
        &self,
        body_id: BodyId,
    ) -> Result<callback_state::OwnerCallFrame> {
        callback_state::check_not_in_callback()?;
        let raw = self.state().ledger.authorize_body(body_id)?;
        let call = self.enter_call()?;
        self.check_world_valid()?;
        debug_checks::check_body_valid_raw(raw)?;
        Ok(call)
    }

    #[inline]
    pub(crate) fn enter_shape_call(
        &self,
        shape_id: ShapeId,
    ) -> Result<callback_state::OwnerCallFrame> {
        callback_state::check_not_in_callback()?;
        let raw = self.state().ledger.authorize_shape(shape_id)?;
        let call = self.enter_call()?;
        self.check_world_valid()?;
        debug_checks::check_shape_valid_raw(raw)?;
        Ok(call)
    }

    #[inline]
    pub(crate) fn enter_joint_call(
        &self,
        joint_id: JointId,
    ) -> Result<callback_state::OwnerCallFrame> {
        callback_state::check_not_in_callback()?;
        let raw = self.state().ledger.authorize_joint(joint_id)?;
        let call = self.enter_call()?;
        self.check_world_valid()?;
        debug_checks::check_joint_valid_raw(raw)?;
        Ok(call)
    }
}

/// Returns the Box3D runtime version.
#[inline]
pub fn version() -> Result<Version> {
    let _call = Foundation::enter_transient_call()?;
    Ok(Version::from_raw(unsafe { ffi::b3GetVersion() }))
}

/// Returns the number of bytes currently allocated by Box3D.
#[inline]
pub fn allocated_byte_count() -> Result<i32> {
    let _call = Foundation::enter_transient_call()?;
    Ok(unsafe { ffi::b3GetByteCount() })
}

/// Returns whether the linked Box3D library uses double precision.
#[inline]
pub const fn is_double_precision() -> bool {
    cfg!(feature = "double-precision")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DistanceJointDef;
    use crate::shapes::{ShapeDropEvent, begin_shape_drop_trace, take_shape_drop_trace};
    use crate::world::creation_transaction::{
        BodyNative, CreationStage, NativeClaim, UnclaimedNative, force_creation_failure,
        force_next_compensation_mismatch, force_next_joint_endpoints, force_next_shape_parent,
    };
    use crate::world::ledger::IdentityClassification;

    fn foundation() -> &'static Foundation {
        Foundation::initialize_default().unwrap()
    }

    fn static_body(world: &mut World) -> BodyId {
        world
            .create_body(
                foundation()
                    .body_def_builder()
                    .body_type(BodyType::Static)
                    .build()
                    .unwrap(),
            )
            .unwrap()
    }

    fn dynamic_body(world: &mut World, x: f32) -> BodyId {
        world
            .create_body(
                foundation()
                    .body_def_builder()
                    .body_type(BodyType::Dynamic)
                    .position([x, 0.0, 0.0])
                    .build()
                    .unwrap(),
            )
            .unwrap()
    }

    #[derive(Clone, Copy)]
    enum BackingFamily {
        Mesh,
        HeightField,
        Compound,
    }

    impl BackingFamily {
        fn drop_event(self) -> ShapeDropEvent {
            match self {
                Self::Mesh => ShapeDropEvent::MeshBacking,
                Self::HeightField => ShapeDropEvent::HeightFieldBacking,
                Self::Compound => ShapeDropEvent::CompoundBacking,
            }
        }
    }

    fn create_backed_shape(
        world: &mut World,
        body: BodyId,
        family: BackingFamily,
    ) -> Result<ShapeId> {
        match family {
            BackingFamily::Mesh => world.create_mesh_shape(
                body,
                &foundation().shape_def(),
                MeshData::box_mesh(Vec3::ZERO, [1.0, 1.0, 1.0], true)?,
                [1.0, 1.0, 1.0],
            ),
            BackingFamily::HeightField => world.create_height_field_shape(
                body,
                &foundation().shape_def(),
                HeightField::grid(2, 2, [1.0, 1.0, 1.0], false)?,
            ),
            BackingFamily::Compound => world.create_compound_shape(
                body,
                &foundation().shape_def(),
                Compound::single_sphere(Sphere::new(Vec3::ZERO, 0.25), SurfaceMaterial::default())?,
            ),
        }
    }

    fn native_counters(world: &World) -> Counters {
        let _call = world.enter_call().unwrap();
        Counters::from_raw(unsafe { ffi::b3World_GetCounters(world.raw()) })
    }

    #[test]
    fn shape_resources_are_removed_on_shape_destroy_and_mesh_replace() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let body = static_body(&mut world);
        let shape = world
            .create_mesh_shape(
                body,
                &foundation().shape_def(),
                MeshData::box_mesh(Vec3::ZERO, [1.0, 1.0, 1.0], true).unwrap(),
                [1.0, 1.0, 1.0],
            )
            .unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 1);

        world
            .set_shape_mesh(
                shape,
                MeshData::box_mesh(Vec3::ZERO, [0.5, 0.5, 0.5], true).unwrap(),
                [1.0, 1.0, 1.0],
            )
            .unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 1);

        world
            .set_shape_sphere(shape, &Sphere::new(Vec3::ZERO, 0.25))
            .unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 0);

        world.destroy_shape(shape, true).unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 0);
    }

    #[test]
    fn resource_backed_shapes_keep_body_static() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let body = static_body(&mut world);
        world
            .create_height_field_shape(
                body,
                &foundation().shape_def(),
                HeightField::grid(2, 2, [1.0, 1.0, 1.0], false).unwrap(),
            )
            .unwrap();

        assert_eq!(
            world.set_body_type(body, BodyType::Dynamic).unwrap_err(),
            Error::InvalidValue {
                context: "body.type",
                reason: crate::error::InvalidValueReason::InvalidCombination,
            }
        );
        assert_eq!(world.body_type(body).unwrap(), BodyType::Static);

        world
            .create_sphere_shape(
                body,
                &foundation().shape_def(),
                &Sphere::new(Vec3::new(2.0, 0.0, 0.0), 0.25),
            )
            .unwrap();
    }

    #[test]
    fn shape_resources_are_removed_on_body_destroy() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let body = static_body(&mut world);
        world
            .create_mesh_shape(
                body,
                &foundation().shape_def(),
                MeshData::box_mesh(Vec3::ZERO, [1.0, 1.0, 1.0], true).unwrap(),
                [1.0, 1.0, 1.0],
            )
            .unwrap();
        world
            .create_height_field_shape(
                body,
                &foundation().shape_def(),
                HeightField::grid(2, 2, [1.0, 1.0, 1.0], false).unwrap(),
            )
            .unwrap();
        world
            .create_compound_shape(
                body,
                &foundation().shape_def(),
                Compound::single_sphere(Sphere::new(Vec3::ZERO, 0.25), SurfaceMaterial::default())
                    .unwrap(),
            )
            .unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 3);

        world.destroy_body(body).unwrap();
        assert_eq!(world.state().ledger.shape_resource_count(), 0);
    }

    #[test]
    fn creation_transaction_body_and_shape_compensate_every_fallible_stage() {
        let stages = [
            CreationStage::AfterClaim,
            CreationStage::AfterBind,
            CreationStage::AfterPostflight,
            CreationStage::BeforePublish,
        ];

        for stage in stages {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let before = world.counters().unwrap();
            force_creation_failure(stage);
            assert_eq!(
                world.create_body(foundation().body_def()).unwrap_err(),
                Error::NativeFailure
            );
            assert_eq!(world.counters().unwrap().body_count, before.body_count);
            assert!(world.create_body(foundation().body_def()).is_ok());
        }

        for family in [
            BackingFamily::Mesh,
            BackingFamily::HeightField,
            BackingFamily::Compound,
        ] {
            for stage in stages {
                let mut world = foundation().create_world(foundation().world_def()).unwrap();
                let body = static_body(&mut world);
                let before = world.counters().unwrap();
                begin_shape_drop_trace();
                force_creation_failure(stage);
                assert_eq!(
                    create_backed_shape(&mut world, body, family).unwrap_err(),
                    Error::NativeFailure
                );
                assert_eq!(world.counters().unwrap().shape_count, before.shape_count);
                assert_eq!(world.state().ledger.shape_resource_count(), 0);
                assert!(world.state().backing_quarantine.is_empty());
                assert_eq!(
                    take_shape_drop_trace(),
                    vec![ShapeDropEvent::NativeShape, family.drop_event(),]
                );
                assert!(create_backed_shape(&mut world, body, family).is_ok());
                assert_eq!(world.state().ledger.shape_resource_count(), 1);
            }
        }
    }

    #[test]
    fn creation_transaction_joint_rollback_poisons_owner() {
        for stage in [
            CreationStage::AfterClaim,
            CreationStage::AfterBind,
            CreationStage::AfterPostflight,
            CreationStage::BeforePublish,
        ] {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let body_a = dynamic_body(&mut world, -1.0);
            let body_b = dynamic_body(&mut world, 1.0);
            let shape = world
                .create_sphere_shape(
                    body_a,
                    &foundation().shape_def(),
                    &Sphere::new(Vec3::ZERO, 0.5),
                )
                .unwrap();
            let retained_joint = world
                .create_distance_joint(DistanceJointDef::new(body_a, body_b).length(2.0))
                .unwrap();
            let before = native_counters(&world);

            force_creation_failure(stage);
            assert_eq!(
                world
                    .create_distance_joint(DistanceJointDef::new(body_a, body_b).length(3.0))
                    .unwrap_err(),
                Error::NativeFailure
            );
            assert_eq!(native_counters(&world).joint_count, before.joint_count);
            assert_eq!(world.body_type(body_a), Err(Error::OwnerPoisoned));
            assert_eq!(world.contains_body(body_a), Err(Error::OwnerPoisoned));
            assert_eq!(world.contains_shape(shape), Err(Error::OwnerPoisoned));
            assert_eq!(
                world.contains_joint(retained_joint),
                Err(Error::OwnerPoisoned)
            );
        }
    }

    #[test]
    fn creation_transaction_commit_disarm_fault_poisons_published_owner() {
        {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let before = native_counters(&world);
            force_creation_failure(CreationStage::BeforeCommitDisarm);
            assert_eq!(
                world.create_body(foundation().body_def()).unwrap_err(),
                Error::NativeFailure
            );
            assert!(world.state().poisoned.get());
            assert_eq!(native_counters(&world).body_count, before.body_count);
            assert_eq!(
                world.create_body(foundation().body_def()).unwrap_err(),
                Error::OwnerPoisoned
            );
        }

        {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let body = static_body(&mut world);
            let before = native_counters(&world);
            begin_shape_drop_trace();
            force_creation_failure(CreationStage::BeforeCommitDisarm);
            assert_eq!(
                create_backed_shape(&mut world, body, BackingFamily::Mesh).unwrap_err(),
                Error::NativeFailure
            );
            assert!(world.state().poisoned.get());
            assert_eq!(native_counters(&world).shape_count, before.shape_count);
            assert_eq!(world.state().ledger.shape_resource_count(), 1);
            assert!(world.state().backing_quarantine.is_empty());
            assert_eq!(world.contains_body(body), Err(Error::OwnerPoisoned));
        }
        assert_eq!(
            take_shape_drop_trace(),
            vec![ShapeDropEvent::NativeShape, ShapeDropEvent::MeshBacking,]
        );

        {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let body_a = dynamic_body(&mut world, -1.0);
            let body_b = dynamic_body(&mut world, 1.0);
            let before = native_counters(&world);
            force_creation_failure(CreationStage::BeforeCommitDisarm);
            assert_eq!(
                world
                    .create_distance_joint(DistanceJointDef::new(body_a, body_b).length(2.0))
                    .unwrap_err(),
                Error::NativeFailure
            );
            assert!(world.state().poisoned.get());
            assert_eq!(native_counters(&world).joint_count, before.joint_count);
            assert_eq!(world.contains_body(body_a), Err(Error::OwnerPoisoned));
        }
    }

    #[test]
    fn creation_transaction_wrong_shape_relation_compensates_before_backing_drop() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let body_a = static_body(&mut world);
        let body_b = static_body(&mut world);
        let before = world.counters().unwrap();
        begin_shape_drop_trace();
        force_next_shape_parent(body_b.into_raw());
        assert_eq!(
            create_backed_shape(&mut world, body_a, BackingFamily::Mesh).unwrap_err(),
            Error::NativeFailure
        );
        assert_eq!(world.counters().unwrap().shape_count, before.shape_count);
        assert_eq!(world.state().ledger.shape_resource_count(), 0);
        assert!(world.state().backing_quarantine.is_empty());
        assert_eq!(
            take_shape_drop_trace(),
            vec![ShapeDropEvent::NativeShape, ShapeDropEvent::MeshBacking,]
        );
        assert!(create_backed_shape(&mut world, body_a, BackingFamily::Mesh).is_ok());
    }

    #[test]
    fn creation_transaction_wrong_joint_relation_poisons_owner() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let body_c = dynamic_body(&mut world, -1.0);
        let body_d = dynamic_body(&mut world, 1.0);
        let before = native_counters(&world);
        force_next_joint_endpoints(body_d.into_raw(), body_c.into_raw());
        assert_eq!(
            world
                .create_distance_joint(DistanceJointDef::new(body_c, body_d).length(2.0))
                .unwrap_err(),
            Error::NativeFailure
        );
        assert!(world.state().poisoned.get());
        assert_eq!(native_counters(&world).joint_count, before.joint_count);
        assert_eq!(world.body_joints(body_c), Err(Error::OwnerPoisoned));
        assert_eq!(world.body_joints(body_d), Err(Error::OwnerPoisoned));
    }

    #[test]
    fn creation_transaction_rejects_empty_invalid_active_and_foreign_outputs() {
        let mut world = foundation().create_world(foundation().world_def()).unwrap();
        let raw_world = world.raw();
        let empty = ffi::b3BodyId {
            index1: 0,
            world0: raw_world.index1 - 1,
            generation: 0,
        };
        let empty_identity = world.state().ledger.classify_body(empty);
        assert!(matches!(
            UnclaimedNative::<BodyNative>::new(empty, (), raw_world, &world.state().poisoned)
                .claim(empty_identity),
            Err(Error::ObjectIdentityExhausted)
        ));
        assert!(!world.state().poisoned.get());

        let invalid = ffi::b3BodyId {
            index1: i32::MAX,
            world0: raw_world.index1 - 1,
            generation: u16::MAX,
        };
        let invalid_identity = world.state().ledger.classify_body(invalid);
        let _call = world.enter_call().unwrap();
        assert!(matches!(
            UnclaimedNative::<BodyNative>::new(invalid, (), raw_world, &world.state().poisoned)
                .claim(invalid_identity),
            Err(Error::ObjectIdentityExhausted)
        ));
        drop(_call);
        assert!(!world.state().poisoned.get());

        let invalid_wrong_world = ffi::b3BodyId {
            index1: i32::MAX,
            world0: u16::MAX,
            generation: u16::MAX,
        };
        let invalid_identity = world.state().ledger.classify_body(invalid_wrong_world);
        let _call = world.enter_call().unwrap();
        assert!(matches!(
            UnclaimedNative::<BodyNative>::new(
                invalid_wrong_world,
                (),
                raw_world,
                &world.state().poisoned,
            )
            .claim(invalid_identity),
            Err(Error::ObjectIdentityExhausted)
        ));
        drop(_call);
        assert!(!world.state().poisoned.get());

        let body = world.create_body(foundation().body_def()).unwrap();
        let raw_body = body.into_raw();
        let active_identity = world.state().ledger.classify_body(raw_body);
        let _call = world.enter_call().unwrap();
        assert!(matches!(
            UnclaimedNative::<BodyNative>::new(raw_body, (), raw_world, &world.state().poisoned)
                .claim(active_identity),
            Err(Error::OwnerPoisoned)
        ));
        assert!(unsafe { ffi::b3Body_IsValid(raw_body) });
        drop(_call);
        drop(world);

        let mut source = foundation().create_world(foundation().world_def()).unwrap();
        let raw_body = source
            .create_body(foundation().body_def())
            .unwrap()
            .into_raw();
        let target = foundation().create_world(foundation().world_def()).unwrap();
        let identity = target.state().ledger.classify_body(raw_body);
        let _call = target.enter_call().unwrap();
        assert!(matches!(
            UnclaimedNative::<BodyNative>::new(
                raw_body,
                (),
                target.raw(),
                &target.state().poisoned,
            )
            .claim(identity),
            Err(Error::OwnerPoisoned)
        ));
        assert!(unsafe { ffi::b3Body_IsValid(raw_body) });
    }

    #[test]
    fn creation_transaction_compensates_retired_candidate_and_poisons_on_mismatch() {
        fn check_retired_collision(rotate_to_visible: bool) {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let target_world = world.raw();
            let pending = world.state_mut().ledger.reserve_body().unwrap();
            let _call = world.enter_call().unwrap();
            let raw = foundation()
                .body_def()
                .prepare()
                .unwrap()
                .create(target_world);
            let WorldState {
                poisoned, ledger, ..
            } = world.state_mut();
            let IdentityClassification::Available(available) = ledger.classify_body(raw) else {
                panic!("fresh test body identity is available")
            };
            let NativeClaim {
                candidate,
                available: claimed,
            } = UnclaimedNative::<BodyNative>::new(raw, (), target_world, poisoned)
                .claim(IdentityClassification::Available(available))
                .unwrap();
            let bound = ledger.bind_body(raw, claimed, pending).unwrap();
            let retired = candidate
                .bind()
                .commit(|_| ledger.publish_body(bound))
                .unwrap();
            let cascade = ledger.prepare_body_cascade(retired).unwrap();
            ledger.finish_body_cascade(retired, cascade);
            if rotate_to_visible {
                let next = ledger.prepare_contact_turnover().unwrap();
                ledger.finish_step(next);
            }
            let retired_identity = ledger.classify_body(raw);
            assert!(matches!(
                &retired_identity,
                IdentityClassification::ObservableRetired
            ));
            assert_eq!(ledger.resolve_observed_body(raw).unwrap(), retired);
            assert!(matches!(
                UnclaimedNative::<BodyNative>::new(raw, (), target_world, poisoned)
                    .claim(retired_identity),
                Err(Error::ObjectIdentityExhausted)
            ));
            assert!(!unsafe { ffi::b3Body_IsValid(raw) });
            assert_eq!(ledger.resolve_observed_body(raw).unwrap(), retired);

            let next = ledger.prepare_contact_turnover().unwrap();
            ledger.finish_step(next);
            if !rotate_to_visible {
                assert_eq!(ledger.resolve_observed_body(raw).unwrap(), retired);
                let next = ledger.prepare_contact_turnover().unwrap();
                ledger.finish_step(next);
            }
            assert!(matches!(
                ledger.resolve_observed_body(raw),
                Err(Error::StaleHandle {
                    kind: crate::error::HandleKind::Body
                })
            ));
            drop(_call);
            assert!(!world.state().poisoned.get());
        }

        check_retired_collision(false);
        check_retired_collision(true);

        let mut world = foundation().create_world(foundation().world_def()).unwrap();

        force_creation_failure(CreationStage::AfterClaim);
        force_next_compensation_mismatch();
        assert_eq!(
            world.create_body(foundation().body_def()).unwrap_err(),
            Error::NativeFailure
        );
        assert!(world.state().poisoned.get());
        assert_eq!(
            world.create_body(foundation().body_def()).unwrap_err(),
            Error::OwnerPoisoned
        );
        drop(world);
    }

    #[test]
    fn creation_transaction_compensation_mismatch_poisons_shape_and_joint_owners() {
        begin_shape_drop_trace();
        {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let body = static_body(&mut world);
            let before = native_counters(&world);
            force_creation_failure(CreationStage::AfterClaim);
            force_next_compensation_mismatch();

            assert_eq!(
                create_backed_shape(&mut world, body, BackingFamily::Mesh).unwrap_err(),
                Error::NativeFailure
            );
            assert!(world.state().poisoned.get());
            assert_eq!(native_counters(&world).shape_count, before.shape_count);
            assert_eq!(world.state().ledger.shape_resource_count(), 0);
            assert_eq!(world.state().backing_quarantine.len(), 1);
            assert_eq!(world.contains_body(body), Err(Error::OwnerPoisoned));
            assert_eq!(
                world.create_body(foundation().body_def()).unwrap_err(),
                Error::OwnerPoisoned
            );
        }
        assert_eq!(
            take_shape_drop_trace(),
            vec![ShapeDropEvent::NativeShape, ShapeDropEvent::MeshBacking,]
        );

        {
            let mut world = foundation().create_world(foundation().world_def()).unwrap();
            let body_a = dynamic_body(&mut world, -1.0);
            let body_b = dynamic_body(&mut world, 1.0);
            let before = native_counters(&world);
            force_creation_failure(CreationStage::AfterClaim);
            force_next_compensation_mismatch();

            assert_eq!(
                world
                    .create_distance_joint(DistanceJointDef::new(body_a, body_b).length(2.0))
                    .unwrap_err(),
                Error::NativeFailure
            );
            assert!(world.state().poisoned.get());
            assert_eq!(native_counters(&world).joint_count, before.joint_count);
            assert_eq!(
                world.create_body(foundation().body_def()).unwrap_err(),
                Error::OwnerPoisoned
            );
        }
    }
}