Skip to main content

dynamis_model/
constraint.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum ConstraintKind {
3    Ball,
4    Distance,
5    Revolute,
6    Prismatic,
7    Fixed,
8}
9
10#[derive(Clone, Copy, Debug)]
11pub struct ConstraintDesc {
12    pub kind: ConstraintKind,
13    pub anchor_a: [f32; 3],
14    pub anchor_b: [f32; 3],
15    pub axis: [f32; 3],
16    pub distance: f32,
17}
18
19impl ConstraintDesc {
20    pub fn ball(anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
21        Self {
22            kind: ConstraintKind::Ball,
23            anchor_a,
24            anchor_b,
25            axis: [0.0, 1.0, 0.0],
26            distance: 0.0,
27        }
28    }
29
30    pub fn distance(anchor_a: [f32; 3], anchor_b: [f32; 3], distance: f32) -> Self {
31        assert!(distance >= 0.0, "constraint distance must be non-negative");
32        Self {
33            kind: ConstraintKind::Distance,
34            anchor_a,
35            anchor_b,
36            axis: [0.0, 1.0, 0.0],
37            distance,
38        }
39    }
40
41    pub fn revolute(anchor_a: [f32; 3], anchor_b: [f32; 3], axis: [f32; 3]) -> Self {
42        assert!(axis != [0.0; 3], "revolute axis must be non-zero");
43        Self {
44            kind: ConstraintKind::Revolute,
45            anchor_a,
46            anchor_b,
47            axis,
48            distance: 0.0,
49        }
50    }
51
52    pub fn prismatic(anchor_a: [f32; 3], anchor_b: [f32; 3], axis: [f32; 3]) -> Self {
53        assert!(axis != [0.0; 3], "prismatic axis must be non-zero");
54        Self {
55            kind: ConstraintKind::Prismatic,
56            anchor_a,
57            anchor_b,
58            axis,
59            distance: 0.0,
60        }
61    }
62
63    pub fn fixed(anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
64        Self {
65            kind: ConstraintKind::Fixed,
66            anchor_a,
67            anchor_b,
68            axis: [0.0, 1.0, 0.0],
69            distance: 0.0,
70        }
71    }
72}
73
74#[derive(Clone, Copy, Debug, PartialEq)]
75pub struct ConstraintHandle {
76    pub id: u32,
77    pub generation: u32,
78}