boxddd 0.2.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
use super::*;

impl World {
    /// Steps the world or panics if Box3D rejects the step.
    pub fn step(&mut self, time_step: f32, sub_step_count: i32) {
        self.try_step(time_step, sub_step_count)
            .expect("Box3D failed to step world");
    }

    /// Tries to simulate one fixed time step.
    ///
    /// This runs collision detection, integration, and constraint solving. The
    /// `time_step` should normally be fixed, and `sub_step_count` controls solver
    /// accuracy.
    #[inline]
    pub fn try_step(&mut self, time_step: f32, sub_step_count: i32) -> Result<()> {
        callback_state::check_not_in_callback()?;
        if !time_step.is_finite() || time_step < 0.0 || sub_step_count < 0 {
            return Err(Error::InvalidArgument);
        }
        let _guard = box3d_lock::lock();
        self.callbacks.reset_panics();
        if let Some(task_system) = self.task_system.as_ref() {
            task_system.reset_panics();
        }
        unsafe { ffi::b3World_Step(self.raw, time_step, sub_step_count) };
        if self.callbacks.panicked()
            || self
                .task_system
                .as_ref()
                .is_some_and(|task_system| task_system.panicked())
        {
            return Err(Error::CallbackPanicked);
        }
        Ok(())
    }

    /// Returns the world gravity vector.
    #[inline]
    pub fn gravity(&self) -> Vec3 {
        self.try_gravity().expect("invalid Box3D world")
    }

