1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
use std::fmt;
use crate::{prelude::*, utils::make_isometry};
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
use bevy::render::mesh::{Indices, VertexAttributeValues};
#[cfg(all(feature = "3d", feature = "async-collider"))]
use bevy::utils::HashMap;
use bevy::{log, prelude::*, utils::HashSet};
use collision::contact_query::UnsupportedShape;
use itertools::Either;
use parry::{
bounding_volume::Aabb,
shape::{RoundShape, SharedShape, TypedShape},
};
/// Parameters controlling the VHACD convex decomposition algorithm.
///
/// See <https://github.com/Unity-Technologies/VHACD#parameters> for details.
pub type VHACDParameters = parry::transformation::vhacd::VHACDParameters;
/// Flags used for the preprocessing of a triangle mesh collider.
pub type TriMeshFlags = parry::shape::TriMeshFlags;
/// A collider used for detecting collisions and generating contacts.
///
/// ## Creation
///
/// `Collider` has tons of methods for creating colliders of various shapes:
///
/// ```
/// # use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "# use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "# use bevy_xpbd_3d::prelude::*;")]
/// #
/// # fn setup(mut commands: Commands) {
/// // Create a ball collider with a given radius
/// commands.spawn(Collider::ball(0.5));
/// // Create a capsule collider with a given height and radius
/// commands.spawn(Collider::capsule(2.0, 0.5));
/// # }
/// ```
///
/// Colliders on their own only detect contacts and generate
/// [collision events](ContactReportingPlugin#collision-events).
/// To make colliders apply contact forces, they have to be attached
/// to [rigid bodies](RigidBody):
///
/// ```
/// use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use bevy_xpbd_3d::prelude::*;")]
///
/// // Spawn a dynamic body that falls onto a static platform
/// fn setup(mut commands: Commands) {
/// commands.spawn((
/// RigidBody::Dynamic,
/// Collider::ball(0.5),
/// Transform::from_xyz(0.0, 2.0, 0.0),
/// ));
#[cfg_attr(
feature = "2d",
doc = " commands.spawn((RigidBody::Static, Collider::cuboid(5.0, 0.5)));"
)]
#[cfg_attr(
feature = "3d",
doc = " commands.spawn((RigidBody::Static, Collider::cuboid(5.0, 0.5, 5.0)));"
)]
/// }
/// ```
///
/// Colliders can be further configured using various components like [`Friction`], [`Restitution`],
/// [`Sensor`], and [`CollisionLayers`].
///
/// In addition, Bevy XPBD automatically adds some other components for colliders, like the following:
///
/// - [`ColliderParent`]
/// - [`ColliderAabb`]
/// - [`CollidingEntities`]
/// - [`ColliderDensity`]
/// - [`ColliderMassProperties`]
///
#[cfg_attr(
feature = "3d",
doc = "Colliders can also be generated automatically from meshes and scenes. See [`AsyncCollider`] and [`AsyncSceneCollider`]."
)]
///
/// ### Multiple colliders
///
/// It can often be useful to attach multiple colliders to the same rigid body.
///
/// This can be done in two ways. Either use [`Collider::compound`] to have one collider that consists of many
/// shapes, or for more control, spawn several collider entities as the children of a rigid body:
///
/// ```
/// use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use bevy_xpbd_3d::prelude::*;")]
///
/// fn setup(mut commands: Commands) {
/// // Spawn a rigid body with one collider on the same entity and two as children
/// commands
/// .spawn((RigidBody::Dynamic, Collider::ball(0.5)))
/// .with_children(|children| {
/// // Spawn the child colliders positioned relative to the rigid body
/// children.spawn((Collider::ball(0.5), Transform::from_xyz(2.0, 0.0, 0.0)));
/// children.spawn((Collider::ball(0.5), Transform::from_xyz(-2.0, 0.0, 0.0)));
/// });
/// }
/// ```
///
/// Colliders can be arbitrarily nested and transformed relative to the parent.
/// The rigid body that a collider is attached to can be accessed using the [`ColliderParent`] component.
///
/// The benefit of using separate entities for the colliders is that each collider can have its own
/// [friction](Friction), [restitution](Restitution), [collision layers](CollisionLayers),
/// and other configuration options, and they send separate [collision events](ContactReportingPlugin#collision-events).
///
/// ## See more
///
/// - [Rigid bodies](RigidBody)
/// - [Density](ColliderDensity)
/// - [Friction] and [restitution](Restitution) (bounciness)
/// - [Collision layers](CollisionLayers)
/// - [Sensors](Sensor)
#[cfg_attr(
feature = "3d",
doc = "- Creating colliders from meshes with [`AsyncCollider`] and [`AsyncSceneCollider`]"
)]
/// - [Get colliding entities](CollidingEntities)
/// - [Collision events](ContactReportingPlugin#collision-events)
/// - [Accessing, filtering and modifying collisions](Collisions)
/// - [Manual contact queries](contact_query)
///
/// ## 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 [`Collider::shape()`]
/// or [`Collider::shape_scaled()`] methods.
#[derive(Clone, Component)]
pub struct Collider {
/// The raw unscaled collider shape.
shape: SharedShape,
/// The scaled version of the collider shape.
///
/// If the scale is `Vector::ONE`, this will be `None` and `unscaled_shape`
/// will be used instead.
scaled_shape: SharedShape,
/// The global scale used for the collider shape.
scale: Vector,
}
impl From<SharedShape> for Collider {
fn from(value: SharedShape) -> Self {
Self {
shape: value.clone(),
scaled_shape: value,
scale: Vector::ONE,
}
}
}
impl Default for Collider {
fn default() -> Self {
#[cfg(feature = "2d")]
{
Self::cuboid(0.5, 0.5)
}
#[cfg(feature = "3d")]
{
Self::cuboid(0.5, 0.5, 0.5)
}
}
}
impl fmt::Debug for Collider {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.shape_scaled().as_typed_shape() {
TypedShape::Ball(shape) => write!(f, "{:?}", shape),
TypedShape::Cuboid(shape) => write!(f, "{:?}", shape),
TypedShape::RoundCuboid(shape) => write!(f, "{:?}", shape),
TypedShape::Capsule(shape) => write!(f, "{:?}", shape),
TypedShape::Segment(shape) => write!(f, "{:?}", shape),
TypedShape::Triangle(shape) => write!(f, "{:?}", shape),
TypedShape::RoundTriangle(shape) => write!(f, "{:?}", shape),
TypedShape::TriMesh(_) => write!(f, "Trimesh (not representable)"),
TypedShape::Polyline(_) => write!(f, "Polyline (not representable)"),
TypedShape::HalfSpace(shape) => write!(f, "{:?}", shape),
TypedShape::HeightField(shape) => write!(f, "{:?}", shape),
TypedShape::Compound(_) => write!(f, "Compound (not representable)"),
TypedShape::Custom(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::ConvexPolyhedron(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::Cylinder(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::Cone(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::RoundCylinder(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::RoundCone(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "3d")]
TypedShape::RoundConvexPolyhedron(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "2d")]
TypedShape::ConvexPolygon(shape) => write!(f, "{:?}", shape),
#[cfg(feature = "2d")]
TypedShape::RoundConvexPolygon(shape) => write!(f, "{:?}", shape),
}
}
}
impl Collider {
/// Returns the raw unscaled shape of the collider.
pub fn shape(&self) -> &SharedShape {
&self.shape
}
/// Returns the shape of the collider with the scale from its `GlobalTransform` applied.
pub fn shape_scaled(&self) -> &SharedShape {
&self.scaled_shape
}
/// Returns the global scale of the collider.
pub fn scale(&self) -> Vector {
self.scale
}
/// Sets the unscaled shape of the collider. The collider's scale will be applied to this shape.
pub fn set_shape(&mut self, shape: SharedShape) {
if self.scale != Vector::ONE {
self.scaled_shape = shape.clone();
}
self.shape = shape;
}
/// Set the global scaling factor of this shape.
///
/// If the scaling factor is not uniform, and the scaled shape can’t be
/// represented as a supported shape, the shape is approximated as
/// a convex polygon or polyhedron using `num_subdivisions`.
///
/// For example, if a ball was scaled to an ellipse, the new shape would be approximated.
pub fn set_scale(&mut self, scale: Vector, num_subdivisions: u32) {
if scale == self.scale {
return;
}
if scale == Vector::ONE {
// Trivial case.
self.scaled_shape = self.shape.clone();
self.scale = Vector::ONE;
return;
}
if let Ok(scaled) = scale_shape(&self.shape, scale, num_subdivisions) {
self.scaled_shape = scaled;
self.scale = scale;
} else {
log::error!("Failed to create convex hull for scaled collider.");
}
}
/// Computes the [Axis-Aligned Bounding Box](ColliderAabb) of the collider.
#[cfg(feature = "2d")]
pub fn compute_aabb(&self, position: Vector, rotation: Scalar) -> ColliderAabb {
ColliderAabb(self.shape_scaled().compute_aabb(&utils::make_isometry(
position,
Rotation::from_radians(rotation),
)))
}
/// Computes the [Axis-Aligned Bounding Box](ColliderAabb) of the collider.
#[cfg(feature = "3d")]
pub fn compute_aabb(&self, position: Vector, rotation: Quaternion) -> ColliderAabb {
ColliderAabb(
self.shape_scaled()
.compute_aabb(&utils::make_isometry(position, Rotation(rotation))),
)
}
/// Computes the collider's mass properties based on its shape and a given density.
pub fn mass_properties(&self, density: Scalar) -> ColliderMassProperties {
ColliderMassProperties::new(self, density)
}
/// 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.
pub fn compound(
shapes: Vec<(
impl Into<Position>,
impl Into<Rotation>,
impl Into<Collider>,
)>,
) -> Self {
let shapes = shapes
.into_iter()
.map(|(p, r, c)| {
(
utils::make_isometry(*p.into(), r.into()),
c.into().shape_scaled().clone(),
)
})
.collect::<Vec<_>>();
SharedShape::compound(shapes).into()
}
/// Creates a collider with a ball shape defined by its radius.
pub fn ball(radius: Scalar) -> Self {
SharedShape::ball(radius).into()
}
/// Creates a collider with a cuboid shape defined by its extents.
#[cfg(feature = "2d")]
pub fn cuboid(x_length: Scalar, y_length: Scalar) -> Self {
SharedShape::cuboid(x_length * 0.5, y_length * 0.5).into()
}
/// Creates a collider with a cuboid shape defined by its extents.
#[cfg(feature = "3d")]
pub fn cuboid(x_length: Scalar, y_length: Scalar, z_length: Scalar) -> Self {
SharedShape::cuboid(x_length * 0.5, y_length * 0.5, z_length * 0.5).into()
}
/// Creates a collider with a cylinder shape defined by its height along the `Y` axis and its radius on the `XZ` plane.
#[cfg(feature = "3d")]
pub fn cylinder(height: Scalar, radius: Scalar) -> Self {
SharedShape::cylinder(height * 0.5, radius).into()
}
/// 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.
#[cfg(feature = "3d")]
pub fn cone(height: Scalar, radius: Scalar) -> Self {
SharedShape::cone(height * 0.5, radius).into()
}
/// Creates a collider with a capsule shape defined by its height along the `Y` axis and its radius.
pub fn capsule(height: Scalar, radius: Scalar) -> Self {
SharedShape::capsule(
(Vector::Y * height * 0.5).into(),
(Vector::NEG_Y * height * 0.5).into(),
radius,
)
.into()
}
/// Creates a collider with a capsule shape defined by its end points `a` and `b` and its radius.
pub fn capsule_endpoints(a: Vector, b: Vector, radius: Scalar) -> Self {
SharedShape::capsule(a.into(), b.into(), radius).into()
}
/// Creates a collider with a [half-space](https://en.wikipedia.org/wiki/Half-space_(geometry)) shape defined by the outward normal of its planar boundary.
pub fn halfspace(outward_normal: Vector) -> Self {
SharedShape::halfspace(nalgebra::Unit::new_normalize(outward_normal.into())).into()
}
/// Creates a collider with a segment shape defined by its endpoints `a` and `b`.
pub fn segment(a: Vector, b: Vector) -> Self {
SharedShape::segment(a.into(), b.into()).into()
}
/// Creates a collider with a triangle shape defined by its points `a`, `b` and `c`.
pub fn triangle(a: Vector, b: Vector, c: Vector) -> Self {
SharedShape::triangle(a.into(), b.into(), c.into()).into()
}
/// Creates a collider with a polyline shape defined by its vertices and optionally an index buffer.
pub fn polyline(vertices: Vec<Vector>, indices: Option<Vec<[u32; 2]>>) -> Self {
let vertices = vertices.into_iter().map(|v| v.into()).collect();
SharedShape::polyline(vertices, indices).into()
}
/// Creates a collider with a triangle mesh shape defined by its vertex and index buffers.
pub fn trimesh(vertices: Vec<Vector>, indices: Vec<[u32; 3]>) -> Self {
let vertices = vertices.into_iter().map(|v| v.into()).collect();
SharedShape::trimesh(vertices, indices).into()
}
/// Creates a collider with a triangle mesh shape defined by its vertex and index buffers
/// and flags controlling the preprocessing.
pub fn trimesh_with_config(
vertices: Vec<Vector>,
indices: Vec<[u32; 3]>,
flags: TriMeshFlags,
) -> Self {
let vertices = vertices.into_iter().map(|v| v.into()).collect();
SharedShape::trimesh_with_flags(vertices, indices, flags).into()
}
/// Creates a collider shape with a compound shape obtained from the decomposition of a given polyline
/// defined by its vertex and index buffers.
#[cfg(feature = "2d")]
pub fn convex_decomposition(vertices: Vec<Vector>, indices: Vec<[u32; 2]>) -> Self {
let vertices = vertices.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_decomposition(&vertices, &indices).into()
}
/// Creates a collider shape with a compound shape obtained from the decomposition of a given trimesh
/// defined by its vertex and index buffers.
#[cfg(feature = "3d")]
pub fn convex_decomposition(vertices: Vec<Vector>, indices: Vec<[u32; 3]>) -> Self {
let vertices = vertices.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_decomposition(&vertices, &indices).into()
}
/// Creates a collider shape with a compound shape obtained from the decomposition of a given polyline
/// defined by its vertex and index buffers. The given [`VHACDParameters`] are used for configuring
/// the decomposition process.
#[cfg(feature = "2d")]
pub fn convex_decomposition_with_config(
vertices: Vec<Vector>,
indices: Vec<[u32; 2]>,
params: &VHACDParameters,
) -> Self {
let vertices = vertices.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_decomposition_with_params(&vertices, &indices, params).into()
}
/// Creates a collider shape with a compound shape obtained from the decomposition of a given trimesh
/// defined by its vertex and index buffers. The given [`VHACDParameters`] are used for configuring
/// the decomposition process.
#[cfg(feature = "3d")]
pub fn convex_decomposition_with_config(
vertices: Vec<Vector>,
indices: Vec<[u32; 3]>,
params: &VHACDParameters,
) -> Self {
let vertices = vertices.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_decomposition_with_params(&vertices, &indices, params).into()
}
/// Creates a collider with a [convex polygon](https://en.wikipedia.org/wiki/Convex_polygon) shape obtained after computing
/// the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of the given points.
#[cfg(feature = "2d")]
pub fn convex_hull(points: Vec<Vector>) -> Option<Self> {
let points = points.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_hull(&points).map(Into::into)
}
/// Creates a collider with a [convex polyhedron](https://en.wikipedia.org/wiki/Convex_polytope) shape obtained after computing
/// the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) of the given points.
#[cfg(feature = "3d")]
pub fn convex_hull(points: Vec<Vector>) -> Option<Self> {
let points = points.iter().map(|v| (*v).into()).collect::<Vec<_>>();
SharedShape::convex_hull(&points).map(Into::into)
}
/// Creates a collider with a heightfield shape.
///
/// A 2D heightfield is a segment along the `X` axis, subdivided at regular intervals.
///
/// `heights` is a vector indicating the altitude of each subdivision point, and `scale` is a scalar value
/// indicating the length of each subdivided segment along the `X` axis.
#[cfg(feature = "2d")]
pub fn heightfield(heights: Vec<Scalar>, scale: Scalar) -> Self {
SharedShape::heightfield(heights.into(), Vector::splat(scale).into()).into()
}
/// 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.
#[cfg(feature = "3d")]
pub fn heightfield(heights: Vec<Vec<Scalar>>, scale: Vector) -> Self {
let row_count = heights.len();
let column_count = heights[0].len();
let data: Vec<Scalar> = heights.into_iter().flatten().collect();
assert_eq!(
data.len(),
row_count * column_count,
"Each row in `heights` must have the same amount of points"
);
let heights = nalgebra::DMatrix::from_vec(row_count, column_count, data);
SharedShape::heightfield(heights, scale.into()).into()
}
/// Creates a collider with a triangle mesh shape from a `Mesh`.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
/// let mesh = Mesh::from(shape::Cube { size: 1.0 });
/// commands.spawn((
/// Collider::trimesh_from_mesh(&mesh).unwrap(),
/// PbrBundle {
/// mesh: meshes.add(mesh),
/// ..default()
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
pub fn trimesh_from_mesh(mesh: &Mesh) -> Option<Self> {
extract_mesh_vertices_indices(mesh).map(|(vertices, indices)| {
SharedShape::trimesh_with_flags(
vertices,
indices,
TriMeshFlags::MERGE_DUPLICATE_VERTICES,
)
.into()
})
}
/// Creates a collider with a triangle mesh shape from a `Mesh` using the given [`TriMeshFlags`]
/// for controlling the preprocessing.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
/// let mesh = Mesh::from(shape::Cube { size: 1.0 });
/// commands.spawn((
/// Collider::trimesh_from_mesh_with_config(&mesh, TriMeshFlags::all()).unwrap(),
/// PbrBundle {
/// mesh: meshes.add(mesh),
/// ..default()
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
pub fn trimesh_from_mesh_with_config(mesh: &Mesh, flags: TriMeshFlags) -> Option<Self> {
extract_mesh_vertices_indices(mesh).map(|(vertices, indices)| {
SharedShape::trimesh_with_flags(vertices, indices, flags).into()
})
}
/// Creates a collider with a convex polygon shape obtained from the convex hull of a `Mesh`.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
/// let mesh = Mesh::from(shape::Cube { size: 1.0 });
/// commands.spawn((
/// Collider::convex_hull_from_mesh(&mesh).unwrap(),
/// PbrBundle {
/// mesh: meshes.add(mesh),
/// ..default()
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
pub fn convex_hull_from_mesh(mesh: &Mesh) -> Option<Self> {
extract_mesh_vertices_indices(mesh)
.and_then(|(vertices, _)| SharedShape::convex_hull(&vertices).map(|shape| shape.into()))
}
/// Creates a compound shape obtained from the decomposition of a `Mesh`.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
/// let mesh = Mesh::from(shape::Cube { size: 1.0 });
/// commands.spawn((
/// Collider::convex_decomposition_from_mesh(&mesh).unwrap(),
/// PbrBundle {
/// mesh: meshes.add(mesh),
/// ..default()
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
pub fn convex_decomposition_from_mesh(mesh: &Mesh) -> Option<Self> {
extract_mesh_vertices_indices(mesh).map(|(vertices, indices)| {
SharedShape::convex_decomposition(&vertices, &indices).into()
})
}
/// Creates a compound shape obtained from the decomposition of a `Mesh`
/// with the given [`VHACDParameters`] passed to the decomposition algorithm.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
/// let mesh = Mesh::from(shape::Cube { size: 1.0 });
/// let config = VHACDParameters {
/// convex_hull_approximation: false,
/// ..default()
/// };
/// commands.spawn((
/// Collider::convex_decomposition_from_mesh_with_config(&mesh, &config).unwrap(),
/// PbrBundle {
/// mesh: meshes.add(mesh),
/// ..default()
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
pub fn convex_decomposition_from_mesh_with_config(
mesh: &Mesh,
parameters: &VHACDParameters,
) -> Option<Self> {
extract_mesh_vertices_indices(mesh).map(|(vertices, indices)| {
SharedShape::convex_decomposition_with_params(&vertices, &indices, parameters).into()
})
}
}
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
type VerticesIndices = (Vec<nalgebra::Point3<Scalar>>, Vec<[u32; 3]>);
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
fn extract_mesh_vertices_indices(mesh: &Mesh) -> Option<VerticesIndices> {
let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION)?;
let indices = mesh.indices()?;
let vtx: Vec<_> = match vertices {
VertexAttributeValues::Float32(vtx) => Some(
vtx.chunks(3)
.map(|v| [v[0] as Scalar, v[1] as Scalar, v[2] as Scalar].into())
.collect(),
),
VertexAttributeValues::Float32x3(vtx) => Some(
vtx.iter()
.map(|v| [v[0] as Scalar, v[1] as Scalar, v[2] as Scalar].into())
.collect(),
),
_ => None,
}?;
let idx = match indices {
Indices::U16(idx) => idx
.chunks_exact(3)
.map(|i| [i[0] as u32, i[1] as u32, i[2] as u32])
.collect(),
Indices::U32(idx) => idx.chunks_exact(3).map(|i| [i[0], i[1], i[2]]).collect(),
};
Some((vtx, idx))
}
fn scale_shape(
shape: &SharedShape,
scale: Vector,
num_subdivisions: u32,
) -> Result<SharedShape, UnsupportedShape> {
match shape.as_typed_shape() {
TypedShape::Cuboid(s) => Ok(SharedShape::new(s.scaled(&scale.into()))),
TypedShape::RoundCuboid(s) => Ok(SharedShape::new(RoundShape {
border_radius: s.border_radius,
inner_shape: s.inner_shape.scaled(&scale.into()),
})),
TypedShape::Capsule(c) => match c.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to Capsule shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(b)) => Ok(SharedShape::new(b)),
Some(Either::Right(b)) => Ok(SharedShape::new(b)),
},
TypedShape::Ball(b) => match b.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to Ball shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(b)) => Ok(SharedShape::new(b)),
Some(Either::Right(b)) => Ok(SharedShape::new(b)),
},
TypedShape::Segment(s) => Ok(SharedShape::new(s.scaled(&scale.into()))),
TypedShape::Triangle(t) => Ok(SharedShape::new(t.scaled(&scale.into()))),
TypedShape::RoundTriangle(t) => Ok(SharedShape::new(RoundShape {
border_radius: t.border_radius,
inner_shape: t.inner_shape.scaled(&scale.into()),
})),
TypedShape::TriMesh(t) => Ok(SharedShape::new(t.clone().scaled(&scale.into()))),
TypedShape::Polyline(p) => Ok(SharedShape::new(p.clone().scaled(&scale.into()))),
TypedShape::HalfSpace(h) => match h.scaled(&scale.into()) {
None => {
log::error!("Failed to apply scale {} to HalfSpace shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(scaled) => Ok(SharedShape::new(scaled)),
},
TypedShape::HeightField(h) => Ok(SharedShape::new(h.clone().scaled(&scale.into()))),
#[cfg(feature = "2d")]
TypedShape::ConvexPolygon(cp) => match cp.clone().scaled(&scale.into()) {
None => {
log::error!("Failed to apply scale {} to ConvexPolygon shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(scaled) => Ok(SharedShape::new(scaled)),
},
#[cfg(feature = "2d")]
TypedShape::RoundConvexPolygon(cp) => match cp.inner_shape.clone().scaled(&scale.into()) {
None => {
log::error!(
"Failed to apply scale {} to RoundConvexPolygon shape.",
scale
);
Ok(SharedShape::ball(0.0))
}
Some(scaled) => Ok(SharedShape::new(RoundShape {
border_radius: cp.border_radius,
inner_shape: scaled,
})),
},
#[cfg(feature = "3d")]
TypedShape::ConvexPolyhedron(cp) => match cp.clone().scaled(&scale.into()) {
None => {
log::error!("Failed to apply scale {} to ConvexPolyhedron shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(scaled) => Ok(SharedShape::new(scaled)),
},
#[cfg(feature = "3d")]
TypedShape::RoundConvexPolyhedron(cp) => {
match cp.clone().inner_shape.scaled(&scale.into()) {
None => {
log::error!(
"Failed to apply scale {} to RoundConvexPolyhedron shape.",
scale
);
Ok(SharedShape::ball(0.0))
}
Some(scaled) => Ok(SharedShape::new(RoundShape {
border_radius: cp.border_radius,
inner_shape: scaled,
})),
}
}
#[cfg(feature = "3d")]
TypedShape::Cylinder(c) => match c.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to Cylinder shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(b)) => Ok(SharedShape::new(b)),
Some(Either::Right(b)) => Ok(SharedShape::new(b)),
},
#[cfg(feature = "3d")]
TypedShape::RoundCylinder(c) => {
match c.inner_shape.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to RoundCylinder shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(scaled)) => Ok(SharedShape::new(RoundShape {
border_radius: c.border_radius,
inner_shape: scaled,
})),
Some(Either::Right(scaled)) => Ok(SharedShape::new(RoundShape {
border_radius: c.border_radius,
inner_shape: scaled,
})),
}
}
#[cfg(feature = "3d")]
TypedShape::Cone(c) => match c.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to Cone shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(b)) => Ok(SharedShape::new(b)),
Some(Either::Right(b)) => Ok(SharedShape::new(b)),
},
#[cfg(feature = "3d")]
TypedShape::RoundCone(c) => match c.inner_shape.scaled(&scale.into(), num_subdivisions) {
None => {
log::error!("Failed to apply scale {} to RoundCone shape.", scale);
Ok(SharedShape::ball(0.0))
}
Some(Either::Left(scaled)) => Ok(SharedShape::new(RoundShape {
border_radius: c.border_radius,
inner_shape: scaled,
})),
Some(Either::Right(scaled)) => Ok(SharedShape::new(RoundShape {
border_radius: c.border_radius,
inner_shape: scaled,
})),
},
TypedShape::Compound(c) => {
let mut scaled = Vec::with_capacity(c.shapes().len());
for (iso, shape) in c.shapes() {
scaled.push((
#[cfg(feature = "2d")]
make_isometry(
Vector::from(iso.translation) * scale,
Rotation::from_radians(iso.rotation.angle()),
),
#[cfg(feature = "3d")]
make_isometry(
Vector::from(iso.translation) * scale,
Quaternion::from(iso.rotation),
),
scale_shape(shape, scale, num_subdivisions)?,
));
}
Ok(SharedShape::compound(scaled))
}
_ => Err(parry::query::Unsupported),
}
}
/// A component that will automatically generate a [`Collider`] based on the entity's `Mesh`.
/// The type of the generated collider can be specified using [`ComputedCollider`].
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>) {
/// // Spawn a cube with a convex hull collider generated from the mesh
/// commands.spawn((
/// AsyncCollider(ComputedCollider::ConvexHull),
/// PbrBundle {
/// mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
/// ..default(),
/// },
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "async-collider"))]
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
pub struct AsyncCollider(pub ComputedCollider);
/// A component that will automatically generate colliders for the meshes in a scene
/// once the scene has been loaded. The type of the generated collider can be specified
/// using [`ComputedCollider`].
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// use bevy_xpbd_3d::prelude::*;
///
/// fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>) {
/// let scene = SceneBundle {
/// scene: assets.load("my_model.gltf#Scene0"),
/// ..default()
/// };
///
/// // Spawn the scene and automatically generate triangle mesh colliders
/// commands.spawn((
/// scene.clone(),
/// AsyncSceneCollider::new(Some(ComputedCollider::TriMesh)),
/// ));
///
/// // Specify configuration for specific meshes by name
/// commands.spawn((
/// scene.clone(),
/// AsyncSceneCollider::new(Some(ComputedCollider::TriMesh))
/// .with_shape_for_name("Tree", ComputedCollider::ConvexHull)
/// .with_layers_for_name("Tree", CollisionLayers::from_bits(0b0010))
/// .with_density_for_name("Tree", 2.5),
/// ));
///
/// // Only generate colliders for specific meshes by name
/// commands.spawn((
/// scene.clone(),
/// AsyncSceneCollider::new(None)
/// .with_shape_for_name("Tree".to_string(), Some(ComputedCollider::ConvexHull)),
/// ));
///
/// // Generate colliders for everything except specific meshes by name
/// commands.spawn((
/// scene,
/// AsyncSceneCollider::new(ComputedCollider::TriMesh)
/// .without_shape_for_name("Tree"),
/// ));
/// }
/// ```
#[cfg(all(feature = "3d", feature = "async-collider"))]
#[derive(Component, Clone, Debug, Default, PartialEq)]
pub struct AsyncSceneCollider {
/// The default collider type used for each mesh that isn't included in [`meshes_by_name`](#structfield.meshes_by_name).
/// If `None`, all meshes except the ones in [`meshes_by_name`](#structfield.meshes_by_name) will be skipped.
pub default_shape: Option<ComputedCollider>,
/// Specifies data like the collider type and [`CollisionLayers`] for meshes by name.
/// Entries with a `None` value will be skipped.
/// For the meshes not found in this `HashMap`, [`default_shape`](#structfield.default_shape)
/// and all collision layers will be used instead.
pub meshes_by_name: HashMap<String, Option<AsyncSceneColliderData>>,
}
#[cfg(all(feature = "3d", feature = "async-collider"))]
impl AsyncSceneCollider {
/// Creates a new [`AsyncSceneCollider`] with the default collider type used for
/// meshes set to the given `default_shape`.
///
/// If the given collider type is `None`, all meshes except the ones in
/// [`meshes_by_name`](#structfield.meshes_by_name) will be skipped.
/// You can add named shapes using [`with_shape_for_name`](Self::with_shape_for_name).
pub fn new(default_shape: Option<ComputedCollider>) -> Self {
Self {
default_shape,
meshes_by_name: default(),
}
}
/// Specifies the collider type used for a mesh with the given `name`.
pub fn with_shape_for_name(mut self, name: &str, shape: ComputedCollider) -> Self {
if let Some(Some(data)) = self.meshes_by_name.get_mut(name) {
data.shape = shape;
} else {
self.meshes_by_name.insert(
name.to_string(),
Some(AsyncSceneColliderData { shape, ..default() }),
);
}
self
}
/// Specifies the [`CollisionLayers`] used for a mesh with the given `name`.
pub fn with_layers_for_name(mut self, name: &str, layers: CollisionLayers) -> Self {
if let Some(Some(data)) = self.meshes_by_name.get_mut(name) {
data.layers = layers;
} else {
self.meshes_by_name.insert(
name.to_string(),
Some(AsyncSceneColliderData {
layers,
..default()
}),
);
}
self
}
/// Specifies the [`ColliderDensity`] used for a mesh with the given `name`.
pub fn with_density_for_name(mut self, name: &str, density: Scalar) -> Self {
if let Some(Some(data)) = self.meshes_by_name.get_mut(name) {
data.density = density;
} else {
self.meshes_by_name.insert(
name.to_string(),
Some(AsyncSceneColliderData {
density,
..default()
}),
);
}
self
}
/// Sets collider for the mesh associated with the given `name` to `None`, skipping
/// collider generation for it.
pub fn without_shape_with_name(mut self, name: &str) -> Self {
self.meshes_by_name.insert(name.to_string(), None);
self
}
}
/// Configuration for a specific collider generated from a scene using [`AsyncSceneCollider`].
#[cfg(all(feature = "3d", feature = "async-collider"))]
#[derive(Clone, Debug, PartialEq)]
pub struct AsyncSceneColliderData {
/// The type of collider generated for the mesh.
pub shape: ComputedCollider,
/// The [`CollisionLayers`] used for this collider.
pub layers: CollisionLayers,
/// The [`ColliderDensity`] used for this collider.
pub density: Scalar,
}
#[cfg(all(feature = "3d", feature = "async-collider"))]
impl Default for AsyncSceneColliderData {
fn default() -> Self {
Self {
shape: ComputedCollider::TriMesh,
layers: CollisionLayers::default(),
density: 1.0,
}
}
}
/// Determines how a [`Collider`] is generated from a `Mesh`.
///
/// Colliders can be created from meshes with the following components and methods:
///
/// - [`AsyncCollider`] (requires `async-collider` features)
/// - [`AsyncSceneCollider`] (requires `async-collider` features)
/// - [`Collider::trimesh_from_mesh`]
/// - [`Collider::convex_hull_from_mesh`]
/// - [`Collider::convex_decomposition_from_mesh`]
#[cfg(all(feature = "3d", feature = "collider-from-mesh"))]
#[derive(Component, Clone, Debug, Default, PartialEq)]
pub enum ComputedCollider {
/// A triangle mesh.
#[default]
TriMesh,
/// A convex hull.
ConvexHull,
/// A compound shape obtained from a decomposition into convex parts using the specified
/// [`VHACDParameters`].
ConvexDecomposition(VHACDParameters),
}
/// A component that stores the `Entity` ID of the [`RigidBody`] that a [`Collider`] is attached to.
///
/// If the collider is a child of a rigid body, this points to the body's `Entity` ID.
/// If the [`Collider`] component is instead on the same entity as the [`RigidBody`] component,
/// this points to the collider's own `Entity` ID.
///
/// This component is added and updated automatically based on entity hierarchies and should not
/// be modified directly.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use bevy_xpbd_3d::prelude::*;")]
///
/// fn setup(mut commands: Commands) {
/// // Spawn a rigid body with one collider on the same entity and two as children.
/// // Each entity will have a ColliderParent component that has the same rigid body entity.
/// commands
/// .spawn((RigidBody::Dynamic, Collider::ball(0.5)))
/// .with_children(|children| {
/// children.spawn((Collider::ball(0.5), Transform::from_xyz(2.0, 0.0, 0.0)));
/// children.spawn((Collider::ball(0.5), Transform::from_xyz(-2.0, 0.0, 0.0)));
/// });
/// }
/// ```
#[derive(Reflect, Clone, Copy, Component, Debug, PartialEq, Eq)]
pub struct ColliderParent(pub(crate) Entity);
impl ColliderParent {
/// Gets the `Entity` ID of the [`RigidBody`] that this [`Collider`] is attached to.
pub const fn get(&self) -> Entity {
self.0
}
}
/// The transform of a collider relative to the rigid body it's attached to.
/// This is in the local space of the body, not the collider itself.
///
/// This is used for computing things like contact positions and a body's center of mass
/// without having to traverse deeply nested hierarchies. It's updated automatically,
/// so you shouldn't modify it manually.
#[derive(Reflect, Clone, Copy, Component, Debug, PartialEq)]
pub struct ColliderTransform {
/// The translation of a collider in a rigid body's frame of reference.
pub translation: Vector,
/// The rotation of a collider in a rigid body's frame of reference.
pub rotation: Rotation,
/// The global scale of a collider. Equivalent to the `GlobalTransform` scale.
pub scale: Vector,
}
impl ColliderTransform {
/// Transforms a given point by applying the translation, rotation and scale of
/// this [`ColliderTransform`].
pub fn transform_point(&self, mut point: Vector) -> Vector {
point *= self.scale;
point = self.rotation.rotate(point);
point += self.translation;
point
}
}
impl Default for ColliderTransform {
fn default() -> Self {
Self {
translation: Vector::ZERO,
rotation: Rotation::default(),
scale: Vector::ONE,
}
}
}
impl From<Transform> for ColliderTransform {
fn from(value: Transform) -> Self {
Self {
#[cfg(feature = "2d")]
translation: value.translation.truncate().adjust_precision(),
#[cfg(feature = "3d")]
translation: value.translation.adjust_precision(),
rotation: Rotation::from(value.rotation.adjust_precision()),
#[cfg(feature = "2d")]
scale: value.scale.truncate().adjust_precision(),
#[cfg(feature = "3d")]
scale: value.scale.adjust_precision(),
}
}
}
/// A component that marks a [`Collider`] as a sensor, also known as a trigger.
///
/// Sensor colliders send [collision events](ContactReportingPlugin#collision-events) and register intersections,
/// but allow other bodies to pass through them. This is often used to detect when something enters
/// or leaves an area or is intersecting some shape.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use bevy_xpbd_3d::prelude::*;")]
///
/// fn setup(mut commands: Commands) {
/// // Spawn a static body with a sensor collider.
/// // Other bodies will pass through, but it will still send collision events.
/// commands.spawn((RigidBody::Static, Collider::ball(0.5), Sensor));
/// }
/// ```
#[doc(alias = "Trigger")]
#[derive(Reflect, Clone, Component, Debug, Default, PartialEq, Eq)]
#[reflect(Component)]
pub struct Sensor;
/// The Axis-Aligned Bounding Box of a [collider](Collider).
#[derive(Clone, Copy, Component, Debug, Deref, DerefMut, PartialEq)]
pub struct ColliderAabb(pub Aabb);
impl ColliderAabb {
/// Creates a new collider from a given [`SharedShape`] with a default density of 1.0.
pub fn from_shape(shape: &SharedShape) -> Self {
Self(shape.compute_local_aabb())
}
}
impl Default for ColliderAabb {
fn default() -> Self {
ColliderAabb(Aabb::new_invalid())
}
}
/// A component that stores the entities that are colliding with an entity.
///
/// This component is automatically added for all entities with a [`Collider`],
/// but it will only be filled if the [`ContactReportingPlugin`] is enabled (by default, it is).
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
#[cfg_attr(feature = "2d", doc = "use bevy_xpbd_2d::prelude::*;")]
#[cfg_attr(feature = "3d", doc = "use bevy_xpbd_3d::prelude::*;")]
///
/// fn my_system(query: Query<(Entity, &CollidingEntities)>) {
/// for (entity, colliding_entities) in &query {
/// println!(
/// "{:?} is colliding with the following entities: {:?}",
/// entity,
/// colliding_entities
/// );
/// }
/// }
/// ```
#[derive(Reflect, Clone, Component, Debug, Default, Deref, DerefMut, PartialEq, Eq)]
#[reflect(Component)]
pub struct CollidingEntities(pub HashSet<Entity>);