1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use na::{self, DVectorSlice, DVectorSliceMut, Real};
use math::{Force, Inertia, Isometry, Point, Rotation, Translation, Vector, Velocity, SPATIAL_DIM};
use object::{ActivationStatus, BodyHandle, BodyStatus};
use solver::IntegrationParameters;
#[cfg(feature = "dim3")]
use math::AngularVector;
#[cfg(feature = "dim3")]
use utils::GeneralizedCross;
pub struct RigidBody<N: Real> {
handle: BodyHandle,
local_to_world: Isometry<N>,
velocity: Velocity<N>,
local_inertia: Inertia<N>,
inertia: Inertia<N>,
local_com: Point<N>,
com: Point<N>,
augmented_mass: Inertia<N>,
inv_augmented_mass: Inertia<N>,
external_forces: Force<N>,
acceleration: Velocity<N>,
status: BodyStatus,
activation: ActivationStatus<N>,
companion_id: usize,
}
impl<N: Real> RigidBody<N> {
pub fn new(
handle: BodyHandle,
position: Isometry<N>,
local_inertia: Inertia<N>,
local_com: Point<N>,
) -> Self {
let inertia = local_inertia.transformed(&position);
let com = position * local_com;
RigidBody {
handle,
local_to_world: position,
velocity: Velocity::zero(),
local_inertia,
inertia,
local_com,
com,
augmented_mass: inertia,
inv_augmented_mass: inertia.inverse(),
external_forces: Force::zero(),
acceleration: Velocity::zero(),
status: BodyStatus::Dynamic,
activation: ActivationStatus::new_active(),
companion_id: 0,
}
}
#[inline]
pub fn activation_status(&self) -> &ActivationStatus<N> {
&self.activation
}
#[inline]
pub fn activation_status_mut(&mut self) -> &mut ActivationStatus<N> {
&mut self.activation
}
#[inline]
pub fn activate(&mut self) {
if let Some(threshold) = self.activation.deactivation_threshold() {
self.activate_with_energy(threshold * na::convert(2.0));
}
}
#[inline]
pub fn activate_with_energy(&mut self, energy: N) {
self.activation.set_energy(energy)
}
#[inline]
pub fn deactivate(&mut self) {
self.activation.set_energy(N::zero());
self.velocity = Velocity::zero();
}
#[inline]
pub fn is_active(&self) -> bool {
match self.status {
BodyStatus::Dynamic => self.activation.is_active(),
BodyStatus::Kinematic => true,
BodyStatus::Static => false,
BodyStatus::Disabled => false,
}
}
#[inline]
pub fn status(&self) -> BodyStatus {
self.status
}
#[inline]
pub fn set_status(&mut self, status: BodyStatus) {
self.status = status
}
#[inline]
pub fn companion_id(&self) -> usize {
self.companion_id
}
#[inline]
pub fn set_companion_id(&mut self, id: usize) {
self.companion_id = id
}
#[inline]
pub fn is_dynamic(&self) -> bool {
self.status == BodyStatus::Dynamic
}
#[inline]
pub fn is_static(&self) -> bool {
self.status == BodyStatus::Static
}
#[inline]
pub fn is_kinematic(&self) -> bool {
self.status == BodyStatus::Kinematic
}
#[inline]
pub fn center_of_mass(&self) -> Point<N> {
self.com
}
#[inline]
pub fn velocity(&self) -> &Velocity<N> {
&self.velocity
}
#[inline]
pub fn set_position(&mut self, pos: Isometry<N>) {
self.local_to_world = pos;
self.com = pos * self.local_com;
}
#[inline]
pub fn set_velocity(&mut self, vel: Velocity<N>) {
self.velocity = vel
}
#[inline]
pub fn set_linear_velocity(&mut self, vel: Vector<N>) {
self.velocity.linear = vel
}
#[cfg(feature = "dim2")]
#[inline]
pub fn set_angular_velocity(&mut self, vel: N) {
self.velocity.angular = vel
}
#[cfg(feature = "dim3")]
#[inline]
pub fn set_angular_velocity(&mut self, vel: AngularVector<N>) {
self.velocity.angular = vel
}
pub fn clear_dynamics(&mut self) {
self.augmented_mass = Inertia::zero();
self.acceleration = Velocity::zero();
self.external_forces = Force::zero();
}
#[allow(unused_variables)]
pub fn update_dynamics(&mut self, gravity: &Vector<N>, params: &IntegrationParameters<N>) {
match self.status {
BodyStatus::Dynamic => {
#[cfg(feature = "dim3")]
{
self.inertia = self.local_inertia.transformed(&self.local_to_world);
self.augmented_mass += self.inertia;
let i = &self.inertia.angular;
let w = &self.velocity.angular;
let iw = i * w;
let w_dt = w * params.dt;
let w_dt_cross = w_dt.gcross_matrix();
let iw_dt_cross = (iw * params.dt).gcross_matrix();
self.augmented_mass.angular += w_dt_cross * i - iw_dt_cross;
self.inv_augmented_mass = self.augmented_mass.inverse();
let gyroscopic = -w.cross(&iw);
self.acceleration.angular += self.inv_augmented_mass.angular * gyroscopic;
}
self.acceleration.linear += *gravity;
self.acceleration += self.inv_augmented_mass * self.external_forces
}
_ => {}
}
}
#[inline]
pub fn local_inertia(&self) -> &Inertia<N> {
&self.local_inertia
}
#[inline]
pub fn inertia(&self) -> &Inertia<N> {
&self.inertia
}
#[inline]
pub fn augmented_mass(&self) -> &Inertia<N> {
&self.augmented_mass
}
#[inline]
pub fn inv_augmented_mass(&self) -> &Inertia<N> {
&self.inv_augmented_mass
}
#[inline]
pub fn handle(&self) -> BodyHandle {
self.handle
}
#[inline]
pub fn ndofs(&self) -> usize {
SPATIAL_DIM
}
#[inline]
pub fn generalized_velocity(&self) -> DVectorSlice<N> {
DVectorSlice::from_slice(self.velocity.as_slice(), SPATIAL_DIM)
}
#[inline]
pub fn generalized_velocity_mut(&mut self) -> DVectorSliceMut<N> {
DVectorSliceMut::from_slice(self.velocity.as_mut_slice(), SPATIAL_DIM)
}
#[inline]
pub fn generalized_acceleration(&self) -> DVectorSlice<N> {
DVectorSlice::from_slice(self.acceleration.as_slice(), SPATIAL_DIM)
}
#[inline]
pub fn integrate(&mut self, params: &IntegrationParameters<N>) {
let disp = self.velocity * params.dt;
self.apply_displacement(&disp);
}
#[inline]
pub fn apply_displacement(&mut self, displacement: &Velocity<N>) {
let rotation = Rotation::new(displacement.angular);
let translation = Translation::from_vector(displacement.linear);
let shift = Translation::from_vector(self.com.coords);
let disp = translation * shift * rotation * shift.inverse();
let new_pos = disp * self.local_to_world;
self.set_position(new_pos);
}
#[inline]
pub fn apply_force(&mut self, force: &Force<N>) {
self.external_forces.linear += force.linear;
self.external_forces.angular += force.angular;
}
#[inline]
pub fn position(&self) -> Isometry<N> {
self.local_to_world
}
#[inline]
pub fn body_jacobian_mul_force(&self, force: &Force<N>, out: &mut [N]) {
out[..SPATIAL_DIM].copy_from_slice(force.as_slice());
}
#[inline]
pub fn inv_mass_mul_generalized_forces(&self, out: &mut [N]) {
let force = Force::from_slice(out);
self.inv_mass_mul_force(&force, out)
}
#[inline]
pub fn inv_mass_mul_force(&self, force: &Force<N>, out: &mut [N]) {
let acc = self.inv_augmented_mass * *force;
out[..SPATIAL_DIM].copy_from_slice(acc.as_slice());
}
}