Skip to main content

pfx_physics/rigid/
body.rs

1use super::Pose;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum Kind {
5    Dynamic,
6    Kinematic,
7    Fixed,
8}
9
10#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
11pub struct Locks {
12    pub translation: [bool; 3],
13    pub rotation: [bool; 3],
14}
15
16impl Locks {
17    pub const NONE: Self = Self {
18        translation: [false; 3],
19        rotation: [false; 3],
20    };
21    pub const ROTATION: Self = Self {
22        translation: [false; 3],
23        rotation: [true; 3],
24    };
25    pub const PLANE_XY: Self = Self {
26        translation: [false, false, true],
27        rotation: [true, true, false],
28    };
29    pub const PLANE_YZ: Self = Self {
30        translation: [true, false, false],
31        rotation: [false, true, true],
32    };
33
34    pub fn any(&self) -> bool {
35        self.translation.iter().chain(&self.rotation).any(|&l| l)
36    }
37}
38
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub struct Mass {
41    pub mass: f32,
42    pub center: [f32; 3],
43    pub inertia: [f32; 3],
44}
45
46#[derive(Clone, Copy, Debug, PartialEq)]
47pub struct BodyDesc {
48    pub kind: Kind,
49    pub pose: Pose,
50    pub linear_velocity: [f32; 3],
51    pub angular_velocity: [f32; 3],
52    pub gravity_scale: f32,
53    pub linear_damping: f32,
54    pub angular_damping: f32,
55    pub locks: Locks,
56    pub ccd: bool,
57    pub can_sleep: bool,
58    pub asleep: bool,
59    pub mass: Option<Mass>,
60}
61
62impl BodyDesc {
63    pub fn dynamic(pose: Pose) -> Self {
64        Self {
65            kind: Kind::Dynamic,
66            pose,
67            linear_velocity: [0.0; 3],
68            angular_velocity: [0.0; 3],
69            gravity_scale: 1.0,
70            linear_damping: 0.0,
71            angular_damping: 0.0,
72            locks: Locks::NONE,
73            ccd: false,
74            can_sleep: true,
75            asleep: false,
76            mass: None,
77        }
78    }
79
80    pub fn kinematic(pose: Pose) -> Self {
81        Self {
82            kind: Kind::Kinematic,
83            ..Self::dynamic(pose)
84        }
85    }
86
87    pub fn fixed(pose: Pose) -> Self {
88        Self {
89            kind: Kind::Fixed,
90            ..Self::dynamic(pose)
91        }
92    }
93
94    pub fn with_velocity(mut self, linear: [f32; 3]) -> Self {
95        self.linear_velocity = linear;
96        self
97    }
98
99    pub fn with_spin(mut self, angular: [f32; 3]) -> Self {
100        self.angular_velocity = angular;
101        self
102    }
103
104    pub fn with_locks(mut self, locks: Locks) -> Self {
105        self.locks = locks;
106        self
107    }
108
109    pub fn with_ccd(mut self) -> Self {
110        self.ccd = true;
111        self
112    }
113
114    pub fn with_mass(mut self, mass: Mass) -> Self {
115        self.mass = Some(mass);
116        self
117    }
118
119    pub fn with_damping(mut self, linear: f32, angular: f32) -> Self {
120        self.linear_damping = linear;
121        self.angular_damping = angular;
122        self
123    }
124
125    pub fn with_gravity_scale(mut self, scale: f32) -> Self {
126        self.gravity_scale = scale;
127        self
128    }
129}
130
131impl Default for BodyDesc {
132    fn default() -> Self {
133        Self::dynamic(Pose::IDENTITY)
134    }
135}
136
137#[derive(Clone, Copy, Debug, PartialEq)]
138pub struct BodyState {
139    pub pose: Pose,
140    pub linear_velocity: [f32; 3],
141    pub angular_velocity: [f32; 3],
142    pub asleep: bool,
143}