Skip to main content

dynamis_model/
collider.rs

1use crate::shape::Shape;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub enum ContactEventMode {
5    None,
6    BeginEnd,
7    Persist,
8}
9
10#[derive(Clone, Copy, Debug)]
11pub struct ColliderDesc {
12    pub shape: Shape,
13    pub offset: [f32; 3],
14    pub rotation: [f32; 4],
15    pub friction: f32,
16    pub restitution: f32,
17    pub scale: [f32; 3],
18    pub sensor: bool,
19    pub collision_group: Option<u32>,
20    pub collision_mask: Option<u32>,
21    pub rolling_friction: f32,
22    pub spin_friction: f32,
23    pub events: ContactEventMode,
24}
25
26impl ColliderDesc {
27    pub fn new(shape: Shape) -> Self {
28        Self {
29            shape,
30            offset: [0.0; 3],
31            rotation: [0.0, 0.0, 0.0, 1.0],
32            friction: 0.5,
33            restitution: 0.0,
34            scale: [1.0; 3],
35            sensor: false,
36            collision_group: None,
37            collision_mask: None,
38            rolling_friction: 0.0,
39            spin_friction: 0.0,
40            events: ContactEventMode::BeginEnd,
41        }
42    }
43
44    pub fn events(mut self, events: ContactEventMode) -> Self {
45        self.events = events;
46        self
47    }
48
49    pub fn offset(mut self, offset: [f32; 3]) -> Self {
50        self.offset = offset;
51        self
52    }
53
54    pub fn rotation(mut self, rotation: [f32; 4]) -> Self {
55        assert!(
56            (rotation[0] * rotation[0]
57                + rotation[1] * rotation[1]
58                + rotation[2] * rotation[2]
59                + rotation[3] * rotation[3]
60                - 1.0)
61                .abs()
62                < 1e-4,
63            "rotation must be a unit quaternion"
64        );
65        self.rotation = rotation;
66        self
67    }
68
69    pub fn friction(mut self, friction: f32) -> Self {
70        assert!(friction >= 0.0, "friction must be non-negative");
71        self.friction = friction;
72        self
73    }
74
75    pub fn restitution(mut self, restitution: f32) -> Self {
76        self.restitution = restitution;
77        self
78    }
79
80    pub fn scale(mut self, scale: [f32; 3]) -> Self {
81        assert!(
82            scale.iter().all(|value| *value > 0.0),
83            "collider scale must be strictly positive"
84        );
85        self.scale = scale;
86        self
87    }
88
89    pub fn sensor(mut self, sensor: bool) -> Self {
90        self.sensor = sensor;
91        self
92    }
93
94    pub fn collision_group(mut self, group: u32) -> Self {
95        self.collision_group = Some(group);
96        self
97    }
98
99    pub fn collision_mask(mut self, mask: u32) -> Self {
100        self.collision_mask = Some(mask);
101        self
102    }
103
104    pub fn rolling_friction(mut self, rolling_friction: f32) -> Self {
105        assert!(
106            rolling_friction >= 0.0,
107            "rolling friction must be non-negative"
108        );
109        self.rolling_friction = rolling_friction;
110        self
111    }
112
113    pub fn spin_friction(mut self, spin_friction: f32) -> Self {
114        assert!(spin_friction >= 0.0, "spin friction must be non-negative");
115        self.spin_friction = spin_friction;
116        self
117    }
118}