collider_shape/capsule.rs
1use crate::shape::Shape;
2
3/// A capsule shape aligned along the `z`-axis.
4///
5/// Mathematically, a capsule is the set of points that are at most `radius` units away from the line segment.
6/// The line segment is defined by the two endpoints at `(0, 0, -half_length)` and `(0, 0, half_length)`.
7#[derive(PartialEq, Debug, Copy, Clone)]
8pub struct Capsule {
9 /// The radius of the capsule.
10 pub radius: f32,
11 /// The half length of the capsule along the `z`-axis.
12 pub half_length: f32,
13}
14
15impl Capsule {
16 /// Creates a new capsule with given radius and half length.
17 ///
18 /// # Arguments
19 ///
20 /// * `radius` - The radius of the capsule.
21 /// * `half_length` - The half length of the capsule along the `z`-axis.
22 pub fn new(radius: f32, half_length: f32) -> Self {
23 Capsule {
24 radius,
25 half_length,
26 }
27 }
28}
29
30impl Shape for Capsule {
31 fn is_convex(&self) -> bool {
32 true
33 }
34
35 fn clone_box(&self) -> Box<dyn Shape + Send + Sync> {
36 Box::new(*self)
37 }
38
39 fn get_shape_type(&self) -> crate::shape::ShapeType {
40 crate::shape::ShapeType::Capsule
41 }
42
43 fn get_radius(&self) -> Option<f32> {
44 Some(self.radius)
45 }
46
47 fn get_half_length(&self) -> Option<f32> {
48 Some(self.half_length)
49 }
50}