parry3d 0.26.0

3 dimensional collision detection library in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::math::{Pose, Real, Rotation, Vector};
use crate::shape::{Segment, SupportMap};

#[cfg(feature = "alloc")]
use either::Either;

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
#[cfg_attr(feature = "encase", derive(encase::ShaderType))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)
)]
#[repr(C)]
/// A capsule shape, also known as a pill or capped cylinder.
///
/// A capsule is defined by a line segment (its central axis) and a radius. It can be
/// visualized as a cylinder with hemispherical (2D: semicircular) caps on both ends.
/// This makes it perfect for representing elongated round objects.
///
/// # Structure
///
/// - **Segment**: The central axis from point `a` to point `b`
/// - **Radius**: The thickness around the segment
/// - **In 2D**: Looks like a rounded rectangle or "stadium" shape
/// - **In 3D**: Looks like a cylinder with spherical caps (a pill)
///
/// # Properties
///
/// - **Convex**: Yes, capsules are always convex
/// - **Smooth**: Completely smooth surface (no edges or corners)
/// - **Support mapping**: Efficient (constant time)
/// - **Rolling**: Excellent for objects that need to roll smoothly
///
/// # Use Cases
///
/// Capsules are ideal for:
/// - Characters and humanoid figures (torso, limbs)
/// - Pills, medicine capsules
/// - Elongated projectiles (missiles, torpedoes)
/// - Smooth rolling objects
/// - Any object that's "cylinder-like" but needs smooth collision at ends
///
/// # Advantages Over Cylinders
///
/// - **No sharp edges**: Smoother collision response
/// - **Better for characters**: More natural movement and rotation
/// - **Simpler collision detection**: Easier to compute contacts than cylinders
///
/// # Example
///
/// ```rust
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::shape::Capsule;
/// use parry3d::math::Vector;
///
/// // Create a vertical capsule (aligned with Y axis)
/// // Half-height of 2.0 means the segment is 4.0 units long
/// let capsule = Capsule::new_y(2.0, 0.5);
/// assert_eq!(capsule.radius, 0.5);
/// assert_eq!(capsule.height(), 4.0);
///
/// // Create a custom capsule between two points
/// let a = Vector::ZERO;
/// let b = Vector::new(3.0, 4.0, 0.0);
/// let custom = Capsule::new(a, b, 1.0);
/// assert_eq!(custom.height(), 5.0); // Distance from a to b
/// # }
/// ```
pub struct Capsule {
    /// The line segment forming the capsule's central axis.
    ///
    /// The capsule extends from `segment.a` to `segment.b`, with hemispherical
    /// caps centered at each endpoint.
    pub segment: Segment,

    /// The radius of the capsule.
    ///
    /// This is the distance from the central axis to the surface. Must be positive.
    /// The total "thickness" of the capsule is `2 * radius`.
    pub radius: Real,
}

impl Capsule {
    /// Creates a new capsule aligned with the X axis.
    ///
    /// The capsule is centered at the origin and extends along the X axis.
    ///
    /// # Arguments
    ///
    /// * `half_height` - Half the length of the central segment (total length = `2 * half_height`)
    /// * `radius` - The radius of the capsule
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    ///
    /// // Create a capsule extending 6 units along X axis (3 units in each direction)
    /// // with radius 0.5
    /// let capsule = Capsule::new_x(3.0, 0.5);
    /// assert_eq!(capsule.height(), 6.0);
    /// assert_eq!(capsule.radius, 0.5);
    ///
    /// // The center is at the origin
    /// let center = capsule.center();
    /// assert!(center.length() < 1e-6);
    /// # }
    /// ```
    pub fn new_x(half_height: Real, radius: Real) -> Self {
        let b = Vector::X * half_height;
        Self::new(-b, b, radius)
    }

    /// Creates a new capsule aligned with the Y axis.
    ///
    /// The capsule is centered at the origin and extends along the Y axis.
    /// This is the most common orientation for character capsules (standing upright).
    ///
    /// # Arguments
    ///
    /// * `half_height` - Half the length of the central segment
    /// * `radius` - The radius of the capsule
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    ///
    /// // Create a typical character capsule: 2 units tall with 0.3 radius
    /// let character = Capsule::new_y(1.0, 0.3);
    /// assert_eq!(character.height(), 2.0);
    /// assert_eq!(character.radius, 0.3);
    ///
    /// // Total height including the spherical caps: 2.0 + 2 * 0.3 = 2.6
    /// # }
    /// ```
    pub fn new_y(half_height: Real, radius: Real) -> Self {
        let b = Vector::Y * half_height;
        Self::new(-b, b, radius)
    }

