HyperCollision

1 min read Original article ↗

HyperCollision is a Rust library for computing collision and proximity between convex shapes embedded in any dimensional euclidean space.

To use, simply define a shape struct that satisfies the ConvexShape::Support trait and use the GJK algorithm out of the box. A hypersphere struct is defined in tests as an example:

pub struct HyperSphere<const DIMENSION: usize> {
        pub center: Point<f64, DIMENSION>,
        pub radius: f64
}

impl<const DIMENSION: usize> Support<DIMENSION> for HyperSphere<DIMENSION> {

    fn support_function(&self, support_vector:SVector<f64, DIMENSION>) -> Point<f64, DIMENSION> {
        return self.center + (self.radius * support_vector.normalize());
    }

}

fn hypersphere_distant_test() {

    const DIMENSION:usize = 3;

    let sphere_1: HyperSphere<DIMENSION> = HyperSphere {center: Point::from(SVector::<f64, DIMENSION>::from_element(5.0)), radius:3.0};
    let sphere_2: HyperSphere<DIMENSION> = HyperSphere {center: Point::from(SVector::<f64, DIMENSION>::from_element(-5.0)), radius:3.0};

    let rotation:Rotation<f64, DIMENSION> = Rotation::identity();

    let translation:Translation<f64, DIMENSION> = Translation::identity();

    let result = gjk_collision(&sphere_1, rotation, translation, &sphere_2, rotation, translation, 1000);

    assert!(matches!(result, GJKResult::NoCollision));


}

This paper provides a detailed mathematical background on the algorithm.

Bug reports and pull requests are appreciated.