#[repr(C)]pub struct Ball {
pub radius: f32,
}Expand description
A ball shape, also known as a sphere in 3D or a circle in 2D.
A ball is one of the simplest shapes in collision detection, defined by a single parameter: its radius. The center of the ball is always at the origin of its local coordinate system.
§Properties
- In 2D: Represents a circle (all points at distance
radiusfrom the center) - In 3D: Represents a sphere (all points at distance
radiusfrom the center) - Convex: Yes, balls are always convex shapes
- Support mapping: Extremely efficient (constant time)
§Use Cases
Balls are ideal for:
- Projectiles (bullets, cannonballs)
- Spherical objects (planets, marbles, balls)
- Bounding volumes for fast collision detection
- Dynamic objects that need to roll
§Example
use parry3d::shape::Ball;
use nalgebra::Vector3;
// Create a ball with radius 2.0
let ball = Ball::new(2.0);
assert_eq!(ball.radius, 2.0);Fields§
§radius: f32The radius of the ball.
This must be a positive value. A radius of 0.0 is valid but represents a degenerate ball (a single point).
Implementations§
Source§impl Ball
impl Ball
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 ball, transformed by pos.
Sourcepub fn local_bounding_sphere(&self) -> BoundingSphere
pub fn local_bounding_sphere(&self) -> BoundingSphere
Computes the local-space Aabb of this ball.
Source§impl Ball
impl Ball
Sourcepub fn new(radius: f32) -> Ball
pub fn new(radius: f32) -> Ball
Creates a new ball with the given radius.
§Arguments
radius- The radius of the ball. Should be positive.
§Example
use parry3d::shape::Ball;
// Create a ball with radius 5.0
let ball = Ball::new(5.0);
assert_eq!(ball.radius, 5.0);
// You can also create very small balls
let tiny_ball = Ball::new(0.001);
assert_eq!(tiny_ball.radius, 0.001);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 ball.
Uniform scaling (same scale factor on all axes) produces another ball. Non-uniform scaling (different scale factors) produces an ellipsoid, which is approximated as a convex polyhedron.
§Arguments
scale- The scaling factors for each axis (x, y, z in 3D)nsubdivs- Number of subdivisions for polyhedron approximation when scaling is non-uniform
§Returns
Some(Either::Left(Ball))- If scaling is uniform, returns a scaled ballSome(Either::Right(ConvexPolyhedron))- If scaling is non-uniform, returns a polyhedron approximationNone- If the approximation failed (e.g., zero scaling on an axis)
§Example
use parry3d::shape::Ball;
use nalgebra::Vector3;
use either::Either;
let ball = Ball::new(5.0);
// Uniform scaling: produces another ball
let uniform_scale = Vector3::new(2.0, 2.0, 2.0);
if let Some(Either::Left(scaled_ball)) = ball.scaled(&uniform_scale, 10) {
assert_eq!(scaled_ball.radius, 10.0); // 5.0 * 2.0
}
// Non-uniform scaling: produces a polyhedron (ellipsoid approximation)
let non_uniform_scale = Vector3::new(2.0, 1.0, 1.5);
if let Some(Either::Right(polyhedron)) = ball.scaled(&non_uniform_scale, 10) {
// The polyhedron approximates an ellipsoid
assert!(polyhedron.points().len() > 0);
}Trait Implementations§
Source§impl PointQuery for Ball
impl PointQuery for Ball
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 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_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 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 Ball
impl RayCast for Ball
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 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 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 Ball
impl Shape for Ball
Source§fn feature_normal_at_point(
&self,
_: FeatureId,
point: &Point<f32>,
) -> Option<Unit<Vector<f32>>>
fn feature_normal_at_point( &self, _: FeatureId, point: &Point<f32>, ) -> Option<Unit<Vector<f32>>>
The shape’s normal at the given point located on a specific feature.
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
fn ccd_thickness(&self) -> f32
fn ccd_angular_thickness(&self) -> f32
Source§fn shape_type(&self) -> ShapeType
fn shape_type(&self) -> ShapeType
Source§fn as_typed_shape(&self) -> TypedShape<'_>
fn as_typed_shape(&self) -> TypedShape<'_>
Source§fn as_support_map(&self) -> Option<&dyn SupportMap>
fn as_support_map(&self) -> Option<&dyn SupportMap>
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§fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
Source§impl SupportMap for Ball
impl SupportMap for Ball
Source§fn support_point(&self, m: &Isometry<f32>, dir: &Vector<f32>) -> Point<f32>
fn support_point(&self, m: &Isometry<f32>, dir: &Vector<f32>) -> Point<f32>
transform. Read moreSource§fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
impl Copy for Ball
impl StructuralPartialEq for Ball
Auto Trait Implementations§
impl Freeze for Ball
impl RefUnwindSafe for Ball
impl Send for Ball
impl Sync for Ball
impl Unpin for Ball
impl UnwindSafe for Ball
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.