1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Unity Physics helper wrapper
use crate::structs::components::physics::collider::Collider;
use crate::structs::components::physics::layer_mask::LayerMask;
use crate::structs::math::{Ray, RaycastHit, Vector3};
use std::ffi::c_void;
#[repr(transparent)]
pub struct Physics;
impl Physics {
/// Gets the Physics class definition
///
/// # Returns
/// * `Option<Class>` - The UnityEngine.Physics class
pub fn get_class() -> Option<crate::structs::core::Class> {
crate::api::cache::coremodule().class("UnityEngine.Physics")
}
/// Casts a ray against all colliders
///
/// # Arguments
/// * `origin` - The starting point of the ray
/// * `direction` - The direction of the ray
/// * `hit_info` - Output structure containing hit information
/// * `max_distance` - The maximum distance the ray should check
/// * `layer_mask` - A LayerMask that filters which colliders to check
///
/// # Returns
/// * `bool` - True if the ray hits a collider
pub fn raycast(
origin: Vector3,
direction: Vector3,
hit_info: &mut RaycastHit,
max_distance: f32,
layer_mask: LayerMask,
) -> bool {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method((
"Raycast",
[
"UnityEngine.Vector3",
"UnityEngine.Vector3",
"UnityEngine.RaycastHit&",
"System.Single",
"System.Int32",
],
)) {
let mut origin_cp = origin;
let mut direction_cp = direction;
let mut max_distance_cp = max_distance;
let mut layer_mask_cp = layer_mask.value;
let params = &mut [
&mut origin_cp as *mut Vector3 as *mut c_void,
&mut direction_cp as *mut Vector3 as *mut c_void,
hit_info as *mut RaycastHit as *mut c_void,
&mut max_distance_cp as *mut f32 as *mut c_void,
&mut layer_mask_cp as *mut i32 as *mut c_void,
];
let res = unsafe { method.call::<bool>(params) };
return res.unwrap_or(false);
}
}
false
}
/// Casts a ray using a Ray struct
///
/// # Arguments
/// * `ray` - The Ray to cast
/// * `hit_info` - Output structure containing hit information
/// * `max_distance` - The maximum distance the ray should check
/// * `layer_mask` - A LayerMask that filters which colliders to check
///
/// # Returns
/// * `bool` - True if the ray hits a collider
pub fn raycast_ray(
ray: Ray,
hit_info: &mut RaycastHit,
max_distance: f32,
layer_mask: LayerMask,
) -> bool {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method((
"Raycast",
[
"UnityEngine.Ray",
"UnityEngine.RaycastHit&",
"System.Single",
"System.Int32",
],
)) {
let mut ray_cp = ray;
let mut max_distance_cp = max_distance;
let mut layer_mask_cp = layer_mask.value;
let params = &mut [
&mut ray_cp as *mut Ray as *mut c_void,
hit_info as *mut RaycastHit as *mut c_void,
&mut max_distance_cp as *mut f32 as *mut c_void,
&mut layer_mask_cp as *mut i32 as *mut c_void,
];
let res = unsafe { method.call::<bool>(params) };
return res.unwrap_or(false);
}
}
false
}
/// Casts a sphere along a ray
///
/// # Arguments
/// * `origin` - The center of the sphere at the start of the sweep
/// * `radius` - The radius of the sphere
/// * `direction` - The direction into which to sweep the sphere
/// * `hit_info` - Output structure containing hit information
/// * `max_distance` - The maximum distance the sphere should check
/// * `layer_mask` - A LayerMask that filters which colliders to check
///
/// # Returns
/// * `bool` - True if the sphere hits a collider
pub fn sphere_cast(
origin: Vector3,
radius: f32,
direction: Vector3,
hit_info: &mut RaycastHit,
max_distance: f32,
layer_mask: LayerMask,
) -> bool {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method((
"SphereCast",
[
"UnityEngine.Vector3",
"System.Single",
"UnityEngine.Vector3",
"UnityEngine.RaycastHit&",
"System.Single",
"System.Int32",
],
)) {
let mut origin_cp = origin;
let mut radius_cp = radius;
let mut direction_cp = direction;
let mut max_distance_cp = max_distance;
let mut layer_mask_cp = layer_mask.value;
let params = &mut [
&mut origin_cp as *mut Vector3 as *mut c_void,
&mut radius_cp as *mut f32 as *mut c_void,
&mut direction_cp as *mut Vector3 as *mut c_void,
hit_info as *mut RaycastHit as *mut c_void,
&mut max_distance_cp as *mut f32 as *mut c_void,
&mut layer_mask_cp as *mut i32 as *mut c_void,
];
let res = unsafe { method.call::<bool>(params) };
return res.unwrap_or(false);
}
}
false
}
/// Computes and stores colliders touching or inside the sphere
///
/// # Arguments
/// * `position` - Center of the sphere
/// * `radius` - Radius of the sphere
/// * `layer_mask` - A LayerMask that filters which colliders to check
///
/// # Returns
/// * `Vec<Collider>` - A list of colliders that overlap the sphere
pub fn overlap_sphere(position: Vector3, radius: f32, layer_mask: LayerMask) -> Vec<Collider> {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method((
"OverlapSphere",
["UnityEngine.Vector3", "System.Single", "System.Int32"],
)) {
let mut position_cp = position;
let mut radius_cp = radius;
let mut layer_mask_cp = layer_mask.value;
let params = &mut [
&mut position_cp as *mut Vector3 as *mut c_void,
&mut radius_cp as *mut f32 as *mut c_void,
&mut layer_mask_cp as *mut i32 as *mut c_void,
];
let res = unsafe { method.call::<*mut c_void>(params) };
if let Ok(ptr) = res {
if !ptr.is_null() {
let array_ptr =
ptr as *mut crate::structs::collections::Il2cppArray<*mut c_void>;
let mut colliders = Vec::new();
let len = unsafe { (*array_ptr).max_length };
for i in 0..len {
let item_ptr = unsafe { (*array_ptr).at(i) };
if !item_ptr.is_null() {
unsafe {
colliders.push(Collider::from_ptr(item_ptr));
}
}
}
return colliders;
}
}
}
}
Vec::new()
}
/// Gets global gravity
///
/// # Returns
/// * `Vector3` - The gravity vector
pub fn get_gravity() -> Vector3 {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method("get_gravity") {
let res = unsafe { method.call::<Vector3>(&[]) };
return res.unwrap_or(Vector3::ZERO);
}
}
Vector3::ZERO
}
/// Sets global gravity
///
/// # Arguments
/// * `value` - The new gravity vector
pub fn set_gravity(value: Vector3) {
if let Some(class) = Self::get_class() {
if let Some(method) = class.method("set_gravity") {
let mut value_cp = value;
let params = &mut [&mut value_cp as *mut Vector3 as *mut c_void];
let _ = unsafe { method.call::<()>(params) };
}
}
}
}