Skip to main content

il2cpp_bridge_rs/structs/components/physics/
collider.rs

1//! Unity Collider component wrapper
2use crate::structs::components::{Component, ComponentTrait};
3use crate::structs::math::Bounds;
4use crate::structs::Vector3;
5use std::ffi::c_void;
6use std::ops::Deref;
7
8#[repr(C)]
9#[derive(Debug, Clone, Copy)]
10pub struct Collider {
11    /// Base Component structure
12    pub component: Component,
13}
14
15impl ComponentTrait for Collider {
16    fn from_ptr(ptr: *mut c_void) -> Self {
17        Self {
18            component: Component::from_ptr(ptr),
19        }
20    }
21}
22
23impl Deref for Collider {
24    type Target = Component;
25    fn deref(&self) -> &Self::Target {
26        &self.component
27    }
28}
29
30impl Collider {
31    /// Creates a Collider from a raw pointer
32    ///
33    /// # Arguments
34    /// * `ptr` - The raw pointer to the Collider
35    ///
36    /// # Returns
37    /// * `Self` - The created Collider wrapper
38    pub unsafe fn from_ptr(ptr: *mut c_void) -> Self {
39        <Self as ComponentTrait>::from_ptr(ptr)
40    }
41
42    /// Gets the bounding box of the collider
43    ///
44    /// # Returns
45    /// * `Result<Bounds, String>` - The world-space bounding box
46    pub fn get_bounds(&self) -> Result<Bounds, String> {
47        let mut bounds = Bounds::default();
48        unsafe {
49            self.method("get_bounds_Injected")
50                .ok_or("Method 'get_bounds_Injected' not found")?
51                .call::<()>(&[&mut bounds as *mut Bounds as *mut c_void])?;
52        }
53        Ok(bounds)
54    }
55    /// Gets enabled state
56    ///
57    /// # Returns
58    /// * `Result<bool, String>` - True if the collider is enabled
59    pub fn get_enabled(&self) -> Result<bool, String> {
60        unsafe {
61            self.method("get_enabled")
62                .ok_or("Method 'get_enabled' not found")?
63                .call::<bool>(&[])
64        }
65    }
66
67    /// Sets enabled state
68    ///
69    /// # Arguments
70    /// * `value` - The new enabled state
71    ///
72    /// # Returns
73    /// * `Result<(), String>` - Ok if success
74    pub fn set_enabled(&self, value: bool) -> Result<(), String> {
75        unsafe {
76            let mut value_cp = value;
77            self.method("set_enabled")
78                .ok_or("Method 'set_enabled' not found")?
79                .call::<()>(&[&mut value_cp as *mut bool as *mut c_void])?;
80            Ok(())
81        }
82    }
83
84    /// Gets isTrigger state
85    ///
86    /// # Returns
87    /// * `Result<bool, String>` - True if the collider is a trigger
88    pub fn get_is_trigger(&self) -> Result<bool, String> {
89        unsafe {
90            self.method("get_isTrigger")
91                .ok_or("Method 'get_isTrigger' not found")?
92                .call::<bool>(&[])
93        }
94    }
95
96    /// Sets isTrigger state
97    ///
98    /// # Arguments
99    /// * `value` - The new isTrigger state
100    ///
101    /// # Returns
102    /// * `Result<(), String>` - Ok if success
103    pub fn set_is_trigger(&self, value: bool) -> Result<(), String> {
104        unsafe {
105            let mut value_cp = value;
106            self.method("set_isTrigger")
107                .ok_or("Method 'set_isTrigger' not found")?
108                .call::<()>(&[&mut value_cp as *mut bool as *mut c_void])?;
109            Ok(())
110        }
111    }
112
113    /// Gets attached Rigidbody
114    ///
115    /// # Returns
116    /// * `Result<Rigidbody, String>` - The rigidbody attached to this collider (or the one it belongs to)
117    pub fn get_attached_rigidbody(&self) -> Result<super::rigidbody::Rigidbody, String> {
118        unsafe {
119            let ptr = self
120                .method("get_attachedRigidbody")
121                .ok_or("Method 'get_attachedRigidbody' not found")?
122                .call::<*mut c_void>(&[])?;
123
124            if ptr.is_null() {
125                return Err("No attached Rigidbody".to_string());
126            }
127
128            Ok(super::rigidbody::Rigidbody::from_ptr(ptr))
129        }
130    }
131
132    /// Gets the closest point on the collider to a given position
133    ///
134    /// # Arguments
135    /// * `position` - The position to find the closest point to
136    ///
137    /// # Returns
138    /// * `Result<Vector3, String>` - The closest point on the collider surface
139    pub fn closest_point(&self, position: Vector3) -> Result<Vector3, String> {
140        unsafe {
141            let mut pos_cp = position;
142            let result = self
143                .method("ClosestPoint")
144                .ok_or("Method 'ClosestPoint' not found")?
145                .call::<Vector3>(&[&mut pos_cp as *mut Vector3 as *mut c_void])?;
146            Ok(result)
147        }
148    }
149}