Skip to main content

il2cpp_bridge_rs/structs/components/physics/
rigidbody.rs

1//! Unity Rigidbody component wrapper
2use crate::structs::components::{Component, ComponentTrait};
3use crate::structs::Vector3;
4use std::ffi::c_void;
5use std::ops::Deref;
6
7#[repr(C)]
8#[derive(Debug, Clone, Copy)]
9pub struct Rigidbody {
10    /// Base Component structure
11    pub component: Component,
12}
13
14impl ComponentTrait for Rigidbody {
15    fn from_ptr(ptr: *mut c_void) -> Self {
16        Self {
17            component: Component::from_ptr(ptr),
18        }
19    }
20}
21
22impl Deref for Rigidbody {
23    type Target = Component;
24    fn deref(&self) -> &Self::Target {
25        &self.component
26    }
27}
28
29#[repr(i32)]
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum ForceMode {
32    /// Applies a continuous force to the rigidbody, using its mass
33    Force = 0,
34    /// Applies a continuous acceleration to the rigidbody, ignoring its mass
35    Acceleration = 5,
36    /// Applies an instant force impulse to the rigidbody, using its mass
37    Impulse = 1,
38    /// Applies an instant velocity change to the rigidbody, ignoring its mass
39    VelocityChange = 2,
40}
41
42impl Rigidbody {
43    /// Checks if collision detection is enabled
44    ///
45    /// # Returns
46    /// * `Result<bool, String>` - True if collision detection is enabled
47    pub fn get_detect_collisions(&self) -> Result<bool, String> {
48        unsafe {
49            let result = self
50                .method("get_detectCollisions")
51                .ok_or("Method 'get_detectCollisions' not found")?
52                .call::<bool>(&[])?;
53            Ok(result)
54        }
55    }
56
57    /// Sets whether collision detection is enabled
58    ///
59    /// # Arguments
60    /// * `value` - True to enable collision detection
61    ///
62    /// # Returns
63    /// * `Result<(), String>` - Ok if success
64    pub fn set_detect_collisions(&self, value: bool) -> Result<(), String> {
65        unsafe {
66            self.method("set_detectCollisions")
67                .ok_or("Method 'set_detectCollisions' not found")?
68                .call::<()>(&[&value as *const bool as *mut c_void])?;
69            Ok(())
70        }
71    }
72
73    /// Gets the velocity of the rigidbody
74    ///
75    /// # Returns
76    /// * `Result<Vector3, String>` - The velocity vector
77    pub fn get_velocity(&self) -> Result<Vector3, String> {
78        unsafe {
79            let result = self
80                .method("get_velocity")
81                .ok_or("Method 'get_velocity' not found")?
82                .call::<Vector3>(&[])?;
83            Ok(result)
84        }
85    }
86
87    /// Sets the velocity of the rigidbody
88    ///
89    /// # Arguments
90    /// * `value` - The new velocity vector
91    ///
92    /// # Returns
93    /// * `Result<(), String>` - Ok if success
94    pub fn set_velocity(&self, value: Vector3) -> Result<(), String> {
95        unsafe {
96            self.method("set_velocity")
97                .ok_or("Method 'set_velocity' not found")?
98                .call::<()>(&[&value as *const Vector3 as *mut c_void])?;
99            Ok(())
100        }
101    }
102    /// Adds a force to the rigidbody
103    ///
104    /// # Arguments
105    /// * `force` - The force vector to apply
106    /// * `mode` - The ForceMode to use
107    ///
108    /// # Returns
109    /// * `Result<(), String>` - Ok if success
110    pub fn add_force(&self, force: Vector3, mode: ForceMode) -> Result<(), String> {
111        unsafe {
112            let mut force_cp = force;
113            let mut mode_cp = mode as i32;
114            let params = &[
115                &mut force_cp as *mut Vector3 as *mut c_void,
116                &mut mode_cp as *mut i32 as *mut c_void,
117            ];
118            self.method(("AddForce", 2))
119                .ok_or("Method 'AddForce' not found")?
120                .call::<()>(params)?;
121            Ok(())
122        }
123    }
124
125    /// Gets mass
126    ///
127    /// # Returns
128    /// * `Result<f32, String>` - The mass of the rigidbody
129    pub fn get_mass(&self) -> Result<f32, String> {
130        unsafe {
131            self.method("get_mass")
132                .ok_or("Method 'get_mass' not found")?
133                .call::<f32>(&[])
134        }
135    }
136
137    /// Sets mass
138    ///
139    /// # Arguments
140    /// * `value` - The new mass
141    ///
142    /// # Returns
143    /// * `Result<(), String>` - Ok if success
144    pub fn set_mass(&self, value: f32) -> Result<(), String> {
145        unsafe {
146            let mut value_cp = value;
147            self.method("set_mass")
148                .ok_or("Method 'set_mass' not found")?
149                .call::<()>(&[&mut value_cp as *mut f32 as *mut c_void])?;
150            Ok(())
151        }
152    }
153
154    /// Gets drag
155    ///
156    /// # Returns
157    /// * `Result<f32, String>` - The linear drag coefficient
158    pub fn get_drag(&self) -> Result<f32, String> {
159        unsafe {
160            self.method("get_drag")
161                .ok_or("Method 'get_drag' not found")?
162                .call::<f32>(&[])
163        }
164    }
165
166    /// Sets drag
167    ///
168    /// # Arguments
169    /// * `value` - The new linear drag coefficient
170    ///
171    /// # Returns
172    /// * `Result<(), String>` - Ok if success
173    pub fn set_drag(&self, value: f32) -> Result<(), String> {
174        unsafe {
175            let mut value_cp = value;
176            self.method("set_drag")
177                .ok_or("Method 'set_drag' not found")?
178                .call::<()>(&[&mut value_cp as *mut f32 as *mut c_void])?;
179            Ok(())
180        }
181    }
182
183    /// Gets angular drag
184    ///
185    /// # Returns
186    /// * `Result<f32, String>` - The angular drag coefficient
187    pub fn get_angular_drag(&self) -> Result<f32, String> {
188        unsafe {
189            self.method("get_angularDrag")
190                .ok_or("Method 'get_angularDrag' not found")?
191                .call::<f32>(&[])
192        }
193    }
194
195    /// Sets angular drag
196    ///
197    /// # Arguments
198    /// * `value` - The new angular drag coefficient
199    ///
200    /// # Returns
201    /// * `Result<(), String>` - Ok if success
202    pub fn set_angular_drag(&self, value: f32) -> Result<(), String> {
203        unsafe {
204            let mut value_cp = value;
205            self.method("set_angularDrag")
206                .ok_or("Method 'set_angularDrag' not found")?
207                .call::<()>(&[&mut value_cp as *mut f32 as *mut c_void])?;
208            Ok(())
209        }
210    }
211
212    /// Checks if kinematic
213    ///
214    /// # Returns
215    /// * `Result<bool, String>` - True if the rigidbody is kinematic
216    pub fn get_is_kinematic(&self) -> Result<bool, String> {
217        unsafe {
218            self.method("get_isKinematic")
219                .ok_or("Method 'get_isKinematic' not found")?
220                .call::<bool>(&[])
221        }
222    }
223
224    /// Sets kinematic
225    ///
226    /// # Arguments
227    /// * `value` - True to set the rigidbody as kinematic
228    ///
229    /// # Returns
230    /// * `Result<(), String>` - Ok if success
231    pub fn set_is_kinematic(&self, value: bool) -> Result<(), String> {
232        unsafe {
233            let mut value_cp = value;
234            self.method("set_isKinematic")
235                .ok_or("Method 'set_isKinematic' not found")?
236                .call::<()>(&[&mut value_cp as *mut bool as *mut c_void])?;
237            Ok(())
238        }
239    }
240
241    /// Checks if gravity is used
242    ///
243    /// # Returns
244    /// * `Result<bool, String>` - True if gravity is applied
245    pub fn get_use_gravity(&self) -> Result<bool, String> {
246        unsafe {
247            self.method("get_useGravity")
248                .ok_or("Method 'get_useGravity' not found")?
249                .call::<bool>(&[])
250        }
251    }
252
253    /// Sets use gravity
254    ///
255    /// # Arguments
256    /// * `value` - True to apply gravity
257    ///
258    /// # Returns
259    /// * `Result<(), String>` - Ok if success
260    pub fn set_use_gravity(&self, value: bool) -> Result<(), String> {
261        unsafe {
262            let mut value_cp = value;
263            self.method("set_useGravity")
264                .ok_or("Method 'set_useGravity' not found")?
265                .call::<()>(&[&mut value_cp as *mut bool as *mut c_void])?;
266            Ok(())
267        }
268    }
269}