collide_with_boundary

Function collide_with_boundary 

Source
pub fn collide_with_boundary(
    bounding_box: &Rect,
    boundary: &Rect,
) -> Option<BounceDirection>
Expand description

A function to check collision between some moving object with a bounding box and the edges of some boundary. If the boundary rectangle does not fully contain the bounding box, return the direction the object must bounce to stay within the boundary.

ยงExample

use rust_training_tool::collision;
use rust_training_tool::egui::{Pos2, Rect, Vec2};
let boundary = Rect::from_center_size(Pos2::ZERO, Vec2::new(10.0, 10.0));

// boundary box is inside the boundary rect
let mut bounding_box = Rect::from_center_size(Pos2::ZERO, Vec2::new(1.0, 1.0));
assert!(collision::collide_with_boundary(&bounding_box, &boundary).is_none());

// move bounding box so it intersects the boundary on the right
bounding_box.set_center(Pos2::new(9.1, 0.0));
assert!(matches!(
    collision::collide_with_boundary(&bounding_box, &boundary),
    Some(collision::BounceDirection::Left)
));