impact-rs

Crates.ioimpact-rs
lib.rsimpact-rs
version0.0.18
sourcesrc
created_at2024-02-26 08:59:49.084789
updated_at2024-10-29 14:23:03.552916
descriptionCollision query library for 2D games
homepage
repositoryhttps://github.com/piot/impact-rs
max_upload_size
id1153220
size155,409
Peter Bjorklund (piot)

documentation

README

Impact-rs

This crate provides utilities for performing collision queries between rectangles and rays, including swept checks for moving rectangles. It leverages fixed-point arithmetic provided by the fixed32 crate to handle the computations.

Features

  • Ray vs. Rect Collision Detection: Detect collisions between a ray and a rectangle, returning contact point, contact normal, and the closest time of collision.
  • Swept Rectangle Collision: Check for potential collisions as one rectangle moves towards another. Either using a delta 2D vector, or a scalar sweep in horizontal or vertical direction.
Swept Rectangle
  • Fixed-Point Precision: Uses fixed-point numbers (Fp) from the fixed32 crate for precise calculations without floating-point errors.

Usage

To use this crate in your project, add it to your Cargo.toml:

[dependencies]
impact_rs = "0.0.17"

Example

use fixed32::Fp;
use fixed32_math::{Rect, Vector};
use impact_rs::prelude::*;

fn main() {
  let ray_origin = Vector::from((1, 2));
  let ray_direction = Vector::from((3, 4));
  let target_rect = Rect::from((5, 6, 7, 8));

  let collision_result = ray_vs_rect(ray_origin, ray_direction, target_rect);

  if let Some(result) = collision_result {
    println!("Collision at: {:?}", result.contact_point);
    println!("Normal at collision: {:?}", result.contact_normal);
    println!("Closest collision time: {:?}", result.closest_time);
  } else {
    println!("No collision detected.");
  }
}
Commit count: 27

cargo fmt