Struct gdnative::core_types::Aabb
source · Expand description
Axis-aligned bounding box.
Aabb
consists of a position, a size, and several utility functions. It is typically used for
fast overlap tests.
The 2D counterpart to Aabb
is Rect2
.
Fields§
§position: Vector3
The bounding box’s position in 3D space.
size: Vector3
Width, height, and depth of the bounding box.
Implementations§
source§impl Aabb
impl Aabb
sourcepub fn new(position: Vector3, size: Vector3) -> Aabb
pub fn new(position: Vector3, size: Vector3) -> Aabb
Creates an Aabb
by position and size.
Note that while size
components are allowed to be negative, they can lead to unintuitive results.
It is recommended to use abs
on such AABBs.
sourcepub fn set_end(&mut self, new_end: Vector3)
pub fn set_end(&mut self, new_end: Vector3)
Ending corner. Setting this value will change the size.
sourcepub fn abs(self) -> Aabb
pub fn abs(self) -> Aabb
Returns an Aabb
with equivalent position and area, modified so that the most-negative
corner is the origin and the size is positive.
sourcepub fn volume(self) -> f32
pub fn volume(self) -> f32
Returns the volume of the bounding box. See also has_no_volume
.
This method corresponds to the get_area
GDScript method.
sourcepub fn has_no_volume(self) -> bool
pub fn has_no_volume(self) -> bool
Returns true if the bounding box is flat or empty. See also
volume
.
This method corresponds to the has_no_area
GDScript method.
Note: If the bounding box has a negative size and is not flat or empty, this method will return true.
sourcepub fn has_no_surface(self) -> bool
pub fn has_no_surface(self) -> bool
Returns true if the bounding box is empty or all of its dimensions are negative.
sourcepub fn contains_point(self, point: Vector3) -> bool
pub fn contains_point(self, point: Vector3) -> bool
Returns true if the bounding box contains a point. By convention, the right and bottom edges of
the Rect2
are considered exclusive, so points on these edges are not included.
Note: This method is not reliable for bounding boxes with a negative size. Use
abs
to get a positive sized equivalent box to check for contained points.
sourcepub fn is_equal_approx(self, b: Aabb) -> bool
pub fn is_equal_approx(self, b: Aabb) -> bool
Returns true if this bounding box and b
are approximately equal, by calling
is_equal_approx
on each component.
sourcepub fn get_endpoint(self, index: usize) -> Option<Vector3>
pub fn get_endpoint(self, index: usize) -> Option<Vector3>
Gets the position of the 8 endpoints of the bounding box in space.
The index returns an arbitrary point, but all points are guaranteed to be unique.
sourcepub fn longest_axis(self) -> (Axis, f32)
pub fn longest_axis(self) -> (Axis, f32)
Returns the longest side of this AABB as an axis index and its length.
If multiple axes have the same length, then the first in order X, Y, Z is returned.
To get the unit vector along the axis, use Axis::to_unit_vector()
.
If you want to emulate the separate GDScript methods, you can do this:
let (index, size) = aabb.longest_axis();
let axis = index.to_unit_vector();
sourcepub fn shortest_axis(self) -> (Axis, f32)
pub fn shortest_axis(self) -> (Axis, f32)
Returns the shortest side of this AABB as an axis index and its length.
If multiple axes have the same length, then the first in order X, Y, Z is returned.
To get the unit vector along the axis, use Axis::to_unit_vector()
.
If you want to emulate the separate GDScript methods, you can do this:
let (index, size) = aabb.shortest_axis();
let axis = index.to_unit_vector();
sourcepub fn get_support(self, dir: Vector3) -> Vector3
pub fn get_support(self, dir: Vector3) -> Vector3
Returns the support point in a given direction. This is useful for collision detection algorithms.
The support point is a point on the boundary of the AABB, which is the furthest from the center in the given direction dir
.
In other words, when you cast a ray from the AABB’s center toward dir
and intersect that with the AABB boundary, you will get
the support point.
Mathematically, the support point corresponds to the point which maximizes its dot product with dir
.
See also 1 and 2 for more information.
sourcepub fn grow(self, by: f32) -> Aabb
pub fn grow(self, by: f32) -> Aabb
Returns a copy of the bounding box, grown a given amount of units on all 6 sides.
It is possible to specify a negative amount to shrink the AABB (note that this can invert the AABB).
sourcepub fn intersects(self, b: Aabb) -> bool
pub fn intersects(self, b: Aabb) -> bool
Returns true if the bounding box overlaps with b
.
This excludes borders; if the intersection has no volume, false
is returned.
sourcepub fn intersects_plane(self, plane: Plane) -> bool
pub fn intersects_plane(self, plane: Plane) -> bool
Returns true if the bounding box is on both sides of a plane.
sourcepub fn intersects_segment(self, from: Vector3, to: Vector3) -> bool
pub fn intersects_segment(self, from: Vector3, to: Vector3) -> bool
Returns true if the bounding box intersects the line segment between from
and to
.
sourcepub fn intersection(self, b: Aabb) -> Option<Aabb>
pub fn intersection(self, b: Aabb) -> Option<Aabb>
Returns the intersection between two bounding boxes, or None
if there is no intersection.
This excludes borders; if the intersection has no volume, None
is returned.