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
use crate::prelude::*;
use bevy::prelude::*;
use parry::query::details::TOICompositeShapeShapeBestFirstVisitor;
/// A component used for [shape casting](spatial_query#shape-casting).
///
/// **Shape casting** is a type of [spatial query](spatial_query) where a shape travels along a straight
/// line and computes hits with colliders. This is often used to determine how far an object can move
/// in a direction before it hits something.
///
/// Each shape cast is defined by a `shape` (a [`Collider`]), its local `shape_rotation`, a local `origin` and
/// a local `direction`. The [`ShapeCaster`] will find each hit and add them to the [`ShapeHits`] component in
/// the order of the time of impact.
///
/// Computing lots of hits can be expensive, especially against complex geometry, so the maximum number of hits
/// is one by default. This can be configured through the `max_hits` property.
///
/// The [`ShapeCaster`] is the easiest way to handle simple shape casting. If you want more control and don't want
/// to perform shape casts on every frame, consider using the [`SpatialQuery`] system parameter.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// # #[cfg(feature = "2d")]
/// # use bevy_xpbd_2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use bevy_xpbd_3d::prelude::*;
///
/// # #[cfg(all(feature = "3d", feature = "f32"))]
/// fn setup(mut commands: Commands) {
/// // Spawn a shape caster with a ball shape moving right starting from the origin
/// commands.spawn(ShapeCaster::new(
/// Collider::ball(0.5),
/// Vec3::ZERO,
/// Quat::default(),
/// Vec3::X
/// ));
/// }
///
/// fn print_hits(query: Query<(&ShapeCaster, &ShapeHits)>) {
/// for (shape_caster, hits) in &query {
/// for hit in hits.iter() {
/// println!("Hit entity {:?}", hit.entity);
/// }
/// }
/// }
/// ```
#[derive(Component)]
pub struct ShapeCaster {
/// Controls if the shape caster is enabled.
pub enabled: bool,
/// The shape being cast represented as a [`Collider`].
pub shape: Collider,
/// The local origin of the shape relative to the [`Position`] and [`Rotation`]
/// of the shape caster entity or its parent.
///
/// To get the global origin, use the `global_origin` method.
pub origin: Vector,
/// The global origin of the shape.
global_origin: Vector,
/// The local rotation of the shape being cast relative to the [`Rotation`]
/// of the shape caster entity or its parent. Expressed in radians.
///
/// To get the global shape rotation, use the `global_shape_rotation` method.
#[cfg(feature = "2d")]
pub shape_rotation: Scalar,
/// The local rotation of the shape being cast relative to the [`Rotation`]
/// of the shape caster entity or its parent.
///
/// To get the global shape rotation, use the `global_shape_rotation` method.
#[cfg(feature = "3d")]
pub shape_rotation: Quaternion,
/// The global rotation of the shape.
#[cfg(feature = "2d")]
global_shape_rotation: Scalar,
/// The global rotation of the shape.
#[cfg(feature = "3d")]
global_shape_rotation: Quaternion,
/// The local direction of the shape cast relative to the [`Rotation`] of the shape caster entity or its parent.
///
/// To get the global direction, use the `global_direction` method.
pub direction: Vector,
/// The global direction of the shape cast.
global_direction: Vector,
/// The maximum distance the shape can travel. By default this is infinite, so the shape will travel
/// until a hit is found.
pub max_time_of_impact: Scalar,
/// The maximum number of hits allowed. By default this is one and only the first hit is returned.
pub max_hits: u32,
/// Controls how the shape cast behaves when the shape is already penetrating a [collider](Collider)
/// at the shape origin.
///
/// If set to true **and** the shape is being cast in a direction where it will eventually stop penetrating,
/// the shape cast will not stop immediately, and will instead continue until another hit.\
/// If set to false, the shape cast will stop immediately and return the hit. This is the default.
pub ignore_origin_penetration: bool,
/// Rules that determine which colliders are taken into account in the query.
pub query_filter: SpatialQueryFilter,
}
impl Default for ShapeCaster {
fn default() -> Self {
Self {
enabled: true,
shape: Collider::ball(0.0),
origin: Vector::ZERO,
global_origin: Vector::ZERO,
#[cfg(feature = "2d")]
shape_rotation: 0.0,
#[cfg(feature = "3d")]
shape_rotation: Quaternion::IDENTITY,
#[cfg(feature = "2d")]
global_shape_rotation: 0.0,
#[cfg(feature = "3d")]
global_shape_rotation: Quaternion::IDENTITY,
direction: Vector::ZERO,
global_direction: Vector::ZERO,
max_time_of_impact: Scalar::MAX,
max_hits: 1,
ignore_origin_penetration: false,
query_filter: SpatialQueryFilter::default(),
}
}
}
impl ShapeCaster {
/// Creates a new [`ShapeCaster`] with a given shape, origin, shape rotation and direction.
#[cfg(feature = "2d")]
pub fn new(shape: Collider, origin: Vector, shape_rotation: Scalar, direction: Vector) -> Self {
Self {
shape,
origin,
shape_rotation,
direction: direction.normalize(),
..default()
}
}
#[cfg(feature = "3d")]
/// Creates a new [`ShapeCaster`] with a given shape, origin, shape rotation and direction.
pub fn new(
shape: Collider,
origin: Vector,
shape_rotation: Quaternion,
direction: Vector,
) -> Self {
Self {
shape,
origin,
shape_rotation,
direction,
..default()
}
}
/// Sets the ray origin.
pub fn with_origin(mut self, origin: Vector) -> Self {
self.origin = origin;
self
}
/// Sets the ray direction.
pub fn with_direction(mut self, direction: Vector) -> Self {
self.direction = direction;
self
}
/// Controls how the shape cast behaves when the shape is already penetrating a [collider](Collider)
/// at the shape origin.
///
/// If set to true **and** the shape is being cast in a direction where it will eventually stop penetrating,
/// the shape cast will not stop immediately, and will instead continue until another hit.\
/// If set to false, the shape cast will stop immediately and return the hit. This is the default.
pub fn with_ignore_origin_penetration(mut self, ignore: bool) -> Self {
self.ignore_origin_penetration = ignore;
self
}
/// Sets the maximum time of impact, i.e. the maximum distance that the ray is allowed to travel.
pub fn with_max_time_of_impact(mut self, max_time_of_impact: Scalar) -> Self {
self.max_time_of_impact = max_time_of_impact;
self
}
/// Sets the maximum number of allowed hits.
pub fn with_max_hits(mut self, max_hits: u32) -> Self {
self.max_hits = max_hits;
self
}
/// Sets the shape caster's [query filter](SpatialQueryFilter) that controls which colliders
/// should be included or excluded by shape casts.
pub fn with_query_filter(mut self, query_filter: SpatialQueryFilter) -> Self {
self.query_filter = query_filter;
self
}
/// Enables the [`ShapeCaster`].
pub fn enable(&mut self) {
self.enabled = true;
}
/// Disables the [`ShapeCaster`].
pub fn disable(&mut self) {
self.enabled = false;
}
/// Returns the global origin of the ray.
pub fn global_origin(&self) -> Vector {
self.global_origin
}
/// Returns the global rotation of the shape.
#[cfg(feature = "2d")]
pub fn global_shape_rotation(&self) -> Scalar {
self.global_shape_rotation
}
/// Returns the global rotation of the shape.
#[cfg(feature = "3d")]
pub fn global_shape_rotation(&self) -> Quaternion {
self.global_shape_rotation
}
/// Returns the global direction of the ray.
pub fn global_direction(&self) -> Vector {
self.global_direction
}
/// Sets the global origin of the ray.
pub(crate) fn set_global_origin(&mut self, global_origin: Vector) {
self.global_origin = global_origin;
}
/// Sets the global rotation of the shape.
#[cfg(feature = "2d")]
pub(crate) fn set_global_shape_rotation(&mut self, global_rotation: Scalar) {
self.global_shape_rotation = global_rotation;
}
/// Sets the global rotation of the shape.
#[cfg(feature = "3d")]
pub(crate) fn set_global_shape_rotation(&mut self, global_rotation: Quaternion) {
self.global_shape_rotation = global_rotation;
}
/// Sets the global direction of the ray.
pub(crate) fn set_global_direction(&mut self, global_direction: Vector) {
self.global_direction = global_direction;
}
pub(crate) fn cast(&self, hits: &mut ShapeHits, query_pipeline: &SpatialQueryPipeline) {
hits.count = 0;
let shape_rotation: Rotation;
#[cfg(feature = "2d")]
{
shape_rotation = Rotation::from_radians(self.global_shape_rotation());
}
#[cfg(feature = "3d")]
{
shape_rotation = Rotation::from(self.global_shape_rotation());
}
let shape_isometry = utils::make_isometry(self.global_origin(), &shape_rotation);
let shape_direction = self.global_direction().into();
let mut query_filter = self.query_filter.clone();
while hits.count < self.max_hits {
let pipeline_shape = query_pipeline.as_composite_shape(query_filter.clone());
let mut visitor = TOICompositeShapeShapeBestFirstVisitor::new(
&*query_pipeline.dispatcher,
&shape_isometry,
&shape_direction,
&pipeline_shape,
&**self.shape.get_shape(),
self.max_time_of_impact,
!self.ignore_origin_penetration,
);
if let Some(hit) = query_pipeline.qbvh.traverse_best_first(&mut visitor).map(
|(_, (entity_index, hit))| ShapeHitData {
entity: query_pipeline.entity_from_index(entity_index),
time_of_impact: hit.toi,
point1: hit.witness1.into(),
point2: hit.witness2.into(),
normal1: hit.normal1.into(),
normal2: hit.normal2.into(),
},
) {
if (hits.vector.len() as u32) < hits.count + 1 {
hits.vector.push(hit);
} else {
hits.vector[0] = hit;
}
hits.count += 1;
query_filter.excluded_entities.insert(hit.entity);
} else {
return;
}
}
}
}
/// Contains the hits of a shape cast by a [`ShapeCaster`]. The hits are in the order of time of impact.
///
/// The maximum number of hits depends on the value of `max_hits` in [`ShapeCaster`]. By default only
/// one hit is computed, as shape casting for many results can be expensive.
///
/// ## Example
///
/// ```
/// use bevy::prelude::*;
/// # #[cfg(feature = "2d")]
/// # use bevy_xpbd_2d::prelude::*;
/// # #[cfg(feature = "3d")]
/// use bevy_xpbd_3d::prelude::*;
///
/// fn print_hits(query: Query<&ShapeHits, With<ShapeCaster>>) {
/// for hits in &query {
/// for hit in hits.iter() {
/// println!(
/// "Hit entity {:?} with time of impact {}",
/// hit.entity,
/// hit.time_of_impact,
/// );
/// }
/// }
/// }
/// ```
#[derive(Component, Clone, Debug)]
pub struct ShapeHits {
pub(crate) vector: Vec<ShapeHitData>,
pub(crate) count: u32,
}
impl ShapeHits {
/// Returns a slice over the shape cast hits.
pub fn as_slice(&self) -> &[ShapeHitData] {
&self.vector[0..self.count as usize]
}
/// Returns the number of hits.
#[doc(alias = "count")]
pub fn len(&self) -> usize {
self.count as usize
}
/// Returns true if the number of hits is 0.
pub fn is_empty(&self) -> bool {
self.count == 0
}
/// Returns an iterator over the hits in the order of time of impact.
pub fn iter(&self) -> std::slice::Iter<ShapeHitData> {
self.as_slice().iter()
}
}
/// Data related to a hit during a [shape cast](spatial_query#shape-casting).
#[derive(Component, Clone, Copy, Debug)]
pub struct ShapeHitData {
/// The entity of the collider that was hit by the ray.
pub entity: Entity,
/// How long the shape travelled before the initial hit,
/// i.e. the distance between the origin and the point of intersection.
pub time_of_impact: Scalar,
/// The closest point on the cast shape, at the time of impact,
/// expressed in the local space of the cast shape.
pub point1: Vector,
/// The closest point on the collider that was hit by the shape cast, at the time of impact,
/// expressed in the local space of the collider shape.
pub point2: Vector,
/// The outward normal on the cast shape, at the time of impact,
/// expressed in the local space of the cast shape.
pub normal1: Vector,
/// The outward normal on the collider that was hit by the shape cast, at the time of impact,
/// expressed in the local space of the collider shape.
pub normal2: Vector,
}