Skip to main content

dynamis_model/
body.rs

1#[derive(Clone, Copy, Debug, PartialEq)]
2pub struct BodyHandle {
3    pub id: u32,
4    pub generation: u32,
5}
6
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct BodyState {
9    pub position: [f32; 3],
10    pub orientation: [f32; 4],
11    pub velocity: [f32; 3],
12    pub angular_velocity: [f32; 3],
13    pub inverse_mass: f32,
14    pub step: u64,
15}
16
17#[derive(Clone, Copy)]
18pub struct BodyDesc {
19    pub position: [f32; 3],
20    pub orientation: [f32; 4],
21    pub velocity: [f32; 3],
22    pub angular_velocity: [f32; 3],
23    pub mass: f32,
24    pub restitution: f32,
25    pub friction: f32,
26    pub radius: f32,
27}
28
29impl BodyDesc {
30    pub fn sphere(radius: f32) -> Self {
31        assert!(radius > 0.0, "collider radius must be strictly positive");
32        Self {
33            position: [0.0; 3],
34            orientation: [0.0, 0.0, 0.0, 1.0],
35            velocity: [0.0; 3],
36            angular_velocity: [0.0; 3],
37            mass: 1.0,
38            restitution: 0.0,
39            friction: 0.5,
40            radius,
41        }
42    }
43
44    pub fn static_sphere(radius: f32) -> Self {
45        assert!(radius > 0.0, "collider radius must be strictly positive");
46        Self {
47            mass: 0.0,
48            ..Self::sphere(radius)
49        }
50    }
51
52    pub fn position(mut self, position: [f32; 3]) -> Self {
53        self.position = position;
54        self
55    }
56
57    pub fn orientation(mut self, orientation: [f32; 4]) -> Self {
58        assert!(
59            (orientation[0] * orientation[0]
60                + orientation[1] * orientation[1]
61                + orientation[2] * orientation[2]
62                + orientation[3] * orientation[3]
63                - 1.0)
64                .abs()
65                < 1e-4,
66            "orientation must be a unit quaternion"
67        );
68        self.orientation = orientation;
69        self
70    }
71
72    pub fn velocity(mut self, velocity: [f32; 3]) -> Self {
73        self.velocity = velocity;
74        self
75    }
76
77    pub fn angular_velocity(mut self, angular_velocity: [f32; 3]) -> Self {
78        self.angular_velocity = angular_velocity;
79        self
80    }
81
82    pub fn mass(mut self, mass: f32) -> Self {
83        assert!(mass >= 0.0, "mass must be non-negative");
84        self.mass = mass;
85        self
86    }
87
88    pub fn restitution(mut self, restitution: f32) -> Self {
89        self.restitution = restitution;
90        self
91    }
92
93    pub fn friction(mut self, friction: f32) -> Self {
94        assert!(friction >= 0.0, "friction must be non-negative");
95        self.friction = friction;
96        self
97    }
98}