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