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