pub struct SharedShape(pub Arc<dyn Shape>);Expand description
A reference-counted, shareable geometric shape.
SharedShape is a wrapper around Arc<dyn Shape> that allows multiple parts of your
code to share ownership of the same shape without copying it. This is particularly useful
when the same geometric shape is used by multiple colliders or when you want to avoid
the memory overhead of duplicating complex shapes like triangle meshes.
§Why use SharedShape?
- Memory efficiency: Share expensive shapes (like
TriMeshorHeightField) across multiple colliders - Performance: Cloning a
SharedShapeonly increments a reference count, not the shape data - Type erasure: Store different shape types uniformly via the
Shapetrait - Flexibility: Can be converted to a unique instance via
make_mutwhen needed
§When to use SharedShape?
Use SharedShape when:
- You need multiple colliders with the same geometry
- You’re working with large, complex shapes (meshes, compounds, heightfields)
- You want to store heterogeneous shape types in a collection
Use concrete shape types directly when:
- You have a single, simple shape that won’t be shared
- You need direct access to shape-specific methods
§Examples
Creating and sharing a ball shape:
// Create a ball with radius 1.0
let shape = SharedShape::ball(1.0);
// Clone it cheaply - only the Arc is cloned, not the shape data
let shape_clone = shape.clone();
// Both shapes reference the same underlying ball
assert_eq!(shape.as_ball().unwrap().radius, 1.0);
assert_eq!(shape_clone.as_ball().unwrap().radius, 1.0);Tuple Fields§
§0: Arc<dyn Shape>Implementations§
Sourcepub fn make_mut(&mut self) -> &mut dyn Shape
pub fn make_mut(&mut self) -> &mut dyn Shape
Returns a mutable reference to the underlying shape, cloning it if necessary.
This method implements copy-on-write semantics. If the Arc has multiple references,
it will clone the shape data to create a unique instance. Otherwise, no cloning occurs.
§Examples
let mut shape1 = SharedShape::ball(1.0);
let shape2 = shape1.clone(); // Shares the same Arc
// This will clone the shape because it's shared
let ball = shape1.make_mut().as_ball_mut().unwrap();
ball.radius = 2.0;
// shape1 has been modified, shape2 still has the original value
assert_eq!(shape1.as_ball().unwrap().radius, 2.0);
assert_eq!(shape2.as_ball().unwrap().radius, 1.0);Sourcepub fn compound(shapes: Vec<(Isometry<f32>, SharedShape)>) -> Self
pub fn compound(shapes: Vec<(Isometry<f32>, SharedShape)>) -> Self
Creates a compound shape made of multiple subshapes.
Each subshape has its own position and orientation relative to the compound’s origin.
§Examples
let ball1 = SharedShape::ball(0.5);
let ball2 = SharedShape::ball(0.5);
#[cfg(feature = "dim3")]
let compound = SharedShape::compound(vec![
(Isometry::translation(1.0, 0.0, 0.0), ball1),
(Isometry::translation(-1.0, 0.0, 0.0), ball2),
]);
#[cfg(feature = "dim2")]
let compound = SharedShape::compound(vec![
(Isometry::translation(1.0, 0.0), ball1),
(Isometry::translation(-1.0, 0.0), ball2),
]);Sourcepub fn ball(radius: f32) -> Self
pub fn ball(radius: f32) -> Self
Creates a ball (sphere in 3D, circle in 2D) with the specified radius.
A ball is the simplest and most efficient collision shape.
§Examples
let ball = SharedShape::ball(1.0);
assert_eq!(ball.as_ball().unwrap().radius, 1.0);Sourcepub fn halfspace(outward_normal: Unit<Vector<f32>>) -> Self
pub fn halfspace(outward_normal: Unit<Vector<f32>>) -> Self
Initialize a plane shape defined by its outward normal.
Sourcepub fn round_cuboid(hx: f32, hy: f32, border_radius: f32) -> Self
pub fn round_cuboid(hx: f32, hy: f32, border_radius: f32) -> Self
Initialize a round cuboid shape defined by its half-extents and border radius.
Sourcepub fn capsule(a: Point<f32>, b: Point<f32>, radius: f32) -> Self
pub fn capsule(a: Point<f32>, b: Point<f32>, radius: f32) -> Self
Initialize a capsule shape from its endpoints and radius.
Sourcepub fn capsule_x(half_height: f32, radius: f32) -> Self
pub fn capsule_x(half_height: f32, radius: f32) -> Self
Initialize a capsule shape aligned with the x axis.
Sourcepub fn capsule_y(half_height: f32, radius: f32) -> Self
pub fn capsule_y(half_height: f32, radius: f32) -> Self
Creates a capsule aligned with the Y axis.
A capsule is a line segment with rounded ends, commonly used for character controllers.
§Examples
// Create a character capsule: 1.8 units tall with 0.3 radius
let character = SharedShape::capsule_y(0.9, 0.3);Sourcepub fn segment(a: Point<f32>, b: Point<f32>) -> Self
pub fn segment(a: Point<f32>, b: Point<f32>) -> Self
Initialize a segment shape from its endpoints.
Sourcepub fn triangle(a: Point<f32>, b: Point<f32>, c: Point<f32>) -> Self
pub fn triangle(a: Point<f32>, b: Point<f32>, c: Point<f32>) -> Self
Initializes a triangle shape.
Sourcepub fn round_triangle(
a: Point<f32>,
b: Point<f32>,
c: Point<f32>,
border_radius: f32,
) -> Self
pub fn round_triangle( a: Point<f32>, b: Point<f32>, c: Point<f32>, border_radius: f32, ) -> Self
Initializes a triangle shape with round corners.
Sourcepub fn polyline(
vertices: Vec<Point<f32>>,
indices: Option<Vec<[u32; 2]>>,
) -> Self
pub fn polyline( vertices: Vec<Point<f32>>, indices: Option<Vec<[u32; 2]>>, ) -> Self
Initializes a polyline shape defined by its vertex and index buffers.
If no index buffer is provided, the polyline is assumed to describe a line strip.
Sourcepub fn trimesh(
vertices: Vec<Point<f32>>,
indices: Vec<[u32; 3]>,
) -> Result<Self, TriMeshBuilderError>
pub fn trimesh( vertices: Vec<Point<f32>>, indices: Vec<[u32; 3]>, ) -> Result<Self, TriMeshBuilderError>
Creates a triangle mesh shape from vertices and triangle indices.
A triangle mesh represents a complex surface as a collection of triangles, useful for terrain, static geometry, or any shape that can’t be represented by simpler primitives.
§Returns
Returns Ok(SharedShape) if the mesh is valid, or an error if indices are out of bounds.
§Examples
#[cfg(feature = "dim3")]
let vertices = vec![
Point::new(0.0, 0.0, 0.0),
Point::new(1.0, 0.0, 0.0),
Point::new(0.0, 1.0, 0.0),
];
#[cfg(feature = "dim3")]
let indices = vec![[0, 1, 2]];
#[cfg(feature = "dim3")]
let mesh = SharedShape::trimesh(vertices, indices).unwrap();Sourcepub fn trimesh_with_flags(
vertices: Vec<Point<f32>>,
indices: Vec<[u32; 3]>,
flags: TriMeshFlags,
) -> Result<Self, TriMeshBuilderError>
pub fn trimesh_with_flags( vertices: Vec<Point<f32>>, indices: Vec<[u32; 3]>, flags: TriMeshFlags, ) -> Result<Self, TriMeshBuilderError>
Initializes a triangle mesh shape defined by its vertex and index buffers and pre-processing flags.
Sourcepub fn voxels(voxel_size: Vector<f32>, grid_coords: &[Point<i32>]) -> Self
pub fn voxels(voxel_size: Vector<f32>, grid_coords: &[Point<i32>]) -> Self
Initializes a shape made of voxels.
Each voxel has the size voxel_size and grid coordinate given by grid_coords.
The primitive_geometry controls the behavior of collision detection at voxels boundaries.
For initializing a voxels shape from points in space, see Self::voxels_from_points.
For initializing a voxels shape from a mesh to voxelize, see Self::voxelized_mesh.
For initializing multiple voxels shape from the convex decomposition of a mesh, see
Self::voxelized_convex_decomposition.
Sourcepub fn voxels_from_points(
voxel_size: Vector<f32>,
points: &[Point<f32>],
) -> Self
pub fn voxels_from_points( voxel_size: Vector<f32>, points: &[Point<f32>], ) -> Self
Initializes a shape made of voxels.
Each voxel has the size voxel_size and contains at least one point from centers.
The primitive_geometry controls the behavior of collision detection at voxels boundaries.
Sourcepub fn voxelized_mesh(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
voxel_size: f32,
fill_mode: FillMode,
) -> Self
pub fn voxelized_mesh( vertices: &[Point<f32>], indices: &[[u32; 2]], voxel_size: f32, fill_mode: FillMode, ) -> Self
Initializes a voxels shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into voxelized convex parts.
Sourcepub fn voxelized_convex_decomposition(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
) -> Vec<Self>
pub fn voxelized_convex_decomposition( vertices: &[Point<f32>], indices: &[[u32; 2]], ) -> Vec<Self>
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into voxelized convex parts.
Sourcepub fn voxelized_convex_decomposition_with_params(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
params: &VHACDParameters,
) -> Vec<Self>
pub fn voxelized_convex_decomposition_with_params( vertices: &[Point<f32>], indices: &[[u32; 2]], params: &VHACDParameters, ) -> Vec<Self>
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into voxelized convex parts.
Sourcepub fn convex_decomposition(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
) -> Self
pub fn convex_decomposition( vertices: &[Point<f32>], indices: &[[u32; 2]], ) -> Self
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into convex parts.
Sourcepub fn round_convex_decomposition(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
border_radius: f32,
) -> Self
pub fn round_convex_decomposition( vertices: &[Point<f32>], indices: &[[u32; 2]], border_radius: f32, ) -> Self
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into convex parts dilated with round corners.
Sourcepub fn convex_decomposition_with_params(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
params: &VHACDParameters,
) -> Self
pub fn convex_decomposition_with_params( vertices: &[Point<f32>], indices: &[[u32; 2]], params: &VHACDParameters, ) -> Self
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into convex parts.
Sourcepub fn round_convex_decomposition_with_params(
vertices: &[Point<f32>],
indices: &[[u32; 2]],
params: &VHACDParameters,
border_radius: f32,
) -> Self
pub fn round_convex_decomposition_with_params( vertices: &[Point<f32>], indices: &[[u32; 2]], params: &VHACDParameters, border_radius: f32, ) -> Self
Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or polyline (in 2D) into convex parts dilated with round corners.
Sourcepub fn convex_hull(points: &[Point<f32>]) -> Option<Self>
pub fn convex_hull(points: &[Point<f32>]) -> Option<Self>
Creates a new shared shape that is the convex-hull of the given points.
Sourcepub fn convex_polyline(points: Vec<Point<f32>>) -> Option<Self>
pub fn convex_polyline(points: Vec<Point<f32>>) -> Option<Self>
Creates a new shared shape that is a 2D convex polygon from a set of points assumed to describe a counter-clockwise convex polyline.
This does not compute the convex-hull of the input points: convexity of the input is
assumed and not checked. For a version that calculates the convex hull of the input points,
use SharedShape::convex_hull instead.
The generated ConvexPolygon will contain the given points with any point
collinear to the previous and next ones removed. For a version that leaves the input
points unmodified, use SharedShape::convex_polyline_unmodified.
Returns None if all points form an almost flat line.
Sourcepub fn convex_polyline_unmodified(points: Vec<Point<f32>>) -> Option<Self>
pub fn convex_polyline_unmodified(points: Vec<Point<f32>>) -> Option<Self>
Creates a new shared shape that is a 2D convex polygon from a set of points assumed to describe a counter-clockwise convex polyline.
This is the same as SharedShape::convex_polyline but without removing any point
from the input even if some are coplanar.
Returns None if points doesn’t contain at least three points.
Sourcepub fn round_convex_hull(
points: &[Point<f32>],
border_radius: f32,
) -> Option<Self>
pub fn round_convex_hull( points: &[Point<f32>], border_radius: f32, ) -> Option<Self>
Creates a new shared shape with rounded corners that is the
convex-hull of the given points, dilated by border_radius.
Methods from Deref<Target = dyn Shape>§
Sourcepub fn is<__T: Shape>(&self) -> bool
pub fn is<__T: Shape>(&self) -> bool
Returns true if the trait object wraps an object of type __T.
Sourcepub fn downcast_rc<__T: Shape>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>
pub fn downcast_rc<__T: Shape>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>
Returns an Rc-ed object from an Rc-ed trait object if the underlying object is of
type __T. Returns the original Rc-ed trait if it isn’t.
Sourcepub fn downcast_ref<__T: Shape>(&self) -> Option<&__T>
pub fn downcast_ref<__T: Shape>(&self) -> Option<&__T>
Returns a reference to the object within the trait object if it is of type __T, or
None if it isn’t.
Sourcepub fn downcast_arc<__T: Shape + Any + Send + Sync>(
self: Arc<Self>,
) -> Result<Arc<__T>, Arc<Self>>
pub fn downcast_arc<__T: Shape + Any + Send + Sync>( self: Arc<Self>, ) -> Result<Arc<__T>, Arc<Self>>
Returns an Arc-ed object from an Arc-ed trait object if the underlying object is of
type __T. Returns the original Arc-ed trait if it isn’t.
Sourcepub fn as_shape<T: Shape>(&self) -> Option<&T>
pub fn as_shape<T: Shape>(&self) -> Option<&T>
Converts this abstract shape to the given shape, if it is one.
Sourcepub fn as_cuboid(&self) -> Option<&Cuboid>
pub fn as_cuboid(&self) -> Option<&Cuboid>
Converts this abstract shape to a cuboid, if it is one.
Sourcepub fn as_halfspace(&self) -> Option<&HalfSpace>
pub fn as_halfspace(&self) -> Option<&HalfSpace>
Converts this abstract shape to a halfspace, if it is one.
Sourcepub fn as_segment(&self) -> Option<&Segment>
pub fn as_segment(&self) -> Option<&Segment>
Converts this abstract shape to a segment, if it is one.
Sourcepub fn as_capsule(&self) -> Option<&Capsule>
pub fn as_capsule(&self) -> Option<&Capsule>
Converts this abstract shape to a capsule, if it is one.
Sourcepub fn as_triangle(&self) -> Option<&Triangle>
pub fn as_triangle(&self) -> Option<&Triangle>
Converts this abstract shape to a triangle, if it is one.
Sourcepub fn as_voxels(&self) -> Option<&Voxels>
pub fn as_voxels(&self) -> Option<&Voxels>
Converts this abstract shape to voxels, if it is one.
Sourcepub fn as_compound(&self) -> Option<&Compound>
pub fn as_compound(&self) -> Option<&Compound>
Converts this abstract shape to a compound shape, if it is one.
Sourcepub fn as_trimesh(&self) -> Option<&TriMesh>
pub fn as_trimesh(&self) -> Option<&TriMesh>
Converts this abstract shape to a triangle mesh, if it is one.
Sourcepub fn as_polyline(&self) -> Option<&Polyline>
pub fn as_polyline(&self) -> Option<&Polyline>
Converts this abstract shape to a polyline, if it is one.
Sourcepub fn as_heightfield(&self) -> Option<&HeightField>
pub fn as_heightfield(&self) -> Option<&HeightField>
Converts this abstract shape to a heightfield, if it is one.
Sourcepub fn as_round_cuboid(&self) -> Option<&RoundCuboid>
pub fn as_round_cuboid(&self) -> Option<&RoundCuboid>
Converts this abstract shape to a round cuboid, if it is one.
Sourcepub fn as_round_triangle(&self) -> Option<&RoundTriangle>
pub fn as_round_triangle(&self) -> Option<&RoundTriangle>
Converts this abstract shape to a round triangle, if it is one.
Sourcepub fn as_convex_polygon(&self) -> Option<&ConvexPolygon>
pub fn as_convex_polygon(&self) -> Option<&ConvexPolygon>
Converts this abstract shape to a convex polygon, if it is one.
Sourcepub fn as_round_convex_polygon(&self) -> Option<&RoundConvexPolygon>
pub fn as_round_convex_polygon(&self) -> Option<&RoundConvexPolygon>
Converts this abstract shape to a round convex polygon, if it is one.
Trait Implementations§
Source§fn clone(&self) -> SharedShape
fn clone(&self) -> SharedShape
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
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.