    /// Creates a new capsule aligned with the Z axis.
    ///
    /// The capsule is centered at the origin and extends along the Z axis.
    ///
    /// # Arguments
    ///
    /// * `half_height` - Half the length of the central segment
    /// * `radius` - The radius of the capsule
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    ///
    /// // Create a capsule for a torpedo extending along Z axis
    /// let torpedo = Capsule::new_z(5.0, 0.4);
    /// assert_eq!(torpedo.height(), 10.0);
    /// assert_eq!(torpedo.radius, 0.4);
    /// # }
    /// ```
    #[cfg(feature = "dim3")]
    pub fn new_z(half_height: Real, radius: Real) -> Self {
        let b = Vector::Z * half_height;
        Self::new(-b, b, radius)
    }

    /// Creates a new capsule with custom endpoints and radius.
    ///
    /// This is the most flexible constructor, allowing you to create a capsule
    /// with any orientation and position.
    ///
    /// # Arguments
    ///
    /// * `a` - The first endpoint of the central segment
    /// * `b` - The second endpoint of the central segment
    /// * `radius` - The radius of the capsule
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    /// use parry3d::math::Vector;
    ///
    /// // Create a diagonal capsule
    /// let a = Vector::ZERO;
    /// let b = Vector::new(3.0, 4.0, 0.0);
    /// let capsule = Capsule::new(a, b, 0.5);
    ///
    /// // Height is the distance between a and b
    /// assert_eq!(capsule.height(), 5.0); // 3-4-5 triangle
    ///
    /// // Center is the midpoint
    /// let center = capsule.center();
    /// assert_eq!(center, Vector::new(1.5, 2.0, 0.0));
    /// # }
    /// ```
    pub fn new(a: Vector, b: Vector, radius: Real) -> Self {
        let segment = Segment::new(a, b);
        Self { segment, radius }
    }

    /// Returns the length of the capsule's central segment.
    ///
    /// This is the distance between the two endpoints, **not** including the
    /// hemispherical caps. The total length of the capsule including caps is
    /// `height() + 2 * radius`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    ///
    /// let capsule = Capsule::new_y(3.0, 0.5);
    ///
    /// // Height of the central segment
    /// assert_eq!(capsule.height(), 6.0);
    ///
    /// // Total length including spherical caps
    /// let total_length = capsule.height() + 2.0 * capsule.radius;
    /// assert_eq!(total_length, 7.0);
    /// # }
    /// ```
    pub fn height(&self) -> Real {
        (self.segment.b - self.segment.a).length()
    }

    /// Returns half the length of the capsule's central segment.
    ///
    /// This is equivalent to `height() / 2.0`.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    ///
    /// let capsule = Capsule::new_y(3.0, 0.5);
    /// assert_eq!(capsule.half_height(), 3.0);
    /// assert_eq!(capsule.half_height(), capsule.height() / 2.0);
    /// # }
    /// ```
    pub fn half_height(&self) -> Real {
        self.height() / 2.0
    }

    /// Returns the center point of the capsule.
    ///
    /// This is the midpoint between the two endpoints of the central segment.
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    /// use parry3d::math::Vector;
    ///
    /// let a = Vector::new(-2.0, 0.0, 0.0);
    /// let b = Vector::new(4.0, 0.0, 0.0);
    /// let capsule = Capsule::new(a, b, 1.0);
    ///
    /// let center = capsule.center();
    /// assert_eq!(center, Vector::new(1.0, 0.0, 0.0));
    /// # }
    /// ```
    pub fn center(&self) -> Vector {
        self.segment.a.midpoint(self.segment.b)
    }

    /// Creates a new capsule equal to `self` with all its endpoints transformed by `pos`.
    ///
    /// This applies a rigid transformation (translation and rotation) to the capsule.
    ///
    /// # Arguments
    ///
    /// * `pos` - The isometry (rigid transformation) to apply
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::shape::Capsule;
    /// use parry3d::math::{Pose, Vector};
    ///
    /// let capsule = Capsule::new_y(1.0, 0.5);
    ///
    /// // Translate the capsule 5 units along X axis
    /// let transform = Pose::translation(5.0, 0.0, 0.0);
    /// let transformed = capsule.transform_by(&transform);
    ///
    /// // Center moved by 5 units
    /// assert_eq!(transformed.center().x, 5.0);
    /// // Radius unchanged
    /// assert_eq!(transformed.radius, 0.5);
    /// # }
    /// ```
    pub fn transform_by(&self, pos: &Pose) -> Self {
        Self::new(pos * self.segment.a, pos * self.segment.b, self.radius)
    }

    /// The transformation such that `t * Y` is collinear with `b - a` and `t * origin` equals
    /// the capsule's center.
    pub fn canonical_transform(&self) -> Pose {
        let tra = self.center();
        let rot = self.rotation_wrt_y();
        Pose::from_parts(tra, rot)
    }

    /// The rotation `r` such that `r * Y` is collinear with `b - a`.
    pub fn rotation_wrt_y(&self) -> Rotation {
        let mut dir = self.segment.b - self.segment.a;
        if dir.y < 0.0 {
            dir = -dir;
        }
        let dir = dir.normalize_or(Vector::Y);
        Rotation::from_rotation_arc(Vector::Y, dir)
    }

    /// The transform `t` such that `t * Y` is collinear with `b - a` and such that `t * origin = (b + a) / 2.0`.
    pub fn transform_wrt_y(&self) -> Pose {
        let rot = self.rotation_wrt_y();
        Pose::from_parts(self.center(), rot)
    }

    /// Computes a scaled version of this capsule.
    ///
    /// If the scaling factor is non-uniform, then it can’t be represented as
    /// capsule. Instead, a convex polygon approximation (with `nsubdivs`
    /// subdivisions) is returned. Returns `None` if that approximation had degenerate
    /// normals (for example if the scaling factor along one axis is zero).
    #[cfg(all(feature = "dim2", feature = "alloc"))]
    pub fn scaled(
        self,
        scale: Vector,
        nsubdivs: u32,
    ) -> Option<Either<Self, super::ConvexPolygon>> {
        if scale.x != scale.y {
            // The scaled shape is not a capsule.
            let mut vtx = self.to_polyline(nsubdivs);
            vtx.iter_mut().for_each(|pt| *pt *= scale);
            Some(Either::Right(super::ConvexPolygon::from_convex_polyline(
                vtx,
            )?))
        } else {
            let uniform_scale = scale.x;
            Some(Either::Left(Self::new(
                self.segment.a * uniform_scale,
                self.segment.b * uniform_scale,
                self.radius * uniform_scale.abs(),
            )))
        }
    }

    /// Computes a scaled version of this capsule.
    ///
    /// If the scaling factor is non-uniform, then it can’t be represented as
    /// capsule. Instead, a convex polygon approximation (with `nsubdivs`
    /// subdivisions) is returned. Returns `None` if that approximation had degenerate
    /// normals (for example if the scaling factor along one axis is zero).
    #[cfg(all(feature = "dim3", feature = "alloc"))]
    pub fn scaled(
        self,
        scale: Vector,
        nsubdivs: u32,
    ) -> Option<Either<Self, super::ConvexPolyhedron>> {
        if scale.x != scale.y || scale.x != scale.z || scale.y != scale.z {
            // The scaled shape is not a capsule.
            let (mut vtx, idx) = self.to_trimesh(nsubdivs, nsubdivs);
            vtx.iter_mut().for_each(|pt| *pt *= scale);
            Some(Either::Right(super::ConvexPolyhedron::from_convex_mesh(
                vtx, &idx,
            )?))
        } else {
            let uniform_scale = scale.x;
            Some(Either::Left(Self::new(
                self.segment.a * uniform_scale,
                self.segment.b * uniform_scale,
                self.radius * uniform_scale.abs(),
            )))
        }
    }
}

impl SupportMap for Capsule {
    fn local_support_point(&self, dir: Vector) -> Vector {
        let dir = dir.normalize_or(Vector::Y);
        self.local_support_point_toward(dir)
    }

    fn local_support_point_toward(&self, dir: Vector) -> Vector {
        if dir.dot(self.segment.a) > dir.dot(self.segment.b) {
            self.segment.a + dir * self.radius
        } else {
            self.segment.b + dir * self.radius
        }
    }
}