bevy_rapier3d/geometry/shape_views/
capsule.rs

1use super::SegmentView;
2use crate::math::{Real, Rot, Vect};
3use rapier::parry::shape::Capsule;
4
5/// Read-only access to the properties of a capsule.
6#[derive(Copy, Clone)]
7pub struct CapsuleView<'a> {
8    /// The raw shape from Rapier.
9    pub raw: &'a Capsule,
10}
11
12macro_rules! impl_ref_methods(
13    ($View: ident) => {
14        impl<'a> $View<'a> {
15            /// The axis and endpoint of the capsule.
16            pub fn segment(&self) -> SegmentView<'_> {
17                SegmentView {
18                    raw: &self.raw.segment,
19                }
20            }
21
22            /// The radius of the capsule.
23            pub fn radius(&self) -> Real {
24                self.raw.radius
25            }
26
27            /// The height of this capsule.
28            pub fn height(&self) -> Real {
29                self.raw.height()
30            }
31
32            /// The half-height of this capsule.
33            pub fn half_height(&self) -> Real {
34                self.raw.half_height()
35            }
36
37            /// The center of this capsule.
38            pub fn center(&self) -> Vect {
39                self.raw.center().into()
40            }
41
42            /// The transformation such that `t * Y` is collinear with `b - a` and `t * origin` equals
43            /// the capsule's center.
44            pub fn canonical_transform(&self) -> (Vect, Rot) {
45                self.raw.canonical_transform().into()
46            }
47
48            /// The rotation `r` such that `r * Y` is collinear with `b - a`.
49            #[cfg(feature = "dim2")]
50            pub fn rotation_wrt_y(&self) -> Rot {
51                self.raw.rotation_wrt_y().angle()
52            }
53
54            /// The rotation `r` such that `r * Y` is collinear with `b - a`.
55            #[cfg(feature = "dim3")]
56            pub fn rotation_wrt_y(&self) -> Rot {
57                self.raw.rotation_wrt_y().into()
58            }
59
60            /// The transform `t` such that `t * Y` is collinear with `b - a` and such that `t * origin = (b + a) / 2.0`.
61            pub fn transform_wrt_y(&self) -> (Vect, Rot) {
62                self.raw.transform_wrt_y().into()
63            }
64        }
65    }
66);
67
68impl_ref_methods!(CapsuleView);
69
70/// Read-write access to the properties of a capsule.
71pub struct CapsuleViewMut<'a> {
72    /// The raw shape from Rapier.
73    pub raw: &'a mut Capsule,
74}
75
76impl_ref_methods!(CapsuleViewMut);
77
78impl CapsuleViewMut<'_> {
79    /// Set the segment of this capsule.
80    pub fn set_segment(&mut self, start: Vect, end: Vect) {
81        self.raw.segment.a = start.into();
82        self.raw.segment.b = end.into();
83    }
84
85    /// Set the radius of this capsule.
86    pub fn set_radius(&mut self, radius: Real) {
87        self.raw.radius = radius;
88    }
89}