dora_ssr/dora/
joint.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 joint_type() -> i32;
11	fn joint_distance(collision: i32, body_a: i64, body_b: i64, anchor_a: i64, anchor_b: i64, frequency: f32, damping: f32) -> i64;
12	fn joint_friction(collision: i32, body_a: i64, body_b: i64, world_pos: i64, max_force: f32, max_torque: f32) -> i64;
13	fn joint_gear(collision: i32, joint_a: i64, joint_b: i64, ratio: f32) -> i64;
14	fn joint_spring(collision: i32, body_a: i64, body_b: i64, linear_offset: i64, angular_offset: f32, max_force: f32, max_torque: f32, correction_factor: f32) -> i64;
15	fn joint_move_target(collision: i32, body: i64, target_pos: i64, max_force: f32, frequency: f32, damping: f32) -> i64;
16	fn joint_prismatic(collision: i32, body_a: i64, body_b: i64, world_pos: i64, axis_angle: f32, lower_translation: f32, upper_translation: f32, max_motor_force: f32, motor_speed: f32) -> i64;
17	fn joint_pulley(collision: i32, body_a: i64, body_b: i64, anchor_a: i64, anchor_b: i64, ground_anchor_a: i64, ground_anchor_b: i64, ratio: f32) -> i64;
18	fn joint_revolute(collision: i32, body_a: i64, body_b: i64, world_pos: i64, lower_angle: f32, upper_angle: f32, max_motor_torque: f32, motor_speed: f32) -> i64;
19	fn joint_rope(collision: i32, body_a: i64, body_b: i64, anchor_a: i64, anchor_b: i64, max_length: f32) -> i64;
20	fn joint_weld(collision: i32, body_a: i64, body_b: i64, world_pos: i64, frequency: f32, damping: f32) -> i64;
21	fn joint_wheel(collision: i32, body_a: i64, body_b: i64, world_pos: i64, axis_angle: f32, max_motor_torque: f32, motor_speed: f32, frequency: f32, damping: f32) -> i64;
22	fn joint_get_world(slf: i64) -> i64;
23	fn joint_destroy(slf: i64);
24	fn joint_new(def: i64, item_dict: i64) -> i64;
25}
26use crate::dora::IObject;
27/// A struct that can be used to connect physics bodies together.
28pub struct Joint { raw: i64 }
29crate::dora_object!(Joint);
30impl IJoint for Joint { }
31pub trait IJoint: IObject {
32	/// Gets the physics world that the joint belongs to.
33	fn get_world(&self) -> crate::dora::PhysicsWorld {
34		return unsafe { crate::dora::PhysicsWorld::from(joint_get_world(self.raw())).unwrap() };
35	}
36	/// Destroys the joint and removes it from the physics simulation.
37	fn destroy(&mut self) {
38		unsafe { joint_destroy(self.raw()); }
39	}
40}
41impl Joint {
42	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
43		(unsafe { joint_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
44			match raw {
45				0 => None,
46				_ => Some(Box::new(Joint { raw: raw }))
47			}
48		})
49	}
50	/// Creates a distance joint between two physics bodies.
51	///
52	/// # Arguments
53	///
54	/// * `can_collide` - Whether or not the physics body connected to joint will collide with each other.
55	/// * `body_a` - The first physics body to connect with the joint.
56	/// * `body_b` - The second physics body to connect with the joint.
57	/// * `anchor_a` - The position of the joint on the first physics body.
58	/// * `anchor_b` - The position of the joint on the second physics body.
59	/// * `frequency` - The frequency of the joint, in Hertz.
60	/// * `damping` - The damping ratio of the joint.
61	///
62	/// # Returns
63	///
64	/// * `Joint` - The new distance joint.
65	pub fn distance(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, anchor_a: &crate::dora::Vec2, anchor_b: &crate::dora::Vec2, frequency: f32, damping: f32) -> crate::dora::Joint {
66		unsafe { return crate::dora::Joint::from(joint_distance(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), anchor_a.into_i64(), anchor_b.into_i64(), frequency, damping)).unwrap(); }
67	}
68	/// Creates a friction joint between two physics bodies.
69	///
70	/// # Arguments
71	///
72	/// * `can_collide` - Whether or not the physics body connected to joint will collide with each other.
73	/// * `body_a` - The first physics body to connect with the joint.
74	/// * `body_b` - The second physics body to connect with the joint.
75	/// * `world_pos` - The position of the joint in the game world.
76	/// * `max_force` - The maximum force that can be applied to the joint.
77	/// * `max_torque` - The maximum torque that can be applied to the joint.
78	///
79	/// # Returns
80	///
81	/// * `Joint` - The new friction joint.
82	pub fn friction(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, world_pos: &crate::dora::Vec2, max_force: f32, max_torque: f32) -> crate::dora::Joint {
83		unsafe { return crate::dora::Joint::from(joint_friction(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), world_pos.into_i64(), max_force, max_torque)).unwrap(); }
84	}
85	/// Creates a gear joint between two other joints.
86	///
87	/// # Arguments
88	///
89	/// * `can_collide` - Whether or not the physics bodies connected to the joint can collide with each other.
90	/// * `joint_a` - The first joint to connect with the gear joint.
91	/// * `joint_b` - The second joint to connect with the gear joint.
92	/// * `ratio` - The gear ratio.
93	///
94	/// # Returns
95	///
96	/// * `Joint` - The new gear joint.
97	pub fn gear(collision: bool, joint_a: &dyn crate::dora::IJoint, joint_b: &dyn crate::dora::IJoint, ratio: f32) -> crate::dora::Joint {
98		unsafe { return crate::dora::Joint::from(joint_gear(if collision { 1 } else { 0 }, joint_a.raw(), joint_b.raw(), ratio)).unwrap(); }
99	}
100	/// Creates a new spring joint between the two specified bodies.
101	///
102	/// # Arguments
103	///
104	/// * `can_collide` - Whether the connected bodies should collide with each other.
105	/// * `body_a` - The first body connected to the joint.
106	/// * `body_b` - The second body connected to the joint.
107	/// * `linear_offset` - Position of body-B minus the position of body-A, in body-A's frame.
108	/// * `angular_offset` - Angle of body-B minus angle of body-A.
109	/// * `max_force` - The maximum force the joint can exert.
110	/// * `max_torque` - The maximum torque the joint can exert.
111	/// * `correction_factor` - Correction factor. 0.0 = no correction, 1.0 = full correction.
112	///
113	/// # Returns
114	///
115	/// * `Joint` - The created joint.
116	pub fn spring(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, linear_offset: &crate::dora::Vec2, angular_offset: f32, max_force: f32, max_torque: f32, correction_factor: f32) -> crate::dora::Joint {
117		unsafe { return crate::dora::Joint::from(joint_spring(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), linear_offset.into_i64(), angular_offset, max_force, max_torque, correction_factor)).unwrap(); }
118	}
119	/// Creates a new move joint for the specified body.
120	///
121	/// # Arguments
122	///
123	/// * `can_collide` - Whether the body can collide with other bodies.
124	/// * `body` - The body that the joint is attached to.
125	/// * `target_pos` - The target position that the body should move towards.
126	/// * `max_force` - The maximum force the joint can exert.
127	/// * `frequency` - Frequency ratio.
128	/// * `damping` - Damping ratio.
129	///
130	/// # Returns
131	///
132	/// * `MoveJoint` - The created move joint.
133	pub fn move_target(collision: bool, body: &dyn crate::dora::IBody, target_pos: &crate::dora::Vec2, max_force: f32, frequency: f32, damping: f32) -> crate::dora::MoveJoint {
134		unsafe { return crate::dora::MoveJoint::from(joint_move_target(if collision { 1 } else { 0 }, body.raw(), target_pos.into_i64(), max_force, frequency, damping)).unwrap(); }
135	}
136	/// Creates a new prismatic joint between the two specified bodies.
137	///
138	/// # Arguments
139	///
140	/// * `can_collide` - Whether the connected bodies should collide with each other.
141	/// * `body_a` - The first body connected to the joint.
142	/// * `body_b` - The second body connected to the joint.
143	/// * `world_pos` - The world position of the joint.
144	/// * `axis_angle` - The axis angle of the joint.
145	/// * `lower_translation` - Lower translation limit.
146	/// * `upper_translation` - Upper translation limit.
147	/// * `max_motor_force` - Maximum motor force.
148	/// * `motor_speed` - Motor speed.
149	///
150	/// # Returns
151	///
152	/// * `MotorJoint` - The created prismatic joint.
153	pub fn prismatic(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, world_pos: &crate::dora::Vec2, axis_angle: f32, lower_translation: f32, upper_translation: f32, max_motor_force: f32, motor_speed: f32) -> crate::dora::MotorJoint {
154		unsafe { return crate::dora::MotorJoint::from(joint_prismatic(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), world_pos.into_i64(), axis_angle, lower_translation, upper_translation, max_motor_force, motor_speed)).unwrap(); }
155	}
156	/// Creates a pulley joint between two physics bodies.
157	///
158	/// # Arguments
159	///
160	/// * `can_collide` - Whether or not the connected bodies will collide with each other.
161	/// * `body_a` - The first physics body to connect.
162	/// * `body_b` - The second physics body to connect.
163	/// * `anchor_a` - The position of the anchor point on the first body.
164	/// * `anchor_b` - The position of the anchor point on the second body.
165	/// * `ground_anchor_a` - The position of the ground anchor point on the first body in world coordinates.
166	/// * `ground_anchor_b` - The position of the ground anchor point on the second body in world coordinates.
167	/// * `ratio` - The pulley ratio.
168	///
169	/// # Returns
170	///
171	/// * `Joint` - The pulley joint.
172	pub fn pulley(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, anchor_a: &crate::dora::Vec2, anchor_b: &crate::dora::Vec2, ground_anchor_a: &crate::dora::Vec2, ground_anchor_b: &crate::dora::Vec2, ratio: f32) -> crate::dora::Joint {
173		unsafe { return crate::dora::Joint::from(joint_pulley(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), anchor_a.into_i64(), anchor_b.into_i64(), ground_anchor_a.into_i64(), ground_anchor_b.into_i64(), ratio)).unwrap(); }
174	}
175	/// Creates a revolute joint between two physics bodies.
176	///
177	/// # Arguments
178	///
179	/// * `can_collide` - Whether or not the connected bodies will collide with each other.
180	/// * `body_a` - The first physics body to connect.
181	/// * `body_b` - The second physics body to connect.
182	/// * `world_pos` - The position in world coordinates where the joint will be created.
183	/// * `lower_angle` - The lower angle limit in radians.
184	/// * `upper_angle` - The upper angle limit in radians.
185	/// * `max_motor_torque` - The maximum torque that can be applied to the joint to achieve the target speed.
186	/// * `motor_speed` - The desired speed of the joint.
187	///
188	/// # Returns
189	///
190	/// * `MotorJoint` - The revolute joint.
191	pub fn revolute(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, world_pos: &crate::dora::Vec2, lower_angle: f32, upper_angle: f32, max_motor_torque: f32, motor_speed: f32) -> crate::dora::MotorJoint {
192		unsafe { return crate::dora::MotorJoint::from(joint_revolute(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), world_pos.into_i64(), lower_angle, upper_angle, max_motor_torque, motor_speed)).unwrap(); }
193	}
194	/// Creates a rope joint between two physics bodies.
195	///
196	/// # Arguments
197	///
198	/// * `can_collide` - Whether or not the connected bodies will collide with each other.
199	/// * `body_a` - The first physics body to connect.
200	/// * `body_b` - The second physics body to connect.
201	/// * `anchor_a` - The position of the anchor point on the first body.
202	/// * `anchor_b` - The position of the anchor point on the second body.
203	/// * `max_length` - The maximum distance between the anchor points.
204	///
205	/// # Returns
206	///
207	/// * `Joint` - The rope joint.
208	pub fn rope(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, anchor_a: &crate::dora::Vec2, anchor_b: &crate::dora::Vec2, max_length: f32) -> crate::dora::Joint {
209		unsafe { return crate::dora::Joint::from(joint_rope(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), anchor_a.into_i64(), anchor_b.into_i64(), max_length)).unwrap(); }
210	}
211	/// Creates a weld joint between two bodies.
212	///
213	/// # Arguments
214	///
215	/// * `can_collide` - Whether or not the bodies connected to the joint can collide with each other.
216	/// * `body_a` - The first body to be connected by the joint.
217	/// * `body_b` - The second body to be connected by the joint.
218	/// * `world_pos` - The position in the world to connect the bodies together.
219	/// * `frequency` - The frequency at which the joint should be stiff.
220	/// * `damping` - The damping rate of the joint.
221	///
222	/// # Returns
223	///
224	/// * `Joint` - The newly created weld joint.
225	pub fn weld(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, world_pos: &crate::dora::Vec2, frequency: f32, damping: f32) -> crate::dora::Joint {
226		unsafe { return crate::dora::Joint::from(joint_weld(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), world_pos.into_i64(), frequency, damping)).unwrap(); }
227	}
228	/// Creates a wheel joint between two bodies.
229	///
230	/// # Arguments
231	///
232	/// * `can_collide` - Whether or not the bodies connected to the joint can collide with each other.
233	/// * `body_a` - The first body to be connected by the joint.
234	/// * `body_b` - The second body to be connected by the joint.
235	/// * `world_pos` - The position in the world to connect the bodies together.
236	/// * `axis_angle` - The angle of the joint axis in radians.
237	/// * `max_motor_torque` - The maximum torque the joint motor can exert.
238	/// * `motor_speed` - The target speed of the joint motor.
239	/// * `frequency` - The frequency at which the joint should be stiff.
240	/// * `damping` - The damping rate of the joint.
241	///
242	/// # Returns
243	///
244	/// * `MotorJoint` - The newly created wheel joint.
245	pub fn wheel(collision: bool, body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody, world_pos: &crate::dora::Vec2, axis_angle: f32, max_motor_torque: f32, motor_speed: f32, frequency: f32, damping: f32) -> crate::dora::MotorJoint {
246		unsafe { return crate::dora::MotorJoint::from(joint_wheel(if collision { 1 } else { 0 }, body_a.raw(), body_b.raw(), world_pos.into_i64(), axis_angle, max_motor_torque, motor_speed, frequency, damping)).unwrap(); }
247	}
248	/// Creates a joint instance based on the given joint definition and item dictionary containing physics bodies to be connected by joint.
249	///
250	/// # Arguments
251	///
252	/// * `def` - The joint definition.
253	/// * `item_dict` - The dictionary containing all the bodies and other required items.
254	///
255	/// # Returns
256	///
257	/// * `Joint` - The newly created joint.
258	pub fn new(def: &crate::dora::JointDef, item_dict: &crate::dora::Dictionary) -> Joint {
259		unsafe { return Joint { raw: joint_new(def.raw(), item_dict.raw()) }; }
260	}
261}