Struct bevy_xpbd_3d::components::Collider
source · pub struct Collider(_);Expand description
A collider used for collision detection.
By default, colliders generate collision events and cause a collision response for
rigid bodies. If you only want collision events, you can add a Sensor component.
Creation
Collider has tons of methods for creating colliders of various shapes.
For example, to add a ball collider to a rigid body, use Collider::ball:
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
fn setup(mut commands: Commands) {
commands.spawn((RigidBody::Dynamic, Collider::ball(0.5)));
}In addition, Bevy XPBD will automatically add some other components, like the following:
Collision layers
You can use collsion layers to configure which entities can collide with each other.
See CollisionLayers for more information and examples.
Collision events
There are currently three different collision events: Collision, CollisionStarted and CollisionEnded.
You can listen to these events as you normally would.
For example, you could read contacts like this:
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
fn my_system(mut collision_event_reader: EventReader<Collision>) {
for Collision(contact) in collision_event_reader.iter() {
println!("{:?} and {:?} are colliding", contact.entity1, contact.entity2);
}
}Advanced usage
Internally, Collider uses the shapes provided by parry. If you want to create a collider
using these shapes, you can simply use Collider::from(SharedShape::some_method()).
To get a reference to the internal SharedShape, you can use the get_shape method.
Implementations§
source§impl Collider
impl Collider
sourcepub fn get_shape(&self) -> &SharedShape
pub fn get_shape(&self) -> &SharedShape
Gets the raw shape of the collider. The shapes are provided by parry.
sourcepub fn compute_aabb(
&self,
position: Vector,
rotation: Quaternion
) -> ColliderAabb
pub fn compute_aabb( &self, position: Vector, rotation: Quaternion ) -> ColliderAabb
Computes the Axis-Aligned Bounding Box of the collider.
sourcepub fn compound(
shapes: Vec<(impl Into<Position>, impl Into<Rotation>, impl Into<Collider>)>
) -> Self
pub fn compound( shapes: Vec<(impl Into<Position>, impl Into<Rotation>, impl Into<Collider>)> ) -> Self
Creates a collider with a compound shape defined by a given vector of colliders with a position and a rotation.
Especially for dynamic rigid bodies, compound shape colliders should be preferred over triangle meshes and polylines, because convex shapes typically provide more reliable results.
If you want to create a compound shape from a 3D triangle mesh or 2D polyline, consider using the
Collider::convex_decomposition method.
sourcepub fn ball(radius: Scalar) -> Self
pub fn ball(radius: Scalar) -> Self
Creates a collider with a ball shape defined by its radius.
sourcepub fn cuboid(x_length: Scalar, y_length: Scalar, z_length: Scalar) -> Self
pub fn cuboid(x_length: Scalar, y_length: Scalar, z_length: Scalar) -> Self
Creates a collider with a cuboid shape defined by its extents.
sourcepub fn cylinder(height: Scalar, radius: Scalar) -> Self
pub fn cylinder(height: Scalar, radius: Scalar) -> Self
Creates a collider with a cylinder shape defined by its height along the Y axis and its radius on the XZ plane.
sourcepub fn cone(height: Scalar, radius: Scalar) -> Self
pub fn cone(height: Scalar, radius: Scalar) -> Self
Creates a collider with a cone shape defined by its height along the Y axis and the radius of its base on the XZ plane.
sourcepub fn capsule(height: Scalar, radius: Scalar) -> Self
pub fn capsule(height: Scalar, radius: Scalar) -> Self
Creates a collider with a capsule shape defined by its height along the Y axis and its radius.
sourcepub fn capsule_endpoints(a: Vector, b: Vector, radius: Scalar) -> Self
pub fn capsule_endpoints(a: Vector, b: Vector, radius: Scalar) -> Self
Creates a collider with a capsule shape defined by its end points a and b and its radius.
sourcepub fn halfspace(outward_normal: Vector) -> Self
pub fn halfspace(outward_normal: Vector) -> Self
Creates a collider with a half-space shape defined by the outward normal of its planar boundary.
sourcepub fn segment(a: Vector, b: Vector) -> Self
pub fn segment(a: Vector, b: Vector) -> Self
Creates a collider with a segment shape defined by its endpoints a and b.
sourcepub fn triangle(a: Vector, b: Vector, c: Vector) -> Self
pub fn triangle(a: Vector, b: Vector, c: Vector) -> Self
Creates a collider with a triangle shape defined by its points a, b and c.
sourcepub fn polyline(vertices: Vec<Vector>, indices: Option<Vec<[u32; 2]>>) -> Self
pub fn polyline(vertices: Vec<Vector>, indices: Option<Vec<[u32; 2]>>) -> Self
Creates a collider with a polyline shape defined by its vertices and optionally an index buffer.
sourcepub fn trimesh(vertices: Vec<Vector>, indices: Vec<[u32; 3]>) -> Self
pub fn trimesh(vertices: Vec<Vector>, indices: Vec<[u32; 3]>) -> Self
Creates a collider with a triangle mesh shape defined by its vertex and index buffers.
sourcepub fn trimesh_from_bevy_mesh(mesh: &Mesh) -> Option<Self>
pub fn trimesh_from_bevy_mesh(mesh: &Mesh) -> Option<Self>
Creates a collider with a triangle mesh shape built from a given Bevy Mesh.
sourcepub fn convex_decomposition_from_bevy_mesh(mesh: &Mesh) -> Option<Self>
pub fn convex_decomposition_from_bevy_mesh(mesh: &Mesh) -> Option<Self>
Creates a collider with a compound shape obtained from the decomposition of a triangle mesh
built from a given Bevy Mesh.
sourcepub fn convex_decomposition(
vertices: Vec<Vector>,
indices: Vec<[u32; 3]>
) -> Self
pub fn convex_decomposition( vertices: Vec<Vector>, indices: Vec<[u32; 3]> ) -> Self
Creates a collider shape with a compound shape obtained from the decomposition of a given trimesh defined by its vertex and index buffers.
sourcepub fn convex_hull(points: Vec<Vector>) -> Option<Self>
pub fn convex_hull(points: Vec<Vector>) -> Option<Self>
Creates a collider with a convex polyhedron shape obtained after computing the convex hull of the given points.
sourcepub fn heightfield(heights: Vec<Vec<Scalar>>, scale: Vector) -> Self
pub fn heightfield(heights: Vec<Vec<Scalar>>, scale: Vector) -> Self
Creates a collider with a heightfield shape.
A 3D heightfield is a rectangle on the XZ plane, subdivided in a grid pattern at regular intervals.
heights is a matrix indicating the altitude of each subdivision point. The number of rows indicates
the number of subdivisions along the X axis, while the number of columns indicates the number of
subdivisions along the Z axis.
scale indicates the size of each rectangle on the XZ plane.
Methods from Deref<Target = SharedShape>§
Trait Implementations§
source§fn from(original: SharedShape) -> Collider
fn from(original: SharedShape) -> Collider
Auto Trait Implementations§
impl !RefUnwindSafe for Collider
impl Send for Collider
impl Sync for Collider
impl Unpin for Collider
impl !UnwindSafe for Collider
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for Twhere
U: ShaderType,
&'a T: for<'a> Into<U>,
impl<T, U> AsBindGroupShaderType<U> for Twhere U: ShaderType, &'a T: for<'a> Into<U>,
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T [ShaderType] for self. When used in [AsBindGroup]
derives, it is safe to assume that all images in self exist.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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
source§fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<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>. Rc<Any> 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<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere C: Component,
fn get_components( self, func: &mut impl FnMut(StorageType, OwningPtr<'_, Aligned>) )
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self using data from the given [World]source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§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.