Skip to main content

bevy_rapier2d/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                let pose = self.raw.canonical_transform();
46                #[cfg(feature = "dim2")]
47                return (pose.translation, pose.rotation.angle());
48                #[cfg(feature = "dim3")]
49                return (pose.translation, pose.rotation);
50            }
51
52            /// The rotation `r` such that `r * Y` is collinear with `b - a`.
53            #[cfg(feature = "dim2")]
54            pub fn rotation_wrt_y(&self) -> Rot {
55                self.raw.rotation_wrt_y().angle()
56            }
57
58            /// The rotation `r` such that `r * Y` is collinear with `b - a`.
59            #[cfg(feature = "dim3")]
60            pub fn rotation_wrt_y(&self) -> Rot {
61                self.raw.rotation_wrt_y()
62            }
63
64            /// The transform `t` such that `t * Y` is collinear with `b - a` and such that `t * origin = (b + a) / 2.0`.
65            pub fn transform_wrt_y(&self) -> (Vect, Rot) {
66                let pose = self.raw.transform_wrt_y();
67                #[cfg(feature = "dim2")]
68                return (pose.translation, pose.rotation.angle());
69                #[cfg(feature = "dim3")]
70                return (pose.translation, pose.rotation);
71            }
72        }
73    }
74);
75
76impl_ref_methods!(CapsuleView);
77
78/// Read-write access to the properties of a capsule.
79pub struct CapsuleViewMut<'a> {
80    /// The raw shape from Rapier.
81    pub raw: &'a mut Capsule,
82}
83
84impl_ref_methods!(CapsuleViewMut);
85
86impl CapsuleViewMut<'_> {
87    /// Set the segment of this capsule.
88    pub fn set_segment(&mut self, start: Vect, end: Vect) {
89        self.raw.segment.a = start;
90        self.raw.segment.b = end;
91    }
92
93    /// Set the radius of this capsule.
94    pub fn set_radius(&mut self, radius: Real) {
95        self.raw.radius = radius;
96    }
97}