    /// Tries to return the world gravity vector.
    #[inline]
    pub fn try_gravity(&self) -> Result<Vec3> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        if !unsafe { ffi::b3World_IsValid(self.raw) } {
            return Err(Error::InvalidWorldId);
        }
        Ok(Vec3::from_raw(unsafe { ffi::b3World_GetGravity(self.raw) }))
    }

    /// Sets the world gravity vector or panics if the world is invalid.
    #[inline]
    pub fn set_gravity(&mut self, gravity: impl Into<Vec3>) {
        self.try_set_gravity(gravity)
            .expect("invalid gravity or Box3D world");
    }

    /// Tries to set the world gravity vector.
    #[inline]
    pub fn try_set_gravity(&mut self, gravity: impl Into<Vec3>) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let gravity = gravity.into().validate()?;
        let _guard = box3d_lock::lock();
        if !unsafe { ffi::b3World_IsValid(self.raw) } {
            return Err(Error::InvalidWorldId);
        }
        unsafe { ffi::b3World_SetGravity(self.raw, gravity.into_raw()) };
        Ok(())
    }

    /// Applies a radial explosion or panics if Box3D rejects the definition.
    pub fn explode(&mut self, explosion: &ExplosionDef) {
        self.try_explode(explosion)
            .expect("invalid explosion or Box3D world");
    }

    /// Tries to apply a radial explosion.
    pub fn try_explode(&mut self, explosion: &ExplosionDef) -> Result<()> {
        callback_state::check_not_in_callback()?;
        explosion.validate()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_Explode(self.raw, explosion.raw()) };
        Ok(())
    }

    /// Returns the bounds covering the current simulation.
    pub fn bounds(&self) -> Aabb {
        self.try_bounds().expect("invalid Box3D world")
    }

    /// Tries to return the bounds covering the current simulation.
    #[inline]
    pub fn try_bounds(&self) -> Result<Aabb> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        if !unsafe { ffi::b3World_IsValid(self.raw) } {
            return Err(Error::InvalidWorldId);
        }
        Ok(Aabb::from_raw(unsafe { ffi::b3World_GetBounds(self.raw) }))
    }

    /// Returns the current world performance profile.
    #[inline]
    pub fn profile(&self) -> Profile {
        self.try_profile().expect("invalid Box3D world")
    }

    /// Tries to return the current world performance profile.
    #[inline]
    pub fn try_profile(&self) -> Result<Profile> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        if !unsafe { ffi::b3World_IsValid(self.raw) } {
            return Err(Error::InvalidWorldId);
        }
        Ok(Profile::from_raw(unsafe {
            ffi::b3World_GetProfile(self.raw)
        }))
    }

    /// Returns the current world counters and allocation sizes.
    #[inline]
    pub fn counters(&self) -> Counters {
        self.try_counters().expect("invalid Box3D world")
    }

    /// Tries to return the current world counters and allocation sizes.
    #[inline]
    pub fn try_counters(&self) -> Result<Counters> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        if !unsafe { ffi::b3World_IsValid(self.raw) } {
            return Err(Error::InvalidWorldId);
        }
        Ok(Counters::from_raw(unsafe {
            ffi::b3World_GetCounters(self.raw)
        }))
    }

    /// Tries to enable or disable sleeping for the entire world.
    pub fn try_enable_sleeping(&mut self, enabled: bool) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_EnableSleeping(self.raw, enabled) };
        Ok(())
    }

    /// Enables or disables sleeping for the entire world.
    pub fn enable_sleeping(&mut self, enabled: bool) {
        self.try_enable_sleeping(enabled)
            .expect("invalid Box3D world");
    }

    /// Tries to return whether sleeping is enabled.
    pub fn try_sleeping_enabled(&self) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_IsSleepingEnabled(self.raw) })
    }

    /// Returns whether sleeping is enabled.
    pub fn sleeping_enabled(&self) -> bool {
        self.try_sleeping_enabled().expect("invalid Box3D world")
    }

    /// Tries to enable or disable continuous collision.
    pub fn try_enable_continuous(&mut self, enabled: bool) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_EnableContinuous(self.raw, enabled) };
        Ok(())
    }

    /// Enables or disables continuous collision.
    pub fn enable_continuous(&mut self, enabled: bool) {
        self.try_enable_continuous(enabled)
            .expect("invalid Box3D world");
    }

    /// Tries to return whether continuous collision is enabled.
    pub fn try_continuous_enabled(&self) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_IsContinuousEnabled(self.raw) })
    }

    /// Returns whether continuous collision is enabled.
    pub fn continuous_enabled(&self) -> bool {
        self.try_continuous_enabled().expect("invalid Box3D world")
    }

    /// Tries to set the restitution speed threshold, usually in meters per second.
    pub fn try_set_restitution_threshold(&mut self, value: f32) -> Result<()> {
        validate_nonnegative_scalar(value)?;
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetRestitutionThreshold(self.raw, value) };
        Ok(())
    }

    /// Returns the restitution speed threshold, usually in meters per second.
    pub fn restitution_threshold(&self) -> f32 {
        self.try_restitution_threshold()
            .expect("invalid Box3D world")
    }

    /// Tries to return the restitution speed threshold.
    pub fn try_restitution_threshold(&self) -> Result<f32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetRestitutionThreshold(self.raw) })
    }

    /// Tries to set the collision speed threshold required to emit hit events.
    pub fn try_set_hit_event_threshold(&mut self, value: f32) -> Result<()> {
        validate_nonnegative_scalar(value)?;
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetHitEventThreshold(self.raw, value) };
        Ok(())
    }

    /// Returns the collision speed threshold required to emit hit events.
    pub fn hit_event_threshold(&self) -> f32 {
        self.try_hit_event_threshold().expect("invalid Box3D world")
    }

    /// Tries to return the hit-event speed threshold.
    pub fn try_hit_event_threshold(&self) -> Result<f32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetHitEventThreshold(self.raw) })
    }

    /// Tries to set advanced contact tuning parameters.
    pub fn try_set_contact_tuning(
        &mut self,
        hertz: f32,
        damping_ratio: f32,
        contact_speed: f32,
    ) -> Result<()> {
        validate_nonnegative_scalar(hertz)?;
        validate_nonnegative_scalar(damping_ratio)?;
        validate_nonnegative_scalar(contact_speed)?;
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetContactTuning(self.raw, hertz, damping_ratio, contact_speed) };
        Ok(())
    }

    /// Tries to set the contact point recycling distance.
    pub fn try_set_contact_recycle_distance(&mut self, distance: f32) -> Result<()> {
        validate_nonnegative_scalar(distance)?;
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetContactRecycleDistance(self.raw, distance) };
        Ok(())
    }

    /// Returns the contact point recycling distance.
    pub fn contact_recycle_distance(&self) -> f32 {
        self.try_contact_recycle_distance()
            .expect("invalid Box3D world")
    }

    /// Tries to return the contact point recycling distance.
    pub fn try_contact_recycle_distance(&self) -> Result<f32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetContactRecycleDistance(self.raw) })
    }

    /// Tries to set the maximum linear speed, usually in meters per second.
    pub fn try_set_maximum_linear_speed(&mut self, speed: f32) -> Result<()> {
        validate_nonnegative_scalar(speed)?;
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetMaximumLinearSpeed(self.raw, speed) };
        Ok(())
    }

    /// Returns the maximum linear speed, usually in meters per second.
    pub fn maximum_linear_speed(&self) -> f32 {
        self.try_maximum_linear_speed()
            .expect("invalid Box3D world")
    }

    /// Tries to return the maximum linear speed.
    pub fn try_maximum_linear_speed(&self) -> Result<f32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetMaximumLinearSpeed(self.raw) })
    }

    /// Tries to enable or disable constraint warm starting.
    pub fn try_enable_warm_starting(&mut self, enabled: bool) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_EnableWarmStarting(self.raw, enabled) };
        Ok(())
    }

    /// Returns whether constraint warm starting is enabled.
    pub fn warm_starting_enabled(&self) -> bool {
        self.try_warm_starting_enabled()
            .expect("invalid Box3D world")
    }

    /// Tries to return whether constraint warm starting is enabled.
    pub fn try_warm_starting_enabled(&self) -> Result<bool> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_IsWarmStartingEnabled(self.raw) })
    }

    /// Tries to enable or disable speculative contacts.
    pub fn try_enable_speculative(&mut self, enabled: bool) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_EnableSpeculative(self.raw, enabled) };
        Ok(())
    }

    /// Returns the number of awake bodies in the world.
    pub fn awake_body_count(&self) -> i32 {
        self.try_awake_body_count().expect("invalid Box3D world")
    }

    /// Tries to return the number of awake bodies in the world.
    pub fn try_awake_body_count(&self) -> Result<i32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetAwakeBodyCount(self.raw) })
    }

    /// Returns the maximum capacity reached by this world.
    pub fn max_capacity(&self) -> Capacity {
        self.try_max_capacity().expect("invalid Box3D world")
    }

    /// Tries to return the maximum capacity reached by this world.
    pub fn try_max_capacity(&self) -> Result<Capacity> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(Capacity::from_raw(unsafe {
            ffi::b3World_GetMaxCapacity(self.raw)
        }))
    }

    /// Tries to set the Box3D worker count for future simulation steps.
    pub fn try_set_worker_count(&mut self, count: i32) -> Result<()> {
        if count < 0 {
            return Err(Error::InvalidArgument);
        }
        #[cfg(target_arch = "wasm32")]
        if count > 1 {
            return Err(Error::UnsupportedOnWasm);
        }
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_SetWorkerCount(self.raw, count) };
        Ok(())
    }

    /// Returns the Box3D worker count used for simulation steps.
    pub fn worker_count(&self) -> i32 {
        self.try_worker_count().expect("invalid Box3D world")
    }

    /// Tries to return the Box3D worker count.
    pub fn try_worker_count(&self) -> Result<i32> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        Ok(unsafe { ffi::b3World_GetWorkerCount(self.raw) })
    }

    /// Tries to rebuild the broad-phase tree for static bodies.
    pub fn try_rebuild_static_tree(&mut self) -> Result<()> {
        callback_state::check_not_in_callback()?;
        let _guard = box3d_lock::lock();
        self.check_world_valid_locked()?;
        unsafe { ffi::b3World_RebuildStaticTree(self.raw) };
        Ok(())
    }
}