bevy_math/bounding/mod.rs
1//! This module contains traits and implements for working with bounding shapes
2//!
3//! There are four traits used:
4//! - [`BoundingVolume`] is a generic abstraction for any bounding volume
5//! - [`IntersectsVolume`] abstracts intersection tests against a [`BoundingVolume`]
6//! - [`Bounded2d`]/[`Bounded3d`] are abstractions for shapes to generate [`BoundingVolume`]s
7
8/// A trait that generalizes different bounding volumes.
9/// Bounding volumes are simplified shapes that are used to get simpler ways to check for
10/// overlapping elements or finding intersections.
11///
12/// This trait supports both 2D and 3D bounding shapes.
13pub trait BoundingVolume: Sized {
14 /// The position type used for the volume. This should be `Vec2` for 2D and `Vec3` for 3D.
15 type Translation: Clone + Copy + PartialEq;
16
17 /// The rotation type used for the volume. This should be `f32` for 2D and `Quat` for 3D.
18 type Rotation: Clone + Copy + PartialEq;
19
20 /// The type used for the size of the bounding volume. Usually a half size. For example an
21 /// `f32` radius for a circle, or a `Vec3` with half sizes for x, y and z for a 3D axis-aligned
22 /// bounding box
23 type HalfSize;
24
25 /// Returns the center of the bounding volume.
26 fn center(&self) -> Self::Translation;
27
28 /// Returns the half size of the bounding volume.
29 fn half_size(&self) -> Self::HalfSize;
30
31 /// Computes the visible surface area of the bounding volume.
32 /// This method can be useful to make decisions about merging bounding volumes,
33 /// using a Surface Area Heuristic.
34 ///
35 /// For 2D shapes this would simply be the area of the shape.
36 /// For 3D shapes this would usually be half the area of the shape.
37 fn visible_area(&self) -> f32;
38
39 /// Checks if this bounding volume contains another one.
40 fn contains(&self, other: &Self) -> bool;
41
42 /// Computes the smallest bounding volume that contains both `self` and `other`.
43 fn merge(&self, other: &Self) -> Self;
44
45 /// Increases the size of the bounding volume in each direction by the given amount.
46 fn grow(&self, amount: impl Into<Self::HalfSize>) -> Self;
47
48 /// Decreases the size of the bounding volume in each direction by the given amount.
49 fn shrink(&self, amount: impl Into<Self::HalfSize>) -> Self;
50
51 /// Scale the size of the bounding volume around its center by the given amount
52 fn scale_around_center(&self, scale: impl Into<Self::HalfSize>) -> Self;
53
54 /// Transforms the bounding volume by first rotating it around the origin and then applying a translation.
55 fn transformed_by(
56 mut self,
57 translation: impl Into<Self::Translation>,
58 rotation: impl Into<Self::Rotation>,
59 ) -> Self {
60 self.transform_by(translation, rotation);
61 self
62 }
63
64 /// Transforms the bounding volume by first rotating it around the origin and then applying a translation.
65 fn transform_by(
66 &mut self,
67 translation: impl Into<Self::Translation>,
68 rotation: impl Into<Self::Rotation>,
69 ) {
70 self.rotate_by(rotation);
71 self.translate_by(translation);
72 }
73
74 /// Translates the bounding volume by the given translation.
75 fn translated_by(mut self, translation: impl Into<Self::Translation>) -> Self {
76 self.translate_by(translation);
77 self
78 }
79
80 /// Translates the bounding volume by the given translation.
81 fn translate_by(&mut self, translation: impl Into<Self::Translation>);
82
83 /// Rotates the bounding volume around the origin by the given rotation.
84 ///
85 /// The result is a combination of the original volume and the rotated volume,
86 /// so it is guaranteed to be either the same size or larger than the original.
87 fn rotated_by(mut self, rotation: impl Into<Self::Rotation>) -> Self {
88 self.rotate_by(rotation);
89 self
90 }
91
92 /// Rotates the bounding volume around the origin by the given rotation.
93 ///
94 /// The result is a combination of the original volume and the rotated volume,
95 /// so it is guaranteed to be either the same size or larger than the original.
96 fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>);
97}
98
99/// A trait that generalizes intersection tests against a volume.
100/// Intersection tests can be used for a variety of tasks, for example:
101/// - Raycasting
102/// - Testing for overlap
103/// - Checking if an object is within the view frustum of a camera
104pub trait IntersectsVolume<Volume: BoundingVolume> {
105 /// Check if a volume intersects with this intersection test
106 fn intersects(&self, volume: &Volume) -> bool;
107}
108
109mod bounded2d;
110pub use bounded2d::*;
111mod bounded3d;
112pub use bounded3d::*;
113
114mod raycast2d;
115pub use raycast2d::*;
116mod raycast3d;
117pub use raycast3d::*;