#[repr(C)]pub struct Capsule {
pub segment: Segment,
pub radius: f32,
}Expand description
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
ato pointb - 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
use parry3d::shape::Capsule;
use nalgebra::Point3;
// 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 = Point3::origin();
let b = Point3::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 bFields§
§segment: SegmentThe line segment forming the capsule’s central axis.
The capsule extends from segment.a to segment.b, with hemispherical
caps centered at each endpoint.
radius: f32The 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.
Implementations§
Source§impl Capsule
impl Capsule
Sourcepub fn bounding_sphere(&self, pos: &Isometry<f32>) -> BoundingSphere
pub fn bounding_sphere(&self, pos: &Isometry<f32>) -> BoundingSphere
Computes the world-space bounding sphere of this capsule, transformed by pos.
Sourcepub fn local_bounding_sphere(&self) -> BoundingSphere
pub fn local_bounding_sphere(&self) -> BoundingSphere
Computes the world-space bounding sphere of this capsule.
Source§impl Capsule
impl Capsule
Sourcepub fn new_x(half_height: f32, radius: f32) -> Self
pub fn new_x(half_height: f32, radius: f32) -> Self
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
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.coords.norm() < 1e-6);Sourcepub fn new_y(half_height: f32, radius: f32) -> Self
pub fn new_y(half_height: f32, radius: f32) -> Self
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 segmentradius- The radius of the capsule
§Example
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.6Sourcepub fn new_z(half_height: f32, radius: f32) -> Self
pub fn new_z(half_height: f32, radius: f32) -> Self
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 segmentradius- The radius of the capsule
§Example
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);Sourcepub fn new(a: Point<f32>, b: Point<f32>, radius: f32) -> Self
pub fn new(a: Point<f32>, b: Point<f32>, radius: f32) -> Self
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 segmentb- The second endpoint of the central segmentradius- The radius of the capsule
§Example
use parry3d::shape::Capsule;
use nalgebra::Point3;
// Create a diagonal capsule
let a = Point3::origin();
let b = Point3::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, Point3::new(1.5, 2.0, 0.0));Sourcepub fn height(&self) -> f32
pub fn height(&self) -> f32
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
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);Sourcepub fn half_height(&self) -> f32
pub fn half_height(&self) -> f32
Returns half the length of the capsule’s central segment.
This is equivalent to height() / 2.0.
§Example
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);Sourcepub fn center(&self) -> Point<f32>
pub fn center(&self) -> Point<f32>
Returns the center point of the capsule.
This is the midpoint between the two endpoints of the central segment.
§Example
use parry3d::shape::Capsule;
use nalgebra::Point3;
let a = Point3::new(-2.0, 0.0, 0.0);
let b = Point3::new(4.0, 0.0, 0.0);
let capsule = Capsule::new(a, b, 1.0);
let center = capsule.center();
assert_eq!(center, Point3::new(1.0, 0.0, 0.0));Sourcepub fn transform_by(&self, pos: &Isometry<f32>) -> Self
pub fn transform_by(&self, pos: &Isometry<f32>) -> Self
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
use parry3d::shape::Capsule;
use nalgebra::{Isometry3, Vector3};
let capsule = Capsule::new_y(1.0, 0.5);
// Translate the capsule 5 units along X axis
let transform = Isometry3::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);Sourcepub fn canonical_transform(&self) -> Isometry<f32>
pub fn canonical_transform(&self) -> Isometry<f32>
The transformation such that t * Y is collinear with b - a and t * origin equals
the capsule’s center.
Sourcepub fn rotation_wrt_y(&self) -> Rotation<f32>
pub fn rotation_wrt_y(&self) -> Rotation<f32>
The rotation r such that r * Y is collinear with b - a.
Sourcepub fn transform_wrt_y(&self) -> Isometry<f32>
pub fn transform_wrt_y(&self) -> Isometry<f32>
The transform t such that t * Y is collinear with b - a and such that t * origin = (b + a) / 2.0.
Sourcepub fn scaled(
self,
scale: &Vector<f32>,
nsubdivs: u32,
) -> Option<Either<Self, ConvexPolyhedron>>
pub fn scaled( self, scale: &Vector<f32>, nsubdivs: u32, ) -> Option<Either<Self, ConvexPolyhedron>>
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).
Trait Implementations§
Source§impl PointQuery for Capsule
impl PointQuery for Capsule
Source§fn project_local_point(&self, pt: &Point<f32>, solid: bool) -> PointProjection
fn project_local_point(&self, pt: &Point<f32>, solid: bool) -> PointProjection
self. Read moreSource§fn project_local_point_and_get_feature(
&self,
pt: &Point<f32>,
) -> (PointProjection, FeatureId)
fn project_local_point_and_get_feature( &self, pt: &Point<f32>, ) -> (PointProjection, FeatureId)
self and returns the id of the
feature the point was projected on.Source§fn project_local_point_with_max_dist(
&self,
pt: &Point<f32>,
solid: bool,
max_dist: f32,
) -> Option<PointProjection>
fn project_local_point_with_max_dist( &self, pt: &Point<f32>, solid: bool, max_dist: f32, ) -> Option<PointProjection>
Source§fn project_point_with_max_dist(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
max_dist: f32,
) -> Option<PointProjection>
fn project_point_with_max_dist( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, max_dist: f32, ) -> Option<PointProjection>
self transformed by m, unless the projection lies further than the given max distance.Source§fn distance_to_local_point(&self, pt: &Point<f32>, solid: bool) -> f32
fn distance_to_local_point(&self, pt: &Point<f32>, solid: bool) -> f32
self.Source§fn contains_local_point(&self, pt: &Point<f32>) -> bool
fn contains_local_point(&self, pt: &Point<f32>) -> bool
self.Source§fn project_point(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> PointProjection
fn project_point( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, ) -> PointProjection
self transformed by m.Source§fn distance_to_point(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> f32
fn distance_to_point( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, ) -> f32
self transformed by m.Source§fn project_point_and_get_feature(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
) -> (PointProjection, FeatureId)
fn project_point_and_get_feature( &self, m: &Isometry<f32>, pt: &Point<f32>, ) -> (PointProjection, FeatureId)
self transformed by m and returns the id of the
feature the point was projected on.Source§impl RayCast for Capsule
impl RayCast for Capsule
Source§fn cast_local_ray_and_get_normal(
&self,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<RayIntersection>
fn cast_local_ray_and_get_normal( &self, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<RayIntersection>
Source§fn cast_local_ray(
&self,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<f32>
fn cast_local_ray( &self, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<f32>
Source§fn intersects_local_ray(&self, ray: &Ray, max_time_of_impact: f32) -> bool
fn intersects_local_ray(&self, ray: &Ray, max_time_of_impact: f32) -> bool
Source§fn cast_ray(
&self,
m: &Isometry<f32>,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<f32>
fn cast_ray( &self, m: &Isometry<f32>, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<f32>
Source§fn cast_ray_and_get_normal(
&self,
m: &Isometry<f32>,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<RayIntersection>
fn cast_ray_and_get_normal( &self, m: &Isometry<f32>, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<RayIntersection>
Source§impl Shape for Capsule
impl Shape for Capsule
Source§fn scale_dyn(
&self,
scale: &Vector<f32>,
num_subdivisions: u32,
) -> Option<Box<dyn Shape>>
fn scale_dyn( &self, scale: &Vector<f32>, num_subdivisions: u32, ) -> Option<Box<dyn Shape>>
scale into a boxed trait-object. Read moreSource§fn compute_local_aabb(&self) -> Aabb
fn compute_local_aabb(&self) -> Aabb
Aabb of this shape.Source§fn compute_local_bounding_sphere(&self) -> BoundingSphere
fn compute_local_bounding_sphere(&self) -> BoundingSphere
Source§fn compute_aabb(&self, position: &Isometry<f32>) -> Aabb
fn compute_aabb(&self, position: &Isometry<f32>) -> Aabb
Aabb of this shape with the given position.Source§fn mass_properties(&self, density: f32) -> MassProperties
fn mass_properties(&self, density: f32) -> MassProperties
Source§fn shape_type(&self) -> ShapeType
fn shape_type(&self) -> ShapeType
Source§fn as_typed_shape(&self) -> TypedShape<'_>
fn as_typed_shape(&self) -> TypedShape<'_>
fn ccd_thickness(&self) -> f32
fn ccd_angular_thickness(&self) -> f32
Source§fn as_support_map(&self) -> Option<&dyn SupportMap>
fn as_support_map(&self) -> Option<&dyn SupportMap>
Source§fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
Source§fn clone_box(&self) -> Box<dyn Shape>
fn clone_box(&self) -> Box<dyn Shape>
clone_dynSource§fn compute_bounding_sphere(&self, position: &Isometry<f32>) -> BoundingSphere
fn compute_bounding_sphere(&self, position: &Isometry<f32>) -> BoundingSphere
fn as_composite_shape(&self) -> Option<&dyn CompositeShape>
Source§impl SupportMap for Capsule
impl SupportMap for Capsule
Source§fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
impl Copy for Capsule
Auto Trait Implementations§
impl Freeze for Capsule
impl RefUnwindSafe for Capsule
impl Send for Capsule
impl Sync for Capsule
impl Unpin for Capsule
impl UnwindSafe for Capsule
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.