nphysics3d/object/
ground.rs

1use na::{DVectorSlice, DVectorSliceMut, RealField};
2
3use crate::math::{Force, ForceType, Inertia, Isometry, Point, Translation, Vector, Velocity};
4use crate::object::{
5    ActivationStatus, Body, BodyPart, BodyPartMotion, BodyStatus, BodyUpdateStatus,
6};
7use crate::solver::{ForceDirection, IntegrationParameters};
8use ncollide::shape::DeformationsType;
9
10/// A singleton representing the ground.
11///
12/// Most of its methods are useless but provided anyway to be
13/// similar to the other bodies.
14#[derive(Clone, Debug)]
15pub struct Ground<N: RealField + Copy> {
16    companion_id: usize,
17    activation: ActivationStatus<N>,
18    data: [N; 0],
19}
20
21impl<N: RealField + Copy> Ground<N> {
22    /// Build a new ground structrure.
23    pub fn new() -> Self {
24        Ground {
25            companion_id: 0,
26            activation: ActivationStatus::new_inactive(),
27            data: [],
28        }
29    }
30}
31
32impl<N: RealField + Copy> Body<N> for Ground<N> {
33    #[inline]
34    fn gravity_enabled(&self) -> bool {
35        false
36    }
37
38    #[inline]
39    fn enable_gravity(&mut self, _: bool) {}
40
41    #[inline]
42    fn step_started(&mut self) {}
43
44    #[inline]
45    fn update_kinematics(&mut self) {}
46
47    #[inline]
48    fn update_dynamics(&mut self, _: N) {}
49
50    #[inline]
51    fn update_acceleration(&mut self, _: &Vector<N>, _: &IntegrationParameters<N>) {}
52
53    #[inline]
54    fn clear_forces(&mut self) {}
55
56    #[inline]
57    fn clear_update_flags(&mut self) {}
58
59    #[inline]
60    fn update_status(&self) -> BodyUpdateStatus {
61        BodyUpdateStatus::empty()
62    }
63
64    #[inline]
65    fn num_parts(&self) -> usize {
66        1
67    }
68
69    #[inline]
70    fn part(&self, i: usize) -> Option<&dyn BodyPart<N>> {
71        if i == 0 {
72            Some(self)
73        } else {
74            None
75        }
76    }
77
78    #[inline]
79    fn is_ground(&self) -> bool {
80        true
81    }
82
83    #[inline]
84    fn apply_displacement(&mut self, _: &[N]) {}
85
86    #[inline]
87    fn status(&self) -> BodyStatus {
88        BodyStatus::Static
89    }
90
91    #[inline]
92    fn set_status(&mut self, _: BodyStatus) {}
93
94    #[inline]
95    fn activation_status(&self) -> &ActivationStatus<N> {
96        &self.activation
97    }
98
99    #[inline]
100    fn is_active(&self) -> bool {
101        false
102    }
103
104    #[inline]
105    fn is_dynamic(&self) -> bool {
106        false
107    }
108
109    #[inline]
110    fn is_kinematic(&self) -> bool {
111        false
112    }
113
114    #[inline]
115    fn is_static(&self) -> bool {
116        true
117    }
118
119    #[inline]
120    fn deformed_positions(&self) -> Option<(DeformationsType, &[N])> {
121        None
122    }
123
124    #[inline]
125    fn deformed_positions_mut(&mut self) -> Option<(DeformationsType, &mut [N])> {
126        None
127    }
128
129    #[inline]
130    fn ndofs(&self) -> usize {
131        0
132    }
133
134    #[inline]
135    fn generalized_acceleration(&self) -> DVectorSlice<N> {
136        DVectorSlice::from_slice(&self.data[..], 0)
137    }
138
139    #[inline]
140    fn generalized_velocity(&self) -> DVectorSlice<N> {
141        DVectorSlice::from_slice(&self.data[..], 0)
142    }
143
144    #[inline]
145    fn companion_id(&self) -> usize {
146        self.companion_id
147    }
148
149    #[inline]
150    fn set_companion_id(&mut self, id: usize) {
151        self.companion_id = id
152    }
153
154    #[inline]
155    fn generalized_velocity_mut(&mut self) -> DVectorSliceMut<N> {
156        DVectorSliceMut::from_slice(&mut self.data[..], 0)
157    }
158
159    #[inline]
160    fn velocity_at_point(&self, _: usize, _: &Point<N>) -> Velocity<N> {
161        Velocity::zero()
162    }
163
164    #[inline]
165    fn integrate(&mut self, _: &IntegrationParameters<N>) {}
166
167    #[inline]
168    fn activate(&mut self) {}
169
170    #[inline]
171    fn activate_with_energy(&mut self, _: N) {}
172
173    #[inline]
174    fn deactivate(&mut self) {}
175
176    #[inline]
177    fn set_deactivation_threshold(&mut self, _: Option<N>) {}
178
179    #[inline]
180    fn world_point_at_material_point(&self, _: &dyn BodyPart<N>, point: &Point<N>) -> Point<N> {
181        *point
182    }
183
184    #[inline]
185    fn position_at_material_point(&self, _: &dyn BodyPart<N>, point: &Point<N>) -> Isometry<N> {
186        Isometry::from_parts(Translation::from(point.coords), na::one())
187    }
188
189    #[inline]
190    fn material_point_at_world_point(&self, _: &dyn BodyPart<N>, point: &Point<N>) -> Point<N> {
191        *point
192    }
193
194    #[inline]
195    fn fill_constraint_geometry(
196        &self,
197        _: &dyn BodyPart<N>,
198        _: usize, // FIXME: keep this parameter?
199        _: &Point<N>,
200        _: &ForceDirection<N>,
201        _: usize,
202        _: usize,
203        _: &mut [N],
204        _: &mut N,
205        _: Option<&DVectorSlice<N>>,
206        _: Option<&mut N>,
207    ) {
208    }
209
210    fn advance(&mut self, _: N) {}
211
212    fn validate_advancement(&mut self) {}
213
214    fn clamp_advancement(&mut self) {}
215
216    fn part_motion(&self, _: usize, _: N) -> Option<BodyPartMotion<N>> {
217        Some(BodyPartMotion::Static(Isometry::identity()))
218    }
219
220    #[inline]
221    fn has_active_internal_constraints(&mut self) -> bool {
222        false
223    }
224
225    #[inline]
226    fn setup_internal_velocity_constraints(
227        &mut self,
228        _: &DVectorSlice<N>,
229        _: &IntegrationParameters<N>,
230    ) {
231    }
232
233    #[inline]
234    fn warmstart_internal_velocity_constraints(&mut self, _: &mut DVectorSliceMut<N>) {}
235
236    #[inline]
237    fn step_solve_internal_velocity_constraints(&mut self, _: &mut DVectorSliceMut<N>) {}
238
239    #[inline]
240    fn step_solve_internal_position_constraints(&mut self, _: &IntegrationParameters<N>) {}
241
242    #[inline]
243    fn apply_force(&mut self, _: usize, _: &Force<N>, _: ForceType, _: bool) {}
244
245    #[inline]
246    fn apply_local_force(&mut self, _: usize, _: &Force<N>, _: ForceType, _: bool) {}
247
248    #[inline]
249    fn apply_force_at_point(
250        &mut self,
251        _: usize,
252        _: &Vector<N>,
253        _: &Point<N>,
254        _: ForceType,
255        _: bool,
256    ) {
257    }
258
259    #[inline]
260    fn apply_local_force_at_point(
261        &mut self,
262        _: usize,
263        _: &Vector<N>,
264        _: &Point<N>,
265        _: ForceType,
266        _: bool,
267    ) {
268    }
269
270    #[inline]
271    fn apply_force_at_local_point(
272        &mut self,
273        _: usize,
274        _: &Vector<N>,
275        _: &Point<N>,
276        _: ForceType,
277        _: bool,
278    ) {
279    }
280
281    #[inline]
282    fn apply_local_force_at_local_point(
283        &mut self,
284        _: usize,
285        _: &Vector<N>,
286        _: &Point<N>,
287        _: ForceType,
288        _: bool,
289    ) {
290    }
291}
292
293impl<N: RealField + Copy> BodyPart<N> for Ground<N> {
294    #[inline]
295    fn is_ground(&self) -> bool {
296        true
297    }
298
299    #[inline]
300    fn center_of_mass(&self) -> Point<N> {
301        Point::origin()
302    }
303
304    #[inline]
305    fn local_center_of_mass(&self) -> Point<N> {
306        Point::origin()
307    }
308
309    #[inline]
310    fn position(&self) -> Isometry<N> {
311        Isometry::identity()
312    }
313
314    #[inline]
315    fn safe_position(&self) -> Isometry<N> {
316        Isometry::identity()
317    }
318
319    #[inline]
320    fn velocity(&self) -> Velocity<N> {
321        Velocity::zero()
322    }
323
324    #[inline]
325    fn inertia(&self) -> Inertia<N> {
326        Inertia::zero()
327    }
328
329    #[inline]
330    fn local_inertia(&self) -> Inertia<N> {
331        Inertia::zero()
332    }
333}