dynamis-model 0.7.0

Host facing physics descriptions, states, and derived mass properties
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConstraintKind {
    Ball,
    Distance,
    Revolute,
    Prismatic,
    Fixed,
    Gear,
    Pulley,
    Cone,
    SixDof,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum JointDof {
    Separation,
    Hinge,
    Slide,
    SwingA,
    SwingB,
    Twist,
    Cone,
    Rope,
    Linear(usize),
    Angular(usize),
}

impl ConstraintKind {
    pub const fn dofs(self) -> &'static [JointDof] {
        match self {
            Self::Distance => &[JointDof::Separation],
            Self::Revolute => &[JointDof::Hinge],
            Self::Prismatic => &[JointDof::Slide],
            Self::Ball => &[JointDof::SwingA, JointDof::SwingB, JointDof::Twist],
            Self::Cone => &[JointDof::Cone],
            Self::Gear => &[],
            Self::Pulley => &[JointDof::Rope],
            Self::SixDof => &[
                JointDof::Linear(0),
                JointDof::Linear(1),
                JointDof::Linear(2),
                JointDof::Angular(0),
                JointDof::Angular(1),
                JointDof::Angular(2),
            ],
            Self::Fixed => &[],
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JointState {
    kind: ConstraintKind,
    coordinates: [f32; 6],
    rates: [f32; 6],
    impulses: [f32; 6],
}

impl JointState {
    pub fn new(
        kind: ConstraintKind,
        coordinates: [f32; 6],
        rates: [f32; 6],
        impulses: [f32; 6],
    ) -> Self {
        Self {
            kind,
            coordinates,
            rates,
            impulses,
        }
    }

    pub const fn kind(&self) -> ConstraintKind {
        self.kind
    }

    pub const fn dofs(&self) -> &'static [JointDof] {
        self.kind.dofs()
    }

    pub fn coordinate(&self, dof: JointDof) -> f32 {
        self.coordinates[self.slot(dof)]
    }

    pub fn rate(&self, dof: JointDof) -> f32 {
        self.rates[self.slot(dof)]
    }

    pub fn impulse(&self, dof: JointDof) -> f32 {
        self.impulses[self.slot(dof)]
    }

    fn slot(&self, dof: JointDof) -> usize {
        self.dofs()
            .iter()
            .position(|candidate| *candidate == dof)
            .unwrap_or_else(|| panic!("joint dof {dof:?} is outside the {:?} layout", self.kind))
    }
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintLimit {
    pub min: f32,
    pub max: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintMotor {
    pub target_velocity: f32,
    pub max_force: f32,
    pub target_position: Option<f32>,
    pub stiffness: f32,
    pub damping: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintSpring {
    pub frequency: f32,
    pub damping_ratio: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintSwing {
    pub swing_a: f32,
    pub swing_b: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintBreak {
    pub force: f32,
    pub torque: f32,
}

#[derive(Clone, Copy, Debug)]
pub struct DofDesc {
    pub locked: bool,
    pub limit: Option<ConstraintLimit>,
    pub motor: Option<ConstraintMotor>,
}

impl DofDesc {
    pub fn free() -> Self {
        Self {
            locked: false,
            limit: None,
            motor: None,
        }
    }

    pub fn locked() -> Self {
        Self::free().lock()
    }

    pub fn limited(min: f32, max: f32) -> Self {
        Self::free().limit(min, max)
    }

    pub fn driven(motor: ConstraintMotor) -> Self {
        Self::free().motor(motor)
    }

    pub fn lock(mut self) -> Self {
        self.locked = true;
        self
    }

    pub fn limit(mut self, min: f32, max: f32) -> Self {
        assert!(max >= min, "dof limit max must not be below min");
        self.limit = Some(ConstraintLimit { min, max });
        self
    }

    pub fn motor(mut self, motor: ConstraintMotor) -> Self {
        self.motor = Some(motor);
        self
    }
}

#[derive(Clone, Copy, Debug)]
pub struct ConstraintDesc {
    pub kind: ConstraintKind,
    pub anchor_a: [f32; 3],
    pub anchor_b: [f32; 3],
    pub axis_a: [f32; 3],
    pub axis_b: [f32; 3],
    pub reference: [f32; 4],
    pub rest_length: f32,
    pub limit: Option<ConstraintLimit>,
    pub swing: Option<ConstraintSwing>,
    pub motor: Option<ConstraintMotor>,
    pub spring: Option<ConstraintSpring>,
    pub break_threshold: Option<ConstraintBreak>,
    pub gear_ratio: f32,
    pub pulley_fixed_a: [f32; 3],
    pub pulley_fixed_b: [f32; 3],
    pub cone_angle: f32,
    pub dofs: Option<[DofDesc; 6]>,
    pub warm_start: bool,
    pub disable_collisions: bool,
}

impl ConstraintDesc {
    fn base(kind: ConstraintKind) -> Self {
        Self {
            kind,
            anchor_a: [0.0; 3],
            anchor_b: [0.0; 3],
            axis_a: [0.0, 1.0, 0.0],
            axis_b: [0.0, 1.0, 0.0],
            reference: [0.0, 0.0, 0.0, 1.0],
            rest_length: 0.0,
            limit: None,
            swing: None,
            motor: None,
            spring: None,
            break_threshold: None,
            gear_ratio: 1.0,
            pulley_fixed_a: [0.0; 3],
            pulley_fixed_b: [0.0; 3],
            cone_angle: 0.0,
            dofs: None,
            warm_start: true,
            disable_collisions: true,
        }
    }

    pub fn ball(anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
        Self::base(ConstraintKind::Ball).anchors(anchor_a, anchor_b)
    }

    pub fn rekind(mut self, kind: ConstraintKind) -> Self {
        self.kind = kind;
        self
    }

    pub fn distance(anchor_a: [f32; 3], anchor_b: [f32; 3], distance: f32) -> Self {
        assert!(distance >= 0.0, "constraint distance must be non-negative");
        Self::base(ConstraintKind::Distance)
            .anchors(anchor_a, anchor_b)
            .rest_length(distance)
    }

    pub fn revolute(anchor_a: [f32; 3], anchor_b: [f32; 3], axis: [f32; 3]) -> Self {
        assert!(axis != [0.0; 3], "revolute axis must be non-zero");
        Self::base(ConstraintKind::Revolute)
            .anchors(anchor_a, anchor_b)
            .axis(axis)
    }

    pub fn prismatic(anchor_a: [f32; 3], anchor_b: [f32; 3], axis: [f32; 3]) -> Self {
        assert!(axis != [0.0; 3], "prismatic axis must be non-zero");
        Self::base(ConstraintKind::Prismatic)
            .anchors(anchor_a, anchor_b)
            .axis(axis)
    }

    pub fn fixed(anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
        Self::base(ConstraintKind::Fixed).anchors(anchor_a, anchor_b)
    }

    pub fn gear(axis_a: [f32; 3], axis_b: [f32; 3], ratio: f32) -> Self {
        assert!(axis_a != [0.0; 3], "gear axis a must be non-zero");
        assert!(axis_b != [0.0; 3], "gear axis b must be non-zero");
        assert!(ratio != 0.0, "gear ratio must be non-zero");
        let mut desc = Self::base(ConstraintKind::Gear).axis(axis_a);
        desc.axis_b = axis_b;
        desc.gear_ratio = ratio;
        desc
    }

    pub fn pulley(
        anchor_a: [f32; 3],
        anchor_b: [f32; 3],
        fixed_a: [f32; 3],
        fixed_b: [f32; 3],
        length: f32,
    ) -> Self {
        assert!(length > 0.0, "pulley length must be positive");
        let mut desc = Self::base(ConstraintKind::Pulley)
            .anchors(anchor_a, anchor_b)
            .rest_length(length);
        desc.pulley_fixed_a = fixed_a;
        desc.pulley_fixed_b = fixed_b;
        desc
    }

    pub fn cone(anchor_a: [f32; 3], anchor_b: [f32; 3], axis: [f32; 3], half_angle: f32) -> Self {
        assert!(axis != [0.0; 3], "cone axis must be non-zero");
        assert!(
            (0.0..=std::f32::consts::PI).contains(&half_angle),
            "cone half angle must be within [0, pi]"
        );
        let mut desc = Self::base(ConstraintKind::Cone)
            .anchors(anchor_a, anchor_b)
            .axis(axis);
        desc.axis_b = axis;
        desc.cone_angle = half_angle;
        desc
    }

    pub fn six_dof(
        anchor_a: [f32; 3],
        anchor_b: [f32; 3],
        axis_a: [f32; 3],
        axis_b: [f32; 3],
    ) -> Self {
        assert!(axis_a != [0.0; 3], "six dof axis a must be non-zero");
        assert!(axis_b != [0.0; 3], "six dof axis b must be non-zero");
        let mut desc = Self::base(ConstraintKind::SixDof)
            .anchors(anchor_a, anchor_b)
            .axis(axis_a);
        desc.axis_b = axis_b;
        desc.dofs = Some([DofDesc::free(); 6]);
        desc
    }

    pub fn dofs(mut self, dofs: [DofDesc; 6]) -> Self {
        assert_eq!(
            self.kind,
            ConstraintKind::SixDof,
            "dofs require a six dof constraint"
        );
        self.dofs = Some(dofs);
        self
    }

    pub fn dof(mut self, index: usize, desc: DofDesc) -> Self {
        assert_eq!(
            self.kind,
            ConstraintKind::SixDof,
            "dofs require a six dof constraint"
        );
        assert!(index < 6, "dof index must be within 0..6");
        let dofs = self.dofs.get_or_insert([DofDesc::free(); 6]);
        dofs[index] = desc;
        self
    }

    pub fn anchors(mut self, anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
        self.anchor_a = anchor_a;
        self.anchor_b = anchor_b;
        self
    }

    pub fn axis(mut self, axis: [f32; 3]) -> Self {
        assert!(axis != [0.0; 3], "constraint axis must be non-zero");
        self.axis_a = axis;
        self
    }

    pub fn axis_b(mut self, axis_b: [f32; 3]) -> Self {
        assert!(axis_b != [0.0; 3], "constraint axis b must be non-zero");
        self.axis_b = axis_b;
        self
    }

    pub fn reference(mut self, reference: [f32; 4]) -> Self {
        assert!(
            (reference[0] * reference[0]
                + reference[1] * reference[1]
                + reference[2] * reference[2]
                + reference[3] * reference[3]
                - 1.0)
                .abs()
                < 1e-4,
            "reference must be a unit quaternion"
        );
        self.reference = reference;
        self
    }

    pub fn rest_length(mut self, rest_length: f32) -> Self {
        assert!(
            rest_length >= 0.0,
            "constraint distance must be non-negative"
        );
        self.rest_length = rest_length;
        self
    }

    pub fn limit(mut self, min: f32, max: f32) -> Self {
        assert!(max >= min, "constraint limit max must not be below min");
        self.limit = Some(ConstraintLimit { min, max });
        self
    }

    pub fn swing(mut self, swing_a: f32, swing_b: f32) -> Self {
        assert!(swing_a >= 0.0, "swing limit must be non-negative");
        assert!(swing_b >= 0.0, "swing limit must be non-negative");
        self.swing = Some(ConstraintSwing { swing_a, swing_b });
        self
    }

    pub fn motor(mut self, target_velocity: f32) -> Self {
        let motor = self.motor.get_or_insert(ConstraintMotor {
            target_velocity,
            max_force: 0.0,
            target_position: None,
            stiffness: 0.0,
            damping: 0.0,
        });
        motor.target_velocity = target_velocity;
        self
    }

    pub fn motor_force(mut self, max_force: f32) -> Self {
        assert!(max_force >= 0.0, "motor force must be non-negative");
        let motor = self.motor.get_or_insert(ConstraintMotor {
            target_velocity: 0.0,
            max_force: 0.0,
            target_position: None,
            stiffness: 0.0,
            damping: 0.0,
        });
        motor.max_force = max_force;
        self
    }

    pub fn servo(mut self, target_position: f32, stiffness: f32, damping: f32) -> Self {
        assert!(
            (0.0..=1.0).contains(&stiffness),
            "servo stiffness must be within [0, 1]"
        );
        assert!(
            (0.0..=1.0).contains(&damping),
            "servo damping must be within [0, 1]"
        );
        let motor = self.motor.get_or_insert(ConstraintMotor {
            target_velocity: 0.0,
            max_force: 0.0,
            target_position: None,
            stiffness: 0.0,
            damping: 0.0,
        });
        motor.target_position = Some(target_position);
        motor.stiffness = stiffness;
        motor.damping = damping;
        self
    }

    pub fn spring(mut self, frequency: f32, damping_ratio: f32) -> Self {
        assert!(frequency >= 0.0, "spring frequency must be non-negative");
        assert!(
            damping_ratio >= 0.0,
            "spring damping ratio must be non-negative"
        );
        self.spring = Some(ConstraintSpring {
            frequency,
            damping_ratio,
        });
        self
    }

    pub fn break_threshold(mut self, force: f32, torque: f32) -> Self {
        assert!(force >= 0.0, "break force must be non-negative");
        assert!(torque >= 0.0, "break torque must be non-negative");
        self.break_threshold = Some(ConstraintBreak { force, torque });
        self
    }

    pub fn warm_start(mut self, warm_start: bool) -> Self {
        self.warm_start = warm_start;
        self
    }

    pub fn disable_collisions(mut self, disable: bool) -> Self {
        self.disable_collisions = disable;
        self
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ConstraintHandle {
    pub id: u32,
    pub generation: u32,
}