pub struct Rect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}Expand description
§Rect
Contains the position (x, y) and size (width, height) of a rectangle. Can be used for detecting AABB collisions or drawing rectangles.
§Examples
let my_rect = Rect::new(0.0, 0.0, 50.0, 50.0);Drawing a Rect to the screen:
my_rect.draw(RED);
// or
draw_rectangle_from_rect(my_rect, RED);Check collisions:
if my_rect.contains_point(mouse_position_vec2()) {
println!("Mouse is inside the rect")
}
let aabb_intersection: Option<Rect> = my_rect.intersection(other_rect);Fields§
§x: f32§y: f32§w: f32§h: f32Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self
pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self
Makes a new Rect object with the x, y position and Width x Height
Sourcepub fn from_corners(a: Vector2, b: Vector2) -> Self
pub fn from_corners(a: Vector2, b: Vector2) -> Self
Build a Rect from two corner points (handles any order)
Sourcepub fn from_center(center: Vector2, w: f32, h: f32) -> Self
pub fn from_center(center: Vector2, w: f32, h: f32) -> Self
Build a Rect centered on center with the given size
pub const fn left(&self) -> f32
pub const fn right(&self) -> f32
pub const fn top(&self) -> f32
pub const fn bottom(&self) -> f32
pub const fn top_left(&self) -> Vector2
pub const fn top_right(&self) -> Vector2
pub const fn bottom_left(&self) -> Vector2
pub const fn bottom_right(&self) -> Vector2
pub const fn xy(&self) -> Vector2
pub const fn wh(&self) -> Vector2
pub const fn xw(&self) -> Vector2
pub const fn xh(&self) -> Vector2
pub const fn yw(&self) -> Vector2
pub const fn yh(&self) -> Vector2
pub fn corners(&self) -> [Vector2; 4]
pub fn center(&self) -> Vector2
pub const fn area(&self) -> f32
pub const fn perimeter(&self) -> f32
pub const fn size(&self) -> Vector2
pub const fn position(&self) -> Vector2
Sourcepub fn with_position(&self, pos: Vector2) -> Self
pub fn with_position(&self, pos: Vector2) -> Self
Get a new Rect with the specified position
Sourcepub fn with_position_xy(&self, x: f32, y: f32) -> Self
pub fn with_position_xy(&self, x: f32, y: f32) -> Self
Get a new Rect with the specified position
Sourcepub fn change_position_by(&mut self, offset: Vector2)
pub fn change_position_by(&mut self, offset: Vector2)
Changes the position relatively by an offset Vector2.
Sourcepub fn change_size_by(&mut self, offset: Vector2)
pub fn change_size_by(&mut self, offset: Vector2)
Changes the size relatively by an offset Vector2.
Sourcepub fn translated(&self, offset: Vector2) -> Self
pub fn translated(&self, offset: Vector2) -> Self
Get a moved Rect by an offset
Sourcepub fn centered_at(&self, center: Vector2) -> Self
pub fn centered_at(&self, center: Vector2) -> Self
Move the rect so its center is at center
Sourcepub fn inflated(&self, amount: f32) -> Self
pub fn inflated(&self, amount: f32) -> Self
Grow/shrink the rect on all sides by amount (negative shrinks), keeping center fixed
Sourcepub const fn scaled(&self, factor: f32) -> Self
pub const fn scaled(&self, factor: f32) -> Self
Scale width/height around the top-left corner (position unchanged)
Sourcepub fn scaled_from_center(&self, factor: f32) -> Self
pub fn scaled_from_center(&self, factor: f32) -> Self
Scale width/height around the rect’s center
Sourcepub fn union(&self, other: &Rect) -> Self
pub fn union(&self, other: &Rect) -> Self
Smallest rect that fully contains both self and other
Sourcepub fn clamp_point(&self, point: Vector2) -> Vector2
pub fn clamp_point(&self, point: Vector2) -> Vector2
Clamp a point to lie within this rect
Sourcepub fn normalized(&self) -> Self
pub fn normalized(&self) -> Self
Fix a rect with negative width/height into one with positive dimensions
pub fn to_array(&self) -> [f32; 4]
Sourcepub const fn contains_point(&self, x: f32, y: f32) -> bool
pub const fn contains_point(&self, x: f32, y: f32) -> bool
Test if point (x, y) lies inside/on the Rect
Sourcepub const fn contains_point_fully(&self, x: f32, y: f32) -> bool
pub const fn contains_point_fully(&self, x: f32, y: f32) -> bool
Test if point (x, y) lies inside/on the Rect
Sourcepub const fn contains_point_vec2(&self, point: Vector2) -> bool
pub const fn contains_point_vec2(&self, point: Vector2) -> bool
Test if point (Vector2) lies inside/on the Rect
Sourcepub const fn contains_point_fully_vec2(&self, point: Vector2) -> bool
pub const fn contains_point_fully_vec2(&self, point: Vector2) -> bool
Test if point (Vector2) lies inside/on the Rect
Sourcepub fn contains_mouse(&self) -> bool
pub fn contains_mouse(&self) -> bool
Test if mouse lies inside/on the Rect
Sourcepub fn contains_mouse_fully(&self) -> bool
pub fn contains_mouse_fully(&self) -> bool
Test if mouse lies inside/on the Rect
Sourcepub const fn intersects(&self, other: &Rect) -> bool
pub const fn intersects(&self, other: &Rect) -> bool
AABB overlap test
Sourcepub const fn intersection(&self, other: Rect) -> Option<Rect>
pub const fn intersection(&self, other: Rect) -> Option<Rect>
Returns the overlapping region, or None if they don’t intersect
Sourcepub const fn contains_rect_fully(&self, other: &Rect) -> bool
pub const fn contains_rect_fully(&self, other: &Rect) -> bool
True if self fully contains other
Sourcepub fn update(&mut self, new_rect: Rect) -> Rect
pub fn update(&mut self, new_rect: Rect) -> Rect
Updates the original rect with a new one and returns it as well
pub fn draw_rounded(&self, radius: f32, color: Color)
pub fn draw_rounded_ex( &self, top_left: f32, top_right: f32, bottom_left: f32, bottom_right: f32, color: Color, )
Sourcepub fn draw_lines(&self, thickness: f32, color: Color)
pub fn draw_lines(&self, thickness: f32, color: Color)
Draw the Rect’s outline to the screen using a color and thickness
pub fn draw_lines_rounded(&self, radius: f32, thickness: f32, color: Color)
pub fn draw_lines_rounded_ex( &self, top_left: f32, top_right: f32, bottom_left: f32, bottom_right: f32, thickness: f32, color: Color, )
Source§impl Rect
impl Rect
Sourcepub fn resolve_collision(&mut self, other: Rect) -> Option<Vector2>
pub fn resolve_collision(&mut self, other: Rect) -> Option<Vector2>
Pushes self out of an axis-aligned other.
Returns the collision normal (direction self was pushed), or None.
Sourcepub fn resolve_collision_obb(
&mut self,
angle: f32,
other: &Rect,
other_angle: f32,
) -> Option<Vector2>
pub fn resolve_collision_obb( &mut self, angle: f32, other: &Rect, other_angle: f32, ) -> Option<Vector2>
Pushes self out of a rotated other (SAT).
Returns the collision normal (direction self was pushed), or None.
Sourcepub fn intersection_obb(
&self,
angle: f32,
other: &Rect,
other_angle: f32,
) -> Option<Vector2>
pub fn intersection_obb( &self, angle: f32, other: &Rect, other_angle: f32, ) -> Option<Vector2>
OBB vs OBB collision (SAT). Angles are in radians, each rect rotates around its own center.
Returns the minimum translation vector that pushes self out of other, or None if not colliding.
Sourcepub fn intersects_obb(&self, angle: f32, other: &Rect, other_angle: f32) -> bool
pub fn intersects_obb(&self, angle: f32, other: &Rect, other_angle: f32) -> bool
Bool-only OBB overlap test
Sourcepub fn corners_by_angle(&self, angle: f32) -> [Vector2; 4]
pub fn corners_by_angle(&self, angle: f32) -> [Vector2; 4]
Corners of the rect rotated by angle (radians) around its center
Sourcepub fn draw_rotated(&self, angle: f32, color: Color)
pub fn draw_rotated(&self, angle: f32, color: Color)
Draw the Rect rotated by angle (radians) around its center
Sourcepub fn draw_lines_rotated(&self, angle: f32, thickness: f32, color: Color)
pub fn draw_lines_rotated(&self, angle: f32, thickness: f32, color: Color)
Draw the rotated Rect’s outline
Trait Implementations§
impl Copy for Rect
Source§impl PartialOrd for Rect
impl PartialOrd for Rect
impl StructuralPartialEq for Rect
Auto Trait Implementations§
impl Freeze for Rect
impl RefUnwindSafe for Rect
impl Send for Rect
impl Sync for Rect
impl Unpin for Rect
impl UnsafeUnpin for Rect
impl UnwindSafe for Rect
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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<T> DowncastSync for T
impl<T> DowncastSync for T
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more