#[repr(C)]pub struct Cuboid {
pub half_extents: Vector<f32>,
}Expand description
A cuboid shape, also known as a box or rectangle.
A cuboid is defined by its half-extents, which are half the width, height (and depth in 3D) along each axis. The cuboid is always axis-aligned in its local coordinate system and centered at the origin.
§Properties
- In 2D: Represents a rectangle with dimensions
2 * half_extents.xby2 * half_extents.y - In 3D: Represents a box with dimensions
2 * half_extents.x/y/z - Convex: Yes, cuboids are always convex shapes
- Axis-aligned: In local space, yes (but can be rotated via transformation)
§Why Half-Extents?
Using half-extents instead of full dimensions makes many calculations simpler
and more efficient. For example, checking if a point is inside a cuboid becomes:
abs(point.x) <= half_extents.x && abs(point.y) <= half_extents.y
§Use Cases
Cuboids are ideal for:
- Boxes, crates, and containers
- Walls, floors, and platforms
- Simple collision bounds for complex objects
- AABB (Axis-Aligned Bounding Box) representations
§Example
use parry3d::shape::Cuboid;
use nalgebra::Vector3;
// Create a box that is 4 units wide, 2 units tall, and 6 units deep
// (half-extents are half of each dimension)
let cuboid = Cuboid::new(Vector3::new(2.0, 1.0, 3.0));
assert_eq!(cuboid.half_extents.x, 2.0);
assert_eq!(cuboid.half_extents.y, 1.0);
assert_eq!(cuboid.half_extents.z, 3.0);
// Full dimensions would be:
// width = 4.0, height = 2.0, depth = 6.0Fields§
§half_extents: Vector<f32>The half-extents of the cuboid along each axis.
Each component represents half the dimension along that axis:
half_extents.x: Half the widthhalf_extents.y: Half the heighthalf_extents.z: Half the depth (3D only)
All components should be positive.
Implementations§
Source§impl Cuboid
impl Cuboid
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 cuboid, transformed by pos.
Sourcepub fn local_bounding_sphere(&self) -> BoundingSphere
pub fn local_bounding_sphere(&self) -> BoundingSphere
Computes the local-space bounding sphere of this cuboid.
Source§impl Cuboid
impl Cuboid
Sourcepub fn new(half_extents: Vector<f32>) -> Cuboid
pub fn new(half_extents: Vector<f32>) -> Cuboid
Creates a new cuboid from its half-extents.
Half-extents represent half the width along each axis. To create a cuboid with full dimensions (width, height, depth), divide each by 2.
§Arguments
half_extents- Half the dimensions along each axis. All components should be positive.
§Example
use parry3d::shape::Cuboid;
use nalgebra::Vector3;
// Create a 10x6x4 box (full dimensions)
let cuboid = Cuboid::new(Vector3::new(5.0, 3.0, 2.0));
// Verify the half-extents
assert_eq!(cuboid.half_extents.x, 5.0);
assert_eq!(cuboid.half_extents.y, 3.0);
assert_eq!(cuboid.half_extents.z, 2.0);// In 2D:
use parry2d::shape::Cuboid;
use nalgebra::Vector2;
// Create a 20x10 rectangle
let rect = Cuboid::new(Vector2::new(10.0, 5.0));
assert_eq!(rect.half_extents.x, 10.0);
assert_eq!(rect.half_extents.y, 5.0);Sourcepub fn scaled(self, scale: &Vector<f32>) -> Self
pub fn scaled(self, scale: &Vector<f32>) -> Self
Computes a scaled version of this cuboid.
Each dimension is multiplied by the corresponding component of the scale vector.
Unlike balls, cuboids can be scaled non-uniformly (different scale factors per axis)
and still remain valid cuboids.
§Arguments
scale- The scaling factors for each axis
§Returns
A new cuboid with scaled dimensions
§Example
use parry3d::shape::Cuboid;
use nalgebra::Vector3;
let cuboid = Cuboid::new(Vector3::new(1.0, 2.0, 3.0));
// Uniform scaling: double all dimensions
let scaled_uniform = cuboid.scaled(&Vector3::new(2.0, 2.0, 2.0));
assert_eq!(scaled_uniform.half_extents, Vector3::new(2.0, 4.0, 6.0));
// Non-uniform scaling: different scale per axis
let scaled_non_uniform = cuboid.scaled(&Vector3::new(2.0, 1.0, 0.5));
assert_eq!(scaled_non_uniform.half_extents, Vector3::new(2.0, 2.0, 1.5));Sourcepub fn support_feature(&self, local_dir: Vector<f32>) -> PolygonalFeature
pub fn support_feature(&self, local_dir: Vector<f32>) -> PolygonalFeature
Return the face of this cuboid with a normal that maximizes
the dot product with local_dir.
Sourcepub fn local_support_edge_segment(&self, local_dir: Vector<f32>) -> Segment
pub fn local_support_edge_segment(&self, local_dir: Vector<f32>) -> Segment
Return the edge segment of this cuboid with a normal cone containing
a direction that that maximizes the dot product with local_dir.
Sourcepub fn support_face(&self, local_dir: Vector<f32>) -> PolygonalFeature
pub fn support_face(&self, local_dir: Vector<f32>) -> PolygonalFeature
Computes the face with a normal that maximizes the dot-product with local_dir.
Trait Implementations§
Source§impl PointQuery for Cuboid
impl PointQuery for Cuboid
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 PolygonalFeatureMap for Cuboid
impl PolygonalFeatureMap for Cuboid
Source§fn local_support_feature(
&self,
dir: &Unit<Vector<f32>>,
out_feature: &mut PolygonalFeature,
)
fn local_support_feature( &self, dir: &Unit<Vector<f32>>, out_feature: &mut PolygonalFeature, )
self towards the dir.Source§fn is_convex_polyhedron(&self) -> bool
fn is_convex_polyhedron(&self) -> bool
ConvexPolyhedron?Source§impl RayCast for Cuboid
impl RayCast for Cuboid
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 Cuboid
impl Shape for Cuboid
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 feature_normal_at_point(
&self,
feature: FeatureId,
_point: &Point<f32>,
) -> Option<Unit<Vector<f32>>>
fn feature_normal_at_point( &self, feature: FeatureId, _point: &Point<f32>, ) -> Option<Unit<Vector<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 Cuboid
impl SupportMap for Cuboid
Source§fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
impl Copy for Cuboid
impl StructuralPartialEq for Cuboid
Auto Trait Implementations§
impl Freeze for Cuboid
impl RefUnwindSafe for Cuboid
impl Send for Cuboid
impl Sync for Cuboid
impl Unpin for Cuboid
impl UnwindSafe for Cuboid
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.