Collision
The collision module provides pure-data helpers for detecting overlap between
2D and 3D shapes. All functions are free functions (or methods on geometry
types) — no window, no GPU, no handle required. They map directly to raylib’s
CheckCollision* and GetRayCollision* family.
API surface
Rectangle::check_collision_recs— check overlap between twoRectangles.Rectangle::check_collision_point_rec— check if a point is inside a rectangle.Rectangle::check_collision_circle_rec— check if a circle overlaps a rectangle.Rectangle::get_collision_rec— compute the intersection rectangle of two colliding rectangles (Noneif they don’t overlap).check_collision_circles— check overlap between two circles.check_collision_point_circle— check if a point is inside a circle.check_collision_point_triangle— check if a point is inside a triangle.check_collision_point_line— check if a point is within a threshold distance of a line segment.check_collision_lines— check if two line segments intersect; returnsOption<Vector2>with the intersection point.check_collision_spheres— 3D sphere–sphere overlap check.BoundingBox::check_collision_boxes— 3D axis-aligned bounding box overlap check.
Example
Rectangle and circle collision — pure Rust, no window needed.
#![allow(unused)]
fn main() {
extern crate raylib;
use raylib::math::Rectangle;
use raylib::core::collision::{check_collision_circles, check_collision_lines};
use raylib::math::Vector2;
// Two overlapping rectangles
let r1 = Rectangle::new(0.0, 0.0, 10.0, 10.0);
let r2 = Rectangle::new(5.0, 5.0, 10.0, 10.0);
assert!(r1.check_collision_recs(r2), "overlapping rects should collide");
// A separated rectangle pair
let r3 = Rectangle::new(20.0, 20.0, 5.0, 5.0);
assert!(!r1.check_collision_recs(r3), "non-overlapping rects should not collide");
// Point inside rectangle
assert!(r1.check_collision_point_rec(Vector2::new(3.0, 3.0)));
assert!(!r1.check_collision_point_rec(Vector2::new(15.0, 15.0)));
// Two overlapping circles (centers 1 apart, radii sum = 4)
assert!(check_collision_circles(
Vector2::new(0.0, 0.0), 2.0,
Vector2::new(1.0, 0.0), 2.0,
));
// Line intersection
let hit = check_collision_lines(
Vector2::new(-1.0, -1.0), Vector2::new(1.0, 1.0),
Vector2::new(-1.0, 1.0), Vector2::new(1.0, -1.0),
);
assert!(hit.is_some(), "diagonal lines should intersect");
let pt = hit.unwrap();
assert!((pt.x).abs() < 1e-4 && (pt.y).abs() < 1e-4);
}
Gotchas
check_collision_recsis a method onRectangle, not a free function. Call it asrect.check_collision_recs(other). Similarly,check_collision_point_rec,check_collision_circle_rec, andget_collision_recare methods onRectangle.check_collision_linesreturnsOption<Vector2>, notbool. Use.is_some()to test for collision and.unwrap()to read the intersection point.- Touch counts as collision. Circles whose edges exactly touch (distance equals the sum of radii) are considered colliding; this matches raylib’s C behaviour.
See also
- raymath —
Vector2/Vector3/BoundingBoxtypes used here. - Shapes — drawing the same geometry you’re testing.
check_collision_recsdocs.rs
Showcase examples
Showcase examples that exercise this module: