Expand description

Crates.io MIT/Apache 2.0

sepax2d

A safe Rust crate for finding and resolving collisions of convex shapes using the Separating Axis Theorem (SAT) in two dimensions.

Usage

Add the following to the [dependencies] section of your Cargo.toml:

sepax2d = "0.3"

Import the types of shapes you’d like to use and create some new shapes:

use sepax2d::prelude::*;

let rectangle = AABB::new(top_left, width, height);
let circle = Circle::new(center, radius);
let capsule = Capsule::new(center, arm, radius);
let parallelogram = Parallelogram::new(position, u_vector, v_vector);

//The vertices of a polygon are position vectors
//relative to the shape's position, i.e. if position
//is (1, 2), then the absolute location of the
//first vertex is (1, 0).
let triangle = Polygon::from_vertices
(

    position,
    vec![(0.0, -2.0), (-1.0, 2.0), (1.0, 2.0)]

);

Use the sat_overlap(&left, &right) method to get a bool indicating whether or not the two given shapes overlap. Any struct implementing the Shape trait can be used.

assert!(sat_overlap(&circle, &capsule));
assert!(!sat_overlap(&triangle, &rectangle));

Use the sat_collision(&left, &right) method to get a (f32, f32) which represents the shift needed to add to right’s position in order to resolve the overlap of the two shapes.

let resolution = sat_collision(&circle, &triangle);

let position = triangle.position();
triangle.set_position((position.0 + resolution.0, position.1 + resolution.1));

assert!(!sat_overlap(&circle, &triangle));

Use the contains_point(&shape, point) method to get a bool indicating whether or not the specified point is inside the given shape.


let rect = AABB::new((0.0, 0.0), 5.0, 2.0);

assert!(contains_point(&rect, (1.0, 1.0)));
assert!(!contains_point(&rect, (10.0, -1.0)));

Polygon, Circle, Capsule, and Parallelogram shapes implement the Rotate trait, which allows you to rotate them around their position.


let mut triangle = Polygon::from_vertices
(

   position,
   vec![(-1.0, 0.0), (0.0, 2.0), (1.0, 0.0)]

);

triangle.rotate(std::f32::consts::FRAC_PI_2)
//New vertices: [(0.0, -1.0), (-2.0, 0.0), (0.0, 1.0)]

You can use the intersects_line, intersects_ray, and intersects_segment methods to check whether a shape intersects with the corresponding type of line.


let triangle = Polygon::from_vertices((0.0, vec![(0.0, 0.0), (1.0, 1.0), (-1.0, 1.0)]));

assert!(intersects_segment(&triangle, (2.0, 0.5), (-2.0, 0.5)));

Features

Enable the serde feature for (De)Serialization of supported shapes!

Modules

Macros

Traits

A trait indicating that a shape can be rotated around its position. Applicable to all shapes other than AABB.

A trait describing the behavior needed to implement SAT overlap and collision for a given shape.

Functions

Returns true if the given shape contains the specified point, and false if it does not. Does not work for degenerate polygons.

Returns the vector that needs to be added to the second shape’s position to resolve a collision with the first shape. Does not work for degenerate shapes.

Returns true if the given shapes overlap, and false if they do not. Does not work for degenerate shapes.