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.

Collider may be built with the noisy-floats feature, which will use the R64 and N64 types from the noisy_float crate in place of f64 types. In either case, it is ultimately the user's responsibility to ensure that they do not do anything that will result in improper floating point overflow or NaN. For instructions for building a crate with a conditional feature, see http://doc.crates.io/specifying-dependencies.html#choosing-features.

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(-10.0, 0.0), Shape::new_square(2.0)));
hitbox.vel.pos = vec2(1.0, 0.0);
collider.add_hitbox(0, hitbox);

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

while collider.time() < 20.0 {
    let time = collider.next_time().min(20.0);
    collider.set_time(time);
    if let Some((event, id1, id2)) = collider.next() {
        println!("{:?} between hitbox {} and hitbox {} at time {}.", event, id1, id2, collider.time());

        if event == Event::Collide {
            println!("Speed of collided hitboxes is halved.");
            for id in [id1, id2].iter().cloned() {
                let mut hitbox = collider.get_hitbox(id);
                hitbox.vel.pos *= 0.5;
                collider.update_hitbox(id, hitbox);
            }
        }
    }
}

//the above loop prints the following events:
//  Collide between hitbox 0 and hitbox 1 at time 9.
//  Speed of collided hitboxes is halved.
//  Separate between hitbox 0 and hitbox 1 at time 13.01.

Modules

geom

Module containing geometry primitives.

inter

Contains types that describe interactions between hitboxes.

Structs

Collider

A structure that tracks hitboxes and returns collide/separate 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.