Skip to main content

box2d_rust/
constants.rs

1// Port of box2d-cpp-reference/include/box2d/constants.h
2//
3// The C header defines these as macros. Constants that do not depend on runtime
4// state are `pub const`. Those defined in terms of `b2GetLengthUnitsPerMeter()`
5// re-read that global on every use, so they are ported as functions to preserve
6// that behavior exactly.
7//
8// SPDX-FileCopyrightText: 2026 Erin Catto
9// SPDX-License-Identifier: MIT
10
11use crate::core::get_length_units_per_meter;
12use crate::math_functions::PI;
13
14/// Maximum parallel workers. Used for some fixed size arrays. (B2_MAX_WORKERS)
15pub const MAX_WORKERS: i32 = 32;
16
17/// Maximum number of tasks queued per world step. (B2_MAX_TASKS)
18pub const MAX_TASKS: i32 = 256;
19
20/// Maximum number of colors in the constraint graph. (B2_GRAPH_COLOR_COUNT)
21pub const GRAPH_COLOR_COUNT: i32 = 24;
22
23/// Maximum number of simultaneous worlds that can be allocated. (B2_MAX_WORLDS)
24pub const MAX_WORLDS: i32 = 128;
25
26/// Maximum length of the body name. (B2_NAME_LENGTH)
27pub const NAME_LENGTH: i32 = 10;
28
29/// The maximum rotation of a body per time step. Used to prevent numerical
30/// problems. (B2_MAX_ROTATION)
31///
32/// @warning increasing this to 0.5 * pi or greater will break continuous collision.
33pub const MAX_ROTATION: f32 = 0.25 * PI;
34
35/// The default contact recycling world angle threshold. 0.98 ~= 11.5 degrees.
36/// (B2_CONTACT_RECYCLE_COS_ANGLE)
37pub const CONTACT_RECYCLE_COS_ANGLE: f32 = 0.98;
38
39/// For small objects the margin is limited to this fraction times the maximum
40/// extent. (B2_AABB_MARGIN_FRACTION)
41pub const AABB_MARGIN_FRACTION: f32 = 0.125;
42
43/// The time that a body must be still before it will go to sleep, in seconds.
44/// (B2_TIME_TO_SLEEP)
45pub const TIME_TO_SLEEP: f32 = 0.5;
46
47/// Used to detect bad values. Positions greater than about 16km will have
48/// precision problems in single precision, so 100km as a limit should be fine.
49/// In large world mode the broad-phase starts to have excessive padding at
50/// 10,000km. (B2_HUGE)
51#[cfg(feature = "double-precision")]
52pub fn huge() -> f32 {
53    1.0e9 * get_length_units_per_meter()
54}
55
56/// See [`huge`].
57#[cfg(not(feature = "double-precision"))]
58pub fn huge() -> f32 {
59    1.0e5 * get_length_units_per_meter()
60}
61
62/// A small length used as a collision and constraint tolerance. Usually chosen
63/// to be numerically significant but visually insignificant, normally 0.5cm.
64/// (B2_LINEAR_SLOP)
65///
66/// @warning modifying this can have a significant impact on stability.
67pub fn linear_slop() -> f32 {
68    0.005 * get_length_units_per_meter()
69}
70
71/// Box2D uses limited speculative collision, normally 2cm. This reduces jitter.
72/// (B2_SPECULATIVE_DISTANCE)
73///
74/// @warning modifying this can have a significant impact on performance and stability.
75pub fn speculative_distance() -> f32 {
76    4.0 * linear_slop()
77}
78
79/// The default contact recycling distance. (B2_CONTACT_RECYCLE_DISTANCE)
80pub fn contact_recycle_distance() -> f32 {
81    10.0 * linear_slop()
82}
83
84/// Used to fatten AABBs in the dynamic tree so proxies can move a small amount
85/// without triggering a tree adjustment, normally 5cm. (B2_MAX_AABB_MARGIN)
86///
87/// @warning modifying this can have a significant impact on performance.
88pub fn max_aabb_margin() -> f32 {
89    0.05 * get_length_units_per_meter()
90}