dora_ssr/dora/
body_def.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn bodydef_type() -> i32;
11	fn bodydef_set_type(slf: i64, body_type: i32);
12	fn bodydef_get_type(slf: i64) -> i32;
13	fn bodydef_set_position(slf: i64, val: i64);
14	fn bodydef_get_position(slf: i64) -> i64;
15	fn bodydef_set_angle(slf: i64, val: f32);
16	fn bodydef_get_angle(slf: i64) -> f32;
17	fn bodydef_set_face(slf: i64, val: i64);
18	fn bodydef_get_face(slf: i64) -> i64;
19	fn bodydef_set_face_pos(slf: i64, val: i64);
20	fn bodydef_get_face_pos(slf: i64) -> i64;
21	fn bodydef_set_linear_damping(slf: i64, val: f32);
22	fn bodydef_get_linear_damping(slf: i64) -> f32;
23	fn bodydef_set_angular_damping(slf: i64, val: f32);
24	fn bodydef_get_angular_damping(slf: i64) -> f32;
25	fn bodydef_set_linear_acceleration(slf: i64, val: i64);
26	fn bodydef_get_linear_acceleration(slf: i64) -> i64;
27	fn bodydef_set_fixed_rotation(slf: i64, val: i32);
28	fn bodydef_is_fixed_rotation(slf: i64) -> i32;
29	fn bodydef_set_bullet(slf: i64, val: i32);
30	fn bodydef_is_bullet(slf: i64) -> i32;
31	fn bodydef_polygon_with_center(center: i64, width: f32, height: f32, angle: f32, density: f32, friction: f32, restitution: f32) -> i64;
32	fn bodydef_polygon(width: f32, height: f32, density: f32, friction: f32, restitution: f32) -> i64;
33	fn bodydef_polygon_with_vertices(vertices: i64, density: f32, friction: f32, restitution: f32) -> i64;
34	fn bodydef_attach_polygon_with_center(slf: i64, center: i64, width: f32, height: f32, angle: f32, density: f32, friction: f32, restitution: f32);
35	fn bodydef_attach_polygon(slf: i64, width: f32, height: f32, density: f32, friction: f32, restitution: f32);
36	fn bodydef_attach_polygon_with_vertices(slf: i64, vertices: i64, density: f32, friction: f32, restitution: f32);
37	fn bodydef_multi(vertices: i64, density: f32, friction: f32, restitution: f32) -> i64;
38	fn bodydef_attach_multi(slf: i64, vertices: i64, density: f32, friction: f32, restitution: f32);
39	fn bodydef_disk_with_center(center: i64, radius: f32, density: f32, friction: f32, restitution: f32) -> i64;
40	fn bodydef_disk(radius: f32, density: f32, friction: f32, restitution: f32) -> i64;
41	fn bodydef_attach_disk_with_center(slf: i64, center: i64, radius: f32, density: f32, friction: f32, restitution: f32);
42	fn bodydef_attach_disk(slf: i64, radius: f32, density: f32, friction: f32, restitution: f32);
43	fn bodydef_chain(vertices: i64, friction: f32, restitution: f32) -> i64;
44	fn bodydef_attach_chain(slf: i64, vertices: i64, friction: f32, restitution: f32);
45	fn bodydef_attach_polygon_sensor(slf: i64, tag: i32, width: f32, height: f32);
46	fn bodydef_attach_polygon_sensor_with_center(slf: i64, tag: i32, center: i64, width: f32, height: f32, angle: f32);
47	fn bodydef_attach_polygon_sensor_with_vertices(slf: i64, tag: i32, vertices: i64);
48	fn bodydef_attach_disk_sensor_with_center(slf: i64, tag: i32, center: i64, radius: f32);
49	fn bodydef_attach_disk_sensor(slf: i64, tag: i32, radius: f32);
50	fn bodydef_new() -> i64;
51}
52use crate::dora::IObject;
53/// A struct to describe the properties of a physics body.
54pub struct BodyDef { raw: i64 }
55crate::dora_object!(BodyDef);
56impl BodyDef {
57	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
58		(unsafe { bodydef_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
59			match raw {
60				0 => None,
61				_ => Some(Box::new(BodyDef { raw: raw }))
62			}
63		})
64	}
65	/// Sets the define for the type of the body.
66	///
67	/// # Arguments
68	///
69	/// * `body_type` - The type of the body.
70	pub fn set_type(&mut self, body_type: crate::dora::BodyType) {
71		unsafe { bodydef_set_type(self.raw(), body_type as i32); }
72	}
73	/// Gets the define for the type of the body.
74	///
75	/// # Returns
76	///
77	/// * `BodyType` - The type of the body.
78	pub fn get_type(&self) -> crate::dora::BodyType {
79		unsafe { return core::mem::transmute(bodydef_get_type(self.raw())); }
80	}
81	/// Sets define for the position of the body.
82	pub fn set_position(&mut self, val: &crate::dora::Vec2) {
83		unsafe { bodydef_set_position(self.raw(), val.into_i64()) };
84	}
85	/// Gets define for the position of the body.
86	pub fn get_position(&self) -> crate::dora::Vec2 {
87		return unsafe { crate::dora::Vec2::from(bodydef_get_position(self.raw())) };
88	}
89	/// Sets define for the angle of the body.
90	pub fn set_angle(&mut self, val: f32) {
91		unsafe { bodydef_set_angle(self.raw(), val) };
92	}
93	/// Gets define for the angle of the body.
94	pub fn get_angle(&self) -> f32 {
95		return unsafe { bodydef_get_angle(self.raw()) };
96	}
97	/// Sets define for the face image or other items accepted by creating `Face` for the body.
98	pub fn set_face(&mut self, val: &str) {
99		unsafe { bodydef_set_face(self.raw(), crate::dora::from_string(val)) };
100	}
101	/// Gets define for the face image or other items accepted by creating `Face` for the body.
102	pub fn get_face(&self) -> String {
103		return unsafe { crate::dora::to_string(bodydef_get_face(self.raw())) };
104	}
105	/// Sets define for the face position of the body.
106	pub fn set_face_pos(&mut self, val: &crate::dora::Vec2) {
107		unsafe { bodydef_set_face_pos(self.raw(), val.into_i64()) };
108	}
109	/// Gets define for the face position of the body.
110	pub fn get_face_pos(&self) -> crate::dora::Vec2 {
111		return unsafe { crate::dora::Vec2::from(bodydef_get_face_pos(self.raw())) };
112	}
113	/// Sets define for linear damping of the body.
114	pub fn set_linear_damping(&mut self, val: f32) {
115		unsafe { bodydef_set_linear_damping(self.raw(), val) };
116	}
117	/// Gets define for linear damping of the body.
118	pub fn get_linear_damping(&self) -> f32 {
119		return unsafe { bodydef_get_linear_damping(self.raw()) };
120	}
121	/// Sets define for angular damping of the body.
122	pub fn set_angular_damping(&mut self, val: f32) {
123		unsafe { bodydef_set_angular_damping(self.raw(), val) };
124	}
125	/// Gets define for angular damping of the body.
126	pub fn get_angular_damping(&self) -> f32 {
127		return unsafe { bodydef_get_angular_damping(self.raw()) };
128	}
129	/// Sets define for initial linear acceleration of the body.
130	pub fn set_linear_acceleration(&mut self, val: &crate::dora::Vec2) {
131		unsafe { bodydef_set_linear_acceleration(self.raw(), val.into_i64()) };
132	}
133	/// Gets define for initial linear acceleration of the body.
134	pub fn get_linear_acceleration(&self) -> crate::dora::Vec2 {
135		return unsafe { crate::dora::Vec2::from(bodydef_get_linear_acceleration(self.raw())) };
136	}
137	/// Sets whether the body's rotation is fixed or not.
138	pub fn set_fixed_rotation(&mut self, val: bool) {
139		unsafe { bodydef_set_fixed_rotation(self.raw(), if val { 1 } else { 0 }) };
140	}
141	/// Gets whether the body's rotation is fixed or not.
142	pub fn is_fixed_rotation(&self) -> bool {
143		return unsafe { bodydef_is_fixed_rotation(self.raw()) != 0 };
144	}
145	/// Sets whether the body is a bullet or not.
146	/// Set to true to add extra bullet movement check for the body.
147	pub fn set_bullet(&mut self, val: bool) {
148		unsafe { bodydef_set_bullet(self.raw(), if val { 1 } else { 0 }) };
149	}
150	/// Gets whether the body is a bullet or not.
151	/// Set to true to add extra bullet movement check for the body.
152	pub fn is_bullet(&self) -> bool {
153		return unsafe { bodydef_is_bullet(self.raw()) != 0 };
154	}
155	/// Creates a polygon fixture definition with the specified dimensions and center position.
156	///
157	/// # Arguments
158	///
159	/// * `center` - The center point of the polygon.
160	/// * `width` - The width of the polygon.
161	/// * `height` - The height of the polygon.
162	/// * `angle` - The angle of the polygon.
163	/// * `density` - The density of the polygon.
164	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
165	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.
166	pub fn polygon_with_center(center: &crate::dora::Vec2, width: f32, height: f32, angle: f32, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
167		unsafe { return crate::dora::FixtureDef::from(bodydef_polygon_with_center(center.into_i64(), width, height, angle, density, friction, restitution)).unwrap(); }
168	}
169	/// Creates a polygon fixture definition with the specified dimensions.
170	///
171	/// # Arguments
172	///
173	/// * `width` - The width of the polygon.
174	/// * `height` - The height of the polygon.
175	/// * `density` - The density of the polygon.
176	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
177	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.
178	pub fn polygon(width: f32, height: f32, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
179		unsafe { return crate::dora::FixtureDef::from(bodydef_polygon(width, height, density, friction, restitution)).unwrap(); }
180	}
181	/// Creates a polygon fixture definition with the specified vertices.
182	///
183	/// # Arguments
184	///
185	/// * `vertices` - The vertices of the polygon.
186	/// * `density` - The density of the polygon.
187	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
188	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.0.
189	pub fn polygon_with_vertices(vertices: &Vec<crate::dora::Vec2>, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
190		unsafe { return crate::dora::FixtureDef::from(bodydef_polygon_with_vertices(crate::dora::Vector::from_vec2(vertices), density, friction, restitution)).unwrap(); }
191	}
192	/// Attaches a polygon fixture definition to the body.
193	///
194	/// # Arguments
195	///
196	/// * `center` - The center point of the polygon.
197	/// * `width` - The width of the polygon.
198	/// * `height` - The height of the polygon.
199	/// * `angle` - The angle of the polygon.
200	/// * `density` - The density of the polygon.
201	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
202	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.0.
203	pub fn attach_polygon_with_center(&mut self, center: &crate::dora::Vec2, width: f32, height: f32, angle: f32, density: f32, friction: f32, restitution: f32) {
204		unsafe { bodydef_attach_polygon_with_center(self.raw(), center.into_i64(), width, height, angle, density, friction, restitution); }
205	}
206	/// Attaches a polygon fixture definition to the body.
207	///
208	/// # Arguments
209	///
210	/// * `width` - The width of the polygon.
211	/// * `height` - The height of the polygon.
212	/// * `density` - The density of the polygon.
213	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
214	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.0.
215	pub fn attach_polygon(&mut self, width: f32, height: f32, density: f32, friction: f32, restitution: f32) {
216		unsafe { bodydef_attach_polygon(self.raw(), width, height, density, friction, restitution); }
217	}
218	/// Attaches a polygon fixture definition to the body.
219	///
220	/// # Arguments
221	///
222	/// * `vertices` - The vertices of the polygon.
223	/// * `density` - The density of the polygon.
224	/// * `friction` - The friction of the polygon. Should be between 0 and 1.0.
225	/// * `restitution` - The restitution of the polygon. Should be between 0 and 1.0.
226	pub fn attach_polygon_with_vertices(&mut self, vertices: &Vec<crate::dora::Vec2>, density: f32, friction: f32, restitution: f32) {
227		unsafe { bodydef_attach_polygon_with_vertices(self.raw(), crate::dora::Vector::from_vec2(vertices), density, friction, restitution); }
228	}
229	/// Creates a concave shape definition made of multiple convex shapes.
230	///
231	/// # Arguments
232	///
233	/// * `vertices` - A vector containing the vertices of each convex shape that makes up the concave shape. Each convex shape in the vertices vector should end with a `Vec2(0.0, 0.0)` as separator.
234	/// * `density` - The density of the shape.
235	/// * `friction` - The friction coefficient of the shape. Should be between 0.0 and 1.0.
236	/// * `restitution` - The restitution (elasticity) of the shape. Should be between 0.0 and 1.0.
237	///
238	/// # Returns
239	///
240	/// * `FixtureDef` - The resulting fixture definition.
241	pub fn multi(vertices: &Vec<crate::dora::Vec2>, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
242		unsafe { return crate::dora::FixtureDef::from(bodydef_multi(crate::dora::Vector::from_vec2(vertices), density, friction, restitution)).unwrap(); }
243	}
244	/// Attaches a concave shape definition made of multiple convex shapes to the body.
245	///
246	/// # Arguments
247	///
248	/// * `vertices` - A vector containing the vertices of each convex shape that makes up the concave shape. Each convex shape in the vertices vector should end with a `Vec2(0.0, 0.0)` as separator.
249	/// * `density` - The density of the concave shape.
250	/// * `friction` - The friction of the concave shape. Should be between 0.0 and 1.0.
251	/// * `restitution` - The restitution of the concave shape. Should be between 0.0 and 1.0.
252	pub fn attach_multi(&mut self, vertices: &Vec<crate::dora::Vec2>, density: f32, friction: f32, restitution: f32) {
253		unsafe { bodydef_attach_multi(self.raw(), crate::dora::Vector::from_vec2(vertices), density, friction, restitution); }
254	}
255	/// Creates a Disk-shape fixture definition.
256	///
257	/// # Arguments
258	///
259	/// * `center` - The center of the circle.
260	/// * `radius` - The radius of the circle.
261	/// * `density` - The density of the circle.
262	/// * `friction` - The friction coefficient of the circle. Should be between 0.0 and 1.0.
263	/// * `restitution` - The restitution (elasticity) of the circle. Should be between 0.0 and 1.0.
264	///
265	/// # Returns
266	///
267	/// * `FixtureDef` - The resulting fixture definition.
268	pub fn disk_with_center(center: &crate::dora::Vec2, radius: f32, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
269		unsafe { return crate::dora::FixtureDef::from(bodydef_disk_with_center(center.into_i64(), radius, density, friction, restitution)).unwrap(); }
270	}
271	/// Creates a Disk-shape fixture definition.
272	///
273	/// # Arguments
274	///
275	/// * `radius` - The radius of the circle.
276	/// * `density` - The density of the circle.
277	/// * `friction` - The friction coefficient of the circle. Should be between 0.0 and 1.0.
278	/// * `restitution` - The restitution (elasticity) of the circle. Should be between 0.0 and 1.0.
279	///
280	/// # Returns
281	///
282	/// * `FixtureDef` - The resulting fixture definition.
283	pub fn disk(radius: f32, density: f32, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
284		unsafe { return crate::dora::FixtureDef::from(bodydef_disk(radius, density, friction, restitution)).unwrap(); }
285	}
286	/// Attaches a disk fixture definition to the body.
287	///
288	/// # Arguments
289	///
290	/// * `center` - The center point of the disk.
291	/// * `radius` - The radius of the disk.
292	/// * `density` - The density of the disk.
293	/// * `friction` - The friction of the disk. Should be between 0.0 and 1.0.
294	/// * `restitution` - The restitution of the disk. Should be between 0.0 and 1.0.
295	pub fn attach_disk_with_center(&mut self, center: &crate::dora::Vec2, radius: f32, density: f32, friction: f32, restitution: f32) {
296		unsafe { bodydef_attach_disk_with_center(self.raw(), center.into_i64(), radius, density, friction, restitution); }
297	}
298	/// Attaches a disk fixture definition to the body.
299	///
300	/// # Arguments
301	///
302	/// * `radius` - The radius of the disk.
303	/// * `density` - The density of the disk.
304	/// * `friction` - The friction of the disk. Should be between 0.0 and 1.0.
305	/// * `restitution` - The restitution of the disk. Should be between 0.0 and 1.0.
306	pub fn attach_disk(&mut self, radius: f32, density: f32, friction: f32, restitution: f32) {
307		unsafe { bodydef_attach_disk(self.raw(), radius, density, friction, restitution); }
308	}
309	/// Creates a Chain-shape fixture definition. This fixture is a free form sequence of line segments that has two-sided collision.
310	///
311	/// # Arguments
312	///
313	/// * `vertices` - The vertices of the chain.
314	/// * `friction` - The friction coefficient of the chain. Should be between 0.0 and 1.0.
315	/// * `restitution` - The restitution (elasticity) of the chain. Should be between 0.0 and 1.0.
316	///
317	/// # Returns
318	///
319	/// * `FixtureDef` - The resulting fixture definition.
320	pub fn chain(vertices: &Vec<crate::dora::Vec2>, friction: f32, restitution: f32) -> crate::dora::FixtureDef {
321		unsafe { return crate::dora::FixtureDef::from(bodydef_chain(crate::dora::Vector::from_vec2(vertices), friction, restitution)).unwrap(); }
322	}
323	/// Attaches a chain fixture definition to the body. The Chain fixture is a free form sequence of line segments that has two-sided collision.
324	///
325	/// # Arguments
326	///
327	/// * `vertices` - The vertices of the chain.
328	/// * `friction` - The friction of the chain. Should be between 0.0 and 1.0.
329	/// * `restitution` - The restitution of the chain. Should be between 0.0 and 1.0.
330	pub fn attach_chain(&mut self, vertices: &Vec<crate::dora::Vec2>, friction: f32, restitution: f32) {
331		unsafe { bodydef_attach_chain(self.raw(), crate::dora::Vector::from_vec2(vertices), friction, restitution); }
332	}
333	/// Attaches a polygon sensor fixture definition to the body.
334	///
335	/// # Arguments
336	///
337	/// * `tag` - An integer tag for the sensor.
338	/// * `width` - The width of the polygon.
339	/// * `height` - The height of the polygon.
340	pub fn attach_polygon_sensor(&mut self, tag: i32, width: f32, height: f32) {
341		unsafe { bodydef_attach_polygon_sensor(self.raw(), tag, width, height); }
342	}
343	/// Attaches a polygon sensor fixture definition to the body.
344	///
345	/// # Arguments
346	///
347	/// * `tag` - An integer tag for the sensor.
348	/// * `center` - The center point of the polygon.
349	/// * `width` - The width of the polygon.
350	/// * `height` - The height of the polygon.
351	/// * `angle` - Optional. The angle of the polygon.
352	pub fn attach_polygon_sensor_with_center(&mut self, tag: i32, center: &crate::dora::Vec2, width: f32, height: f32, angle: f32) {
353		unsafe { bodydef_attach_polygon_sensor_with_center(self.raw(), tag, center.into_i64(), width, height, angle); }
354	}
355	/// Attaches a polygon sensor fixture definition to the body.
356	///
357	/// # Arguments
358	///
359	/// * `tag` - An integer tag for the sensor.
360	/// * `vertices` - A vector containing the vertices of the polygon.
361	pub fn attach_polygon_sensor_with_vertices(&mut self, tag: i32, vertices: &Vec<crate::dora::Vec2>) {
362		unsafe { bodydef_attach_polygon_sensor_with_vertices(self.raw(), tag, crate::dora::Vector::from_vec2(vertices)); }
363	}
364	/// Attaches a disk sensor fixture definition to the body.
365	///
366	/// # Arguments
367	///
368	/// * `tag` - An integer tag for the sensor.
369	/// * `center` - The center of the disk.
370	/// * `radius` - The radius of the disk.
371	pub fn attach_disk_sensor_with_center(&mut self, tag: i32, center: &crate::dora::Vec2, radius: f32) {
372		unsafe { bodydef_attach_disk_sensor_with_center(self.raw(), tag, center.into_i64(), radius); }
373	}
374	/// Attaches a disk sensor fixture definition to the body.
375	///
376	/// # Arguments
377	///
378	/// * `tag` - An integer tag for the sensor.
379	/// * `radius` - The radius of the disk.
380	pub fn attach_disk_sensor(&mut self, tag: i32, radius: f32) {
381		unsafe { bodydef_attach_disk_sensor(self.raw(), tag, radius); }
382	}
383	/// Creates a new instance of `BodyDef` class.
384	///
385	/// # Returns
386	///
387	/// * A new `BodyDef` object.
388	pub fn new() -> BodyDef {
389		unsafe { return BodyDef { raw: bodydef_new() }; }
390	}
391}