Crate collider [] [src]

Collider is a library for continuous 2D collision detection, for use with game developement.

Most game engines follow the approach of periodically updating the positions of all shapes and checking for collisions at a frozen snapshot in time. Continuous collision detection, on the other hand, means that the time of collision is determined very precisely, and the user is not restricted to a fixed time-stepping method. There are currently two kinds of shapes supported by Collider: circles and rectangles. The user specifies the positions and velocites of these shapes, which they can update at any time, and Collider will solve for the precise times of collision and separation.

There are certain advantages that continuous collision detection holds over the traditional approach. In a game engine, the position of a sprite may be updated to overlap a wall, and in a traditional collision system there would need to be a post-correction to make sure the sprite does not appear inside of the wall. This is not needed with continuous collision detection, since the precise time and location at which the sprite touches the wall is known. Traditional collision detection may have an issue with "tunneling," in which a fast small object runs into a narrow wall and collision detection misses it, or two fast small objects fly right through each other and collision detection misses it. This is also not a problem for contiuous collision detection. It is also debatable that continuous collision detection may be more efficient in certain circumstances, since the hitboxes may be updated less frequently and still maintain a smooth appearance over time.

Example

use collider::{Collider, Hitbox, Event};
use collider::geom::{PlacedShape, Shape, Vec2};
 
let mut collider: Collider = Collider::new(4.0, 0.01);

let mut hitbox = Hitbox::new(PlacedShape::new(Vec2::new(-10.0, 0.0), Shape::new_rect(2.0, 2.0)));
hitbox.vel.pos = Vec2::new(1.0, 0.0);
collider.add_hitbox(0, hitbox);
 
let mut hitbox = Hitbox::new(PlacedShape::new(Vec2::new(10.0, 0.0), Shape::new_circle(2.0)));
hitbox.vel.pos = Vec2::new(-1.0, 0.0);
collider.add_hitbox(1, hitbox);

let mut time = 0.0;
while time < 9.0 {
    assert!(collider.next() == None);
    let timestep = collider.time_until_next();
    collider.advance(timestep);
    time += timestep;
}
assert!(time == 9.0);
assert!(collider.next() == Some((Event::Collide, 0, 1)));

Modules

geom

Module containing geometry primitives.

inter

Contains types that describe interactions between hitboxes.

Structs

Collider

A structure that tracks hitboxes and returns collision/separation events.

Hitbox

Represents a moving shape for continuous collision testing.

Enums

Event

An event type that may be returned from a Collider instance.

Type Definitions

HitboxId

Type used as a handle for referencing hitboxes in a Collider instance.