rust_raylib/
collision.rs

1use crate::{
2    ffi,
3    math::{BoundingBox, Matrix, Ray, RayCollision, Rectangle, Vector2, Vector3},
4    model::Mesh,
5};
6
7/// Check collision between two rectangles
8#[inline]
9pub fn check_collision_rects(rec1: Rectangle, rec2: Rectangle) -> bool {
10    unsafe { ffi::CheckCollisionRecs(rec1.into(), rec2.into()) }
11}
12
13/// Check collision between two circles
14#[inline]
15pub fn check_collision_circles(
16    center1: Vector2,
17    radius1: f32,
18    center2: Vector2,
19    radius2: f32,
20) -> bool {
21    unsafe { ffi::CheckCollisionCircles(center1.into(), radius1, center2.into(), radius2) }
22}
23
24/// Check collision between circle and rectangle
25#[inline]
26pub fn check_collision_circle_rect(center: Vector2, radius: f32, rec: Rectangle) -> bool {
27    unsafe { ffi::CheckCollisionCircleRec(center.into(), radius, rec.into()) }
28}
29
30/// Check if point is inside rectangle
31#[inline]
32pub fn check_point_inside_rect(point: Vector2, rec: Rectangle) -> bool {
33    unsafe { ffi::CheckCollisionPointRec(point.into(), rec.into()) }
34}
35
36/// Check if point is inside circle
37#[inline]
38pub fn check_point_inside_circle(point: Vector2, center: Vector2, radius: f32) -> bool {
39    unsafe { ffi::CheckCollisionPointCircle(point.into(), center.into(), radius) }
40}
41
42/// Check if point is inside a triangle
43#[inline]
44pub fn check_point_inside_triangle(point: Vector2, p1: Vector2, p2: Vector2, p3: Vector2) -> bool {
45    unsafe { ffi::CheckCollisionPointTriangle(point.into(), p1.into(), p2.into(), p3.into()) }
46}
47
48/// Check if point is within a polygon described by array of vertices
49#[inline]
50pub fn check_point_inside_polygon(point: Vector2, points: &[Vector2]) -> bool {
51    unsafe {
52        ffi::CheckCollisionPointPoly(point.into(), points.as_ptr() as *mut _, points.len() as _)
53    }
54}
55
56/// Check the collision between two lines defined by two points each, returns collision point
57#[inline]
58pub fn check_collision_lines(
59    start_pos1: Vector2,
60    end_pos1: Vector2,
61    start_pos2: Vector2,
62    end_pos2: Vector2,
63) -> Option<Vector2> {
64    let mut coll_pt: ffi::Vector2 = ffi::Vector2 { x: 0., y: 0. };
65
66    if unsafe {
67        ffi::CheckCollisionLines(
68            start_pos1.into(),
69            end_pos1.into(),
70            start_pos2.into(),
71            end_pos2.into(),
72            &mut coll_pt as *mut _,
73        )
74    } {
75        Some(coll_pt.into())
76    } else {
77        None
78    }
79}
80
81/// Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
82#[inline]
83pub fn check_collision_point_line(
84    point: Vector2,
85    p1: Vector2,
86    p2: Vector2,
87    threshold: u32,
88) -> bool {
89    unsafe { ffi::CheckCollisionPointLine(point.into(), p1.into(), p2.into(), threshold as _) }
90}
91
92/// Get collision rectangle for two rectangles collision
93#[inline]
94pub fn get_collision_rect(rec1: Rectangle, rec2: Rectangle) -> Rectangle {
95    unsafe { ffi::GetCollisionRec(rec1.into(), rec2.into()).into() }
96}
97
98/// Check collision between two spheres
99#[inline]
100pub fn check_collision_spheres(
101    center1: Vector3,
102    radius1: f32,
103    center2: Vector3,
104    radius2: f32,
105) -> bool {
106    unsafe { ffi::CheckCollisionSpheres(center1.into(), radius1, center2.into(), radius2) }
107}
108
109/// Check collision between two bounding boxes
110#[inline]
111pub fn check_collision_boxes(box1: BoundingBox, box2: BoundingBox) -> bool {
112    unsafe { ffi::CheckCollisionBoxes(box1.into(), box2.into()) }
113}
114
115/// Check collision between box and sphere
116#[inline]
117pub fn check_collision_box_sphere(bbox: BoundingBox, center: Vector3, radius: f32) -> bool {
118    unsafe { ffi::CheckCollisionBoxSphere(bbox.into(), center.into(), radius) }
119}
120
121/// Get collision info between ray and sphere
122#[inline]
123pub fn get_ray_collision_sphere(ray: Ray, center: Vector3, radius: f32) -> RayCollision {
124    unsafe { ffi::GetRayCollisionSphere(ray.into(), center.into(), radius).into() }
125}
126
127/// Get collision info between ray and box
128#[inline]
129pub fn get_ray_collision_box(ray: Ray, bbox: BoundingBox) -> RayCollision {
130    unsafe { ffi::GetRayCollisionBox(ray.into(), bbox.into()).into() }
131}
132
133/// Get collision info between ray and mesh
134#[inline]
135pub fn get_ray_collision_mesh(ray: Ray, mesh: Mesh, transform: Matrix) -> RayCollision {
136    unsafe { ffi::GetRayCollisionMesh(ray.into(), mesh.raw.clone(), transform.into()).into() }
137}
138
139/// Get collision info between ray and triangle
140#[inline]
141pub fn get_ray_collision_triangle(ray: Ray, p1: Vector3, p2: Vector3, p3: Vector3) -> RayCollision {
142    unsafe { ffi::GetRayCollisionTriangle(ray.into(), p1.into(), p2.into(), p3.into()).into() }
143}
144
145/// Get collision info between ray and quad
146#[inline]
147pub fn get_ray_collision_quad(
148    ray: Ray,
149    p1: Vector3,
150    p2: Vector3,
151    p3: Vector3,
152    p4: Vector3,
153) -> RayCollision {
154    unsafe {
155        ffi::GetRayCollisionQuad(ray.into(), p1.into(), p2.into(), p3.into(), p4.into()).into()
156    }
157}