Skip to main content

box3d_rust/types/
body.rs

1// Body creation types and defaults from types.h / types.c.
2// SPDX-FileCopyrightText: 2025 Erin Catto
3// SPDX-License-Identifier: MIT
4
5use crate::core::{get_length_units_per_meter, SECRET_COOKIE};
6use crate::math_functions::{Pos, Quat, Vec3, POS_ZERO, QUAT_IDENTITY, VEC3_ZERO};
7
8/// The body simulation type. Each body is one of these three types.
9/// (b3BodyType)
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11#[repr(i32)]
12pub enum BodyType {
13    /// zero mass, zero velocity, may be manually moved
14    #[default]
15    Static = 0,
16    /// zero mass, velocity set by user, moved by solver
17    Kinematic = 1,
18    /// positive mass, velocity determined by forces, moved by solver
19    Dynamic = 2,
20}
21
22/// Number of body types. (b3_bodyTypeCount)
23pub const BODY_TYPE_COUNT: usize = 3;
24
25/// Motion locks to restrict the body movement. (b3MotionLocks)
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
27pub struct MotionLocks {
28    /// Prevent translation along the x-axis
29    pub linear_x: bool,
30    /// Prevent translation along the y-axis
31    pub linear_y: bool,
32    /// Prevent translation along the z-axis
33    pub linear_z: bool,
34    /// Prevent rotation around the x-axis
35    pub angular_x: bool,
36    /// Prevent rotation around the y-axis
37    pub angular_y: bool,
38    /// Prevent rotation around the z-axis
39    pub angular_z: bool,
40}
41
42/// A body definition holds all the data needed to construct a rigid body.
43/// Must be initialized using [`default_body_def`]. (b3BodyDef)
44#[derive(Debug, Clone, PartialEq)]
45pub struct BodyDef {
46    /// The body type: static, kinematic, or dynamic.
47    pub type_: BodyType,
48    /// The initial world position of the body.
49    pub position: Pos,
50    /// The initial world rotation of the body.
51    pub rotation: Quat,
52    /// The initial linear velocity of the body's origin, usually m/s.
53    pub linear_velocity: Vec3,
54    /// The initial angular velocity of the body, radians per second.
55    pub angular_velocity: Vec3,
56    /// Linear damping used to reduce the linear velocity.
57    pub linear_damping: f32,
58    /// Angular damping used to reduce the angular velocity.
59    pub angular_damping: f32,
60    /// Scale the gravity applied to this body. Non-dimensional.
61    pub gravity_scale: f32,
62    /// Sleep speed threshold, default is 0.05 meters per second.
63    pub sleep_threshold: f32,
64    /// Optional body name for debugging.
65    pub name: String,
66    /// Application specific body data.
67    pub user_data: u64,
68    /// Motion locks to restrict linear and angular movement.
69    pub motion_locks: MotionLocks,
70    /// Set to false if this body should never fall asleep.
71    pub enable_sleep: bool,
72    /// Is this body initially awake or sleeping?
73    pub is_awake: bool,
74    /// Treat this body as a high speed object for continuous collision.
75    pub is_bullet: bool,
76    /// Used to disable a body. A disabled body does not move or collide.
77    pub is_enabled: bool,
78    /// Allow this body to bypass rotational speed limits.
79    pub allow_fast_rotation: bool,
80    /// Enable contact recycling. True by default.
81    pub enable_contact_recycling: bool,
82    /// Used internally to detect a valid definition. DO NOT SET.
83    pub internal_value: i32,
84}
85
86/// Initialize a body definition with the default values. (b3DefaultBodyDef)
87pub fn default_body_def() -> BodyDef {
88    BodyDef {
89        type_: BodyType::Static,
90        position: POS_ZERO,
91        rotation: QUAT_IDENTITY,
92        linear_velocity: VEC3_ZERO,
93        angular_velocity: VEC3_ZERO,
94        linear_damping: 0.0,
95        angular_damping: 0.0,
96        gravity_scale: 1.0,
97        sleep_threshold: 0.05 * get_length_units_per_meter(),
98        name: String::new(),
99        user_data: 0,
100        motion_locks: MotionLocks::default(),
101        enable_sleep: true,
102        is_awake: true,
103        is_bullet: false,
104        is_enabled: true,
105        allow_fast_rotation: false,
106        enable_contact_recycling: true,
107        internal_value: SECRET_COOKIE,
108    }
109}
110
111impl Default for BodyDef {
112    fn default() -> Self {
113        default_body_def()
114    }
115}