basic-collision 0.3.0

Extremely basic collision library that just works
Documentation
  • Coverage
  • 9.09%
    1 out of 11 items documented1 out of 10 items with examples
  • Size
  • Source code size: 4.8 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 330.12 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ghost_93153

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!");
        }
    }
}