[][src]Crate basic_collision

This crate serves the purpose of being an extremely simple collision library. Only features AABB collision for now. For how to use, see example below. Basically, all you need to do is implement the Object trait for all your structs that you want to have collision. From the Object trait you need to implement the fn boundary(&self) -> Boundary function. Then your structs will have the fn does_collide(&self, objects: &[Box<dyn Object>]) -> bool function included.

use basic_collision::*;
 
#[derive(Debug)]
pub struct Block {
    x: f32,
    y: f32,
    width: f32,
    height: f32,
}
 
impl Block {
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Block {
        Block {
            x, y, width, height
        }
    }
}
 
impl Object for Block {
    fn boundary(&self) -> Boundary {
        Boundary::new(self.x, self.y, self.width, self.height)
    }
}
 
fn main() {
    let blocks: Vec<Box<dyn Object>> = vec![
        Box::new(Block::new(100., 150., 60., 50.)),
        Box::new(Block::new(150., 150., 40., 50.)),
        Box::new(Block::new(200., 150., 60., 50.)),
    ];
    for block in blocks.iter() {
        if block.does_collide(&blocks[..]) {
            println!("Collision occurred!");
        }
    }
}

Structs

Boundary

Traits

Object