Debian Codegraph @

scene/3d/physics/physical_bone_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* physical_bone_3d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "physical_bone_3d.h"
32
33 #include "scene/3d/physics/physical_bone_simulator_3d.h"
34 #ifndef DISABLE_DEPRECATED
35 #include "scene/3d/skeleton_3d.h"
36 #endif //_DISABLE_DEPRECATED
37
38 bool PhysicalBone3D::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
39 return false;
40 }
41
42 bool PhysicalBone3D::JointData::_get(const StringName &p_name, Variant &r_ret) const {
43 return false;
44 }
45
46 void PhysicalBone3D::JointData::_get_property_list(List<PropertyInfo> *p_list) const {
47 }
48
49 void PhysicalBone3D::apply_central_impulse(const Vector3 &p_impulse) {
50 PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
51 }
52
53 void PhysicalBone3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
54 PhysicsServer3D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position);
55 }
56
57 void PhysicalBone3D::set_linear_velocity(const Vector3 &p_velocity) {
58 linear_velocity = p_velocity;
59 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
60 }
61
62 Vector3 PhysicalBone3D::get_linear_velocity() const {
63 return linear_velocity;
64 }
65
66 void PhysicalBone3D::set_angular_velocity(const Vector3 &p_velocity) {
67 angular_velocity = p_velocity;
68 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
69 }
70
71 Vector3 PhysicalBone3D::get_angular_velocity() const {
72 return angular_velocity;
73 }
74
75 void PhysicalBone3D::set_use_custom_integrator(bool p_enable) {
76 if (custom_integrator == p_enable) {
77 return;
78 }
79
80 custom_integrator = p_enable;
81 PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
82 }
83
84 bool PhysicalBone3D::is_using_custom_integrator() {
85 return custom_integrator;
86 }
87
88 void PhysicalBone3D::reset_physics_simulation_state() {
89 if (simulate_physics) {
90 _start_physics_simulation();
91 } else {
92 _stop_physics_simulation();
93 }
94 }
95
96 void PhysicalBone3D::reset_to_rest_position() {
97 PhysicalBoneSimulator3D *simulator = get_simulator();
98 Skeleton3D *skeleton = get_skeleton();
99 if (simulator && skeleton) {
100 if (bone_id == -1) {
101 set_global_transform((skeleton->get_global_transform() * body_offset).orthonormalized());
102 } else {
103 set_global_transform((skeleton->get_global_transform() * simulator->get_bone_global_pose(bone_id) * body_offset).orthonormalized());
104 }
105 }
106 }
107
108 bool PhysicalBone3D::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
109 if (JointData::_set(p_name, p_value, j)) {
110 return true;
111 }
112
113 bool is_valid_pin = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_PIN;
114 if ("joint_constraints/bias" == p_name) {
115 bias = p_value;
116 if (is_valid_pin) {
117 PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_BIAS, bias);
118 }
119
120 } else if ("joint_constraints/damping" == p_name) {
121 damping = p_value;
122 if (is_valid_pin) {
123 PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_DAMPING, damping);
124 }
125
126 } else if ("joint_constraints/impulse_clamp" == p_name) {
127 impulse_clamp = p_value;
128 if (is_valid_pin) {
129 PhysicsServer3D::get_singleton()->pin_joint_set_param(j, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp);
130 }
131
132 } else {
133 return false;
134 }
135
136 return true;
137 }
138
139 bool PhysicalBone3D::PinJointData::_get(const StringName &p_name, Variant &r_ret) const {
140 if (JointData::_get(p_name, r_ret)) {
141 return true;
142 }
143
144 if ("joint_constraints/bias" == p_name) {
145 r_ret = bias;
146 } else if ("joint_constraints/damping" == p_name) {
147 r_ret = damping;
148 } else if ("joint_constraints/impulse_clamp" == p_name) {
149 r_ret = impulse_clamp;
150 } else {
151 return false;
152 }
153
154 return true;
155 }
156
157 void PhysicalBone3D::PinJointData::_get_property_list(List<PropertyInfo> *p_list) const {
158 JointData::_get_property_list(p_list);
159
160 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
161 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/damping"), PROPERTY_HINT_RANGE, "0.01,8.0,0.01"));
162 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/impulse_clamp"), PROPERTY_HINT_RANGE, "0.0,64.0,0.01"));
163 }
164
165 bool PhysicalBone3D::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
166 if (JointData::_set(p_name, p_value, j)) {
167 return true;
168 }
169
170 bool is_valid_cone = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_CONE_TWIST;
171 if ("joint_constraints/swing_span" == p_name) {
172 swing_span = Math::deg_to_rad(real_t(p_value));
173 if (is_valid_cone) {
174 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, swing_span);
175 }
176
177 } else if ("joint_constraints/twist_span" == p_name) {
178 twist_span = Math::deg_to_rad(real_t(p_value));
179 if (is_valid_cone) {
180 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, twist_span);
181 }
182
183 } else if ("joint_constraints/bias" == p_name) {
184 bias = p_value;
185 if (is_valid_cone) {
186 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, bias);
187 }
188
189 } else if ("joint_constraints/softness" == p_name) {
190 softness = p_value;
191 if (is_valid_cone) {
192 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, softness);
193 }
194
195 } else if ("joint_constraints/relaxation" == p_name) {
196 relaxation = p_value;
197 if (is_valid_cone) {
198 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, relaxation);
199 }
200
201 } else {
202 return false;
203 }
204
205 return true;
206 }
207
208 bool PhysicalBone3D::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const {
209 if (JointData::_get(p_name, r_ret)) {
210 return true;
211 }
212
213 if ("joint_constraints/swing_span" == p_name) {
214 r_ret = Math::rad_to_deg(swing_span);
215 } else if ("joint_constraints/twist_span" == p_name) {
216 r_ret = Math::rad_to_deg(twist_span);
217 } else if ("joint_constraints/bias" == p_name) {
218 r_ret = bias;
219 } else if ("joint_constraints/softness" == p_name) {
220 r_ret = softness;
221 } else if ("joint_constraints/relaxation" == p_name) {
222 r_ret = relaxation;
223 } else {
224 return false;
225 }
226
227 return true;
228 }
229
230 void PhysicalBone3D::ConeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
231 JointData::_get_property_list(p_list);
232
233 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/swing_span"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
234 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/twist_span"), PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_less,or_greater"));
235 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/bias"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
236 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
237 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/relaxation"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
238 }
239
240 bool PhysicalBone3D::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
241 if (JointData::_set(p_name, p_value, j)) {
242 return true;
243 }
244
245 bool is_valid_hinge = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_HINGE;
246 if ("joint_constraints/angular_limit_enabled" == p_name) {
247 angular_limit_enabled = p_value;
248 if (is_valid_hinge) {
249 PhysicsServer3D::get_singleton()->hinge_joint_set_flag(j, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled);
250 }
251
252 } else if ("joint_constraints/angular_limit_upper" == p_name) {
253 angular_limit_upper = Math::deg_to_rad(real_t(p_value));
254 if (is_valid_hinge) {
255 PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper);
256 }
257
258 } else if ("joint_constraints/angular_limit_lower" == p_name) {
259 angular_limit_lower = Math::deg_to_rad(real_t(p_value));
260 if (is_valid_hinge) {
261 PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower);
262 }
263
264 } else if ("joint_constraints/angular_limit_bias" == p_name) {
265 angular_limit_bias = p_value;
266 if (is_valid_hinge) {
267 PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias);
268 }
269
270 } else if ("joint_constraints/angular_limit_softness" == p_name) {
271 angular_limit_softness = p_value;
272 if (is_valid_hinge) {
273 PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness);
274 }
275
276 } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
277 angular_limit_relaxation = p_value;
278 if (is_valid_hinge) {
279 PhysicsServer3D::get_singleton()->hinge_joint_set_param(j, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation);
280 }
281
282 } else {
283 return false;
284 }
285
286 return true;
287 }
288
289 bool PhysicalBone3D::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const {
290 if (JointData::_get(p_name, r_ret)) {
291 return true;
292 }
293
294 if ("joint_constraints/angular_limit_enabled" == p_name) {
295 r_ret = angular_limit_enabled;
296 } else if ("joint_constraints/angular_limit_upper" == p_name) {
297 r_ret = Math::rad_to_deg(angular_limit_upper);
298 } else if ("joint_constraints/angular_limit_lower" == p_name) {
299 r_ret = Math::rad_to_deg(angular_limit_lower);
300 } else if ("joint_constraints/angular_limit_bias" == p_name) {
301 r_ret = angular_limit_bias;
302 } else if ("joint_constraints/angular_limit_softness" == p_name) {
303 r_ret = angular_limit_softness;
304 } else if ("joint_constraints/angular_limit_relaxation" == p_name) {
305 r_ret = angular_limit_relaxation;
306 } else {
307 return false;
308 }
309
310 return true;
311 }
312
313 void PhysicalBone3D::HingeJointData::_get_property_list(List<PropertyInfo> *p_list) const {
314 JointData::_get_property_list(p_list);
315
316 p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("joint_constraints/angular_limit_enabled")));
317 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
318 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
319 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_bias"), PROPERTY_HINT_RANGE, "0.01,0.99,0.01"));
320 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
321 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_relaxation"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
322 }
323
324 bool PhysicalBone3D::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
325 if (JointData::_set(p_name, p_value, j)) {
326 return true;
327 }
328
329 bool is_valid_slider = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_SLIDER;
330 if ("joint_constraints/linear_limit_upper" == p_name) {
331 linear_limit_upper = p_value;
332 if (is_valid_slider) {
333 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper);
334 }
335
336 } else if ("joint_constraints/linear_limit_lower" == p_name) {
337 linear_limit_lower = p_value;
338 if (is_valid_slider) {
339 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower);
340 }
341
342 } else if ("joint_constraints/linear_limit_softness" == p_name) {
343 linear_limit_softness = p_value;
344 if (is_valid_slider) {
345 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness);
346 }
347
348 } else if ("joint_constraints/linear_limit_restitution" == p_name) {
349 linear_limit_restitution = p_value;
350 if (is_valid_slider) {
351 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution);
352 }
353
354 } else if ("joint_constraints/linear_limit_damping" == p_name) {
355 linear_limit_damping = p_value;
356 if (is_valid_slider) {
357 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution);
358 }
359
360 } else if ("joint_constraints/angular_limit_upper" == p_name) {
361 angular_limit_upper = Math::deg_to_rad(real_t(p_value));
362 if (is_valid_slider) {
363 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper);
364 }
365
366 } else if ("joint_constraints/angular_limit_lower" == p_name) {
367 angular_limit_lower = Math::deg_to_rad(real_t(p_value));
368 if (is_valid_slider) {
369 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower);
370 }
371
372 } else if ("joint_constraints/angular_limit_softness" == p_name) {
373 angular_limit_softness = p_value;
374 if (is_valid_slider) {
375 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
376 }
377
378 } else if ("joint_constraints/angular_limit_restitution" == p_name) {
379 angular_limit_restitution = p_value;
380 if (is_valid_slider) {
381 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness);
382 }
383
384 } else if ("joint_constraints/angular_limit_damping" == p_name) {
385 angular_limit_damping = p_value;
386 if (is_valid_slider) {
387 PhysicsServer3D::get_singleton()->slider_joint_set_param(j, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping);
388 }
389
390 } else {
391 return false;
392 }
393
394 return true;
395 }
396
397 bool PhysicalBone3D::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const {
398 if (JointData::_get(p_name, r_ret)) {
399 return true;
400 }
401
402 if ("joint_constraints/linear_limit_upper" == p_name) {
403 r_ret = linear_limit_upper;
404 } else if ("joint_constraints/linear_limit_lower" == p_name) {
405 r_ret = linear_limit_lower;
406 } else if ("joint_constraints/linear_limit_softness" == p_name) {
407 r_ret = linear_limit_softness;
408 } else if ("joint_constraints/linear_limit_restitution" == p_name) {
409 r_ret = linear_limit_restitution;
410 } else if ("joint_constraints/linear_limit_damping" == p_name) {
411 r_ret = linear_limit_damping;
412 } else if ("joint_constraints/angular_limit_upper" == p_name) {
413 r_ret = Math::rad_to_deg(angular_limit_upper);
414 } else if ("joint_constraints/angular_limit_lower" == p_name) {
415 r_ret = Math::rad_to_deg(angular_limit_lower);
416 } else if ("joint_constraints/angular_limit_softness" == p_name) {
417 r_ret = angular_limit_softness;
418 } else if ("joint_constraints/angular_limit_restitution" == p_name) {
419 r_ret = angular_limit_restitution;
420 } else if ("joint_constraints/angular_limit_damping" == p_name) {
421 r_ret = angular_limit_damping;
422 } else {
423 return false;
424 }
425
426 return true;
427 }
428
429 void PhysicalBone3D::SliderJointData::_get_property_list(List<PropertyInfo> *p_list) const {
430 JointData::_get_property_list(p_list);
431
432 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_upper")));
433 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_lower")));
434 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
435 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
436 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/linear_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
437
438 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
439 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
440 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
441 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_restitution"), PROPERTY_HINT_RANGE, "0.01,16.0,0.01"));
442 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("joint_constraints/angular_limit_damping"), PROPERTY_HINT_RANGE, "0,16.0,0.01"));
443 }
444
445 bool PhysicalBone3D::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) {
446 if (JointData::_set(p_name, p_value, j)) {
447 return true;
448 }
449
450 String path = p_name;
451
452 if (!path.begins_with("joint_constraints/")) {
453 return false;
454 }
455
456 Vector3::Axis axis;
457 {
458 const String axis_s = path.get_slicec('/', 1);
459 if ("x" == axis_s) {
460 axis = Vector3::AXIS_X;
461 } else if ("y" == axis_s) {
462 axis = Vector3::AXIS_Y;
463 } else if ("z" == axis_s) {
464 axis = Vector3::AXIS_Z;
465 } else {
466 return false;
467 }
468 }
469
470 String var_name = path.get_slicec('/', 2);
471 bool is_valid_6dof = j.is_valid() && PhysicsServer3D::get_singleton()->joint_get_type(j) == PhysicsServer3D::JOINT_TYPE_6DOF;
472 if ("linear_limit_enabled" == var_name) {
473 axis_data[axis].linear_limit_enabled = p_value;
474 if (is_valid_6dof) {
475 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled);
476 }
477
478 } else if ("linear_limit_upper" == var_name) {
479 axis_data[axis].linear_limit_upper = p_value;
480 if (is_valid_6dof) {
481 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper);
482 }
483
484 } else if ("linear_limit_lower" == var_name) {
485 axis_data[axis].linear_limit_lower = p_value;
486 if (is_valid_6dof) {
487 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower);
488 }
489
490 } else if ("linear_limit_softness" == var_name) {
491 axis_data[axis].linear_limit_softness = p_value;
492 if (is_valid_6dof) {
493 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness);
494 }
495
496 } else if ("linear_spring_enabled" == var_name) {
497 axis_data[axis].linear_spring_enabled = p_value;
498 if (is_valid_6dof) {
499 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled);
500 }
501
502 } else if ("linear_spring_stiffness" == var_name) {
503 axis_data[axis].linear_spring_stiffness = p_value;
504 if (is_valid_6dof) {
505 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness);
506 }
507
508 } else if ("linear_spring_damping" == var_name) {
509 axis_data[axis].linear_spring_damping = p_value;
510 if (is_valid_6dof) {
511 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping);
512 }
513
514 } else if ("linear_equilibrium_point" == var_name) {
515 axis_data[axis].linear_equilibrium_point = p_value;
516 if (is_valid_6dof) {
517 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point);
518 }
519
520 } else if ("linear_restitution" == var_name) {
521 axis_data[axis].linear_restitution = p_value;
522 if (is_valid_6dof) {
523 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution);
524 }
525
526 } else if ("linear_damping" == var_name) {
527 axis_data[axis].linear_damping = p_value;
528 if (is_valid_6dof) {
529 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping);
530 }
531
532 } else if ("angular_limit_enabled" == var_name) {
533 axis_data[axis].angular_limit_enabled = p_value;
534 if (is_valid_6dof) {
535 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled);
536 }
537
538 } else if ("angular_limit_upper" == var_name) {
539 axis_data[axis].angular_limit_upper = Math::deg_to_rad(real_t(p_value));
540 if (is_valid_6dof) {
541 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper);
542 }
543
544 } else if ("angular_limit_lower" == var_name) {
545 axis_data[axis].angular_limit_lower = Math::deg_to_rad(real_t(p_value));
546 if (is_valid_6dof) {
547 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower);
548 }
549
550 } else if ("angular_limit_softness" == var_name) {
551 axis_data[axis].angular_limit_softness = p_value;
552 if (is_valid_6dof) {
553 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness);
554 }
555
556 } else if ("angular_restitution" == var_name) {
557 axis_data[axis].angular_restitution = p_value;
558 if (is_valid_6dof) {
559 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution);
560 }
561
562 } else if ("angular_damping" == var_name) {
563 axis_data[axis].angular_damping = p_value;
564 if (is_valid_6dof) {
565 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping);
566 }
567
568 } else if ("erp" == var_name) {
569 axis_data[axis].erp = p_value;
570 if (is_valid_6dof) {
571 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp);
572 }
573
574 } else if ("angular_spring_enabled" == var_name) {
575 axis_data[axis].angular_spring_enabled = p_value;
576 if (is_valid_6dof) {
577 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled);
578 }
579
580 } else if ("angular_spring_stiffness" == var_name) {
581 axis_data[axis].angular_spring_stiffness = p_value;
582 if (is_valid_6dof) {
583 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness);
584 }
585
586 } else if ("angular_spring_damping" == var_name) {
587 axis_data[axis].angular_spring_damping = p_value;
588 if (is_valid_6dof) {
589 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping);
590 }
591
592 } else if ("angular_equilibrium_point" == var_name) {
593 axis_data[axis].angular_equilibrium_point = p_value;
594 if (is_valid_6dof) {
595 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point);
596 }
597
598 } else {
599 return false;
600 }
601
602 return true;
603 }
604
605 bool PhysicalBone3D::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const {
606 if (JointData::_get(p_name, r_ret)) {
607 return true;
608 }
609
610 String path = p_name;
611
612 if (!path.begins_with("joint_constraints/")) {
613 return false;
614 }
615
616 int axis;
617 {
618 const String axis_s = path.get_slicec('/', 1);
619 if ("x" == axis_s) {
620 axis = 0;
621 } else if ("y" == axis_s) {
622 axis = 1;
623 } else if ("z" == axis_s) {
624 axis = 2;
625 } else {
626 return false;
627 }
628 }
629
630 String var_name = path.get_slicec('/', 2);
631
632 if ("linear_limit_enabled" == var_name) {
633 r_ret = axis_data[axis].linear_limit_enabled;
634 } else if ("linear_limit_upper" == var_name) {
635 r_ret = axis_data[axis].linear_limit_upper;
636 } else if ("linear_limit_lower" == var_name) {
637 r_ret = axis_data[axis].linear_limit_lower;
638 } else if ("linear_limit_softness" == var_name) {
639 r_ret = axis_data[axis].linear_limit_softness;
640 } else if ("linear_spring_enabled" == var_name) {
641 r_ret = axis_data[axis].linear_spring_enabled;
642 } else if ("linear_spring_stiffness" == var_name) {
643 r_ret = axis_data[axis].linear_spring_stiffness;
644 } else if ("linear_spring_damping" == var_name) {
645 r_ret = axis_data[axis].linear_spring_damping;
646 } else if ("linear_equilibrium_point" == var_name) {
647 r_ret = axis_data[axis].linear_equilibrium_point;
648 } else if ("linear_restitution" == var_name) {
649 r_ret = axis_data[axis].linear_restitution;
650 } else if ("linear_damping" == var_name) {
651 r_ret = axis_data[axis].linear_damping;
652 } else if ("angular_limit_enabled" == var_name) {
653 r_ret = axis_data[axis].angular_limit_enabled;
654 } else if ("angular_limit_upper" == var_name) {
655 r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_upper);
656 } else if ("angular_limit_lower" == var_name) {
657 r_ret = Math::rad_to_deg(axis_data[axis].angular_limit_lower);
658 } else if ("angular_limit_softness" == var_name) {
659 r_ret = axis_data[axis].angular_limit_softness;
660 } else if ("angular_restitution" == var_name) {
661 r_ret = axis_data[axis].angular_restitution;
662 } else if ("angular_damping" == var_name) {
663 r_ret = axis_data[axis].angular_damping;
664 } else if ("erp" == var_name) {
665 r_ret = axis_data[axis].erp;
666 } else if ("angular_spring_enabled" == var_name) {
667 r_ret = axis_data[axis].angular_spring_enabled;
668 } else if ("angular_spring_stiffness" == var_name) {
669 r_ret = axis_data[axis].angular_spring_stiffness;
670 } else if ("angular_spring_damping" == var_name) {
671 r_ret = axis_data[axis].angular_spring_damping;
672 } else if ("angular_equilibrium_point" == var_name) {
673 r_ret = axis_data[axis].angular_equilibrium_point;
674 } else {
675 return false;
676 }
677
678 return true;
679 }
680
681 void PhysicalBone3D::SixDOFJointData::_get_property_list(List<PropertyInfo> *p_list) const {
682 const StringName axis_names[] = { PNAME("x"), PNAME("y"), PNAME("z") };
683 for (int i = 0; i < 3; ++i) {
684 const String prefix = vformat("%s/%s/", PNAME("joint_constraints"), axis_names[i]);
685 p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_limit_enabled")));
686 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_upper")));
687 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_lower")));
688 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
689 p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("linear_spring_enabled")));
690 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_stiffness")));
691 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_spring_damping")));
692 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_equilibrium_point")));
693 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
694 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("linear_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
695 p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_limit_enabled")));
696 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_upper"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
697 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_lower"), PROPERTY_HINT_RANGE, "-180,180,0.01"));
698 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_limit_softness"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
699 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_restitution"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
700 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_damping"), PROPERTY_HINT_RANGE, "0.01,16,0.01"));
701 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("erp")));
702 p_list->push_back(PropertyInfo(Variant::BOOL, prefix + PNAME("angular_spring_enabled")));
703 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_stiffness")));
704 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_spring_damping")));
705 p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + PNAME("angular_equilibrium_point")));
706 }
707 }
708
709 bool PhysicalBone3D::_set(const StringName &p_name, const Variant &p_value) {
710 if (p_name == "bone_name") {
711 set_bone_name(p_value);
712 return true;
713 }
714
715 if (joint_data) {
716 if (joint_data->_set(p_name, p_value, joint)) {
717 #ifdef TOOLS_ENABLED
718 update_gizmos();
719 #endif
720 return true;
721 }
722 }
723
724 return false;
725 }
726
727 bool PhysicalBone3D::_get(const StringName &p_name, Variant &r_ret) const {
728 if (p_name == "bone_name") {
729 r_ret = get_bone_name();
730 return true;
731 }
732
733 if (joint_data) {
734 return joint_data->_get(p_name, r_ret);
735 }
736
737 return false;
738 }
739
740 void PhysicalBone3D::_get_property_list(List<PropertyInfo> *p_list) const {
741 Skeleton3D *skeleton = get_skeleton();
742 if (skeleton) {
743 p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name"), PROPERTY_HINT_ENUM, skeleton->get_concatenated_bone_names()));
744 } else {
745 p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("bone_name")));
746 }
747
748 if (joint_data) {
749 joint_data->_get_property_list(p_list);
750 }
751 }
752
753 void PhysicalBone3D::_notification(int p_what) {
754 switch (p_what) {
755 // We need to wait until the bone has finished being added to the tree
756 // or none of the global transform calls will work correctly.
757 case NOTIFICATION_POST_ENTER_TREE:
758 _update_simulator_path();
759 update_bone_id();
760 reset_to_rest_position();
761 reset_physics_simulation_state();
762 if (joint_data) {
763 _reload_joint();
764 }
765 break;
766
767 // If we're detached from the skeleton we need to
768 // clear our references to it.
769 case NOTIFICATION_UNPARENTED:
770 case NOTIFICATION_EXIT_TREE: {
771 PhysicalBoneSimulator3D *simulator = get_simulator();
772 if (simulator) {
773 if (bone_id != -1) {
774 simulator->unbind_physical_bone_from_bone(bone_id);
775 bone_id = -1;
776 }
777 }
778 PhysicsServer3D::get_singleton()->joint_clear(joint);
779 } break;
780
781 case NOTIFICATION_TRANSFORM_CHANGED: {
782 if (Engine::get_singleton()->is_editor_hint()) {
783 update_offset();
784 }
785 } break;
786 }
787 }
788
789 void PhysicalBone3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
790 set_ignore_transform_notification(true);
791 set_global_transform(p_state->get_transform());
792 set_ignore_transform_notification(false);
793
794 linear_velocity = p_state->get_linear_velocity();
795 angular_velocity = p_state->get_angular_velocity();
796 }
797
798 void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
799 if (!simulate_physics || !_internal_simulate_physics) {
800 return;
801 }
802
803 if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
804 _sync_body_state(p_state);
805
806 Transform3D old_transform = get_global_transform();
807 GDVIRTUAL_CALL(_integrate_forces, p_state);
808 Transform3D new_transform = get_global_transform();
809
810 if (new_transform != old_transform) {
811 // Update the physics server with the new transform, to prevent it from being overwritten at the sync below.
812 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
813 }
814 }
815
816 _sync_body_state(p_state);
817 _on_transform_changed();
818
819 Transform3D global_transform(p_state->get_transform());
820
821 // Update simulator
822 PhysicalBoneSimulator3D *simulator = get_simulator();
823 Skeleton3D *skeleton = get_skeleton();
824 if (simulator && skeleton) {
825 if (bone_id != -1) {
826 simulator->set_bone_global_pose(bone_id, skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse));
827 }
828 }
829 }
830
831 void PhysicalBone3D::_bind_methods() {
832 ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicalBone3D::apply_central_impulse);
833 ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicalBone3D::apply_impulse, Vector3());
834
835 ClassDB::bind_method(D_METHOD("set_joint_type", "joint_type"), &PhysicalBone3D::set_joint_type);
836 ClassDB::bind_method(D_METHOD("get_joint_type"), &PhysicalBone3D::get_joint_type);
837
838 ClassDB::bind_method(D_METHOD("set_joint_offset", "offset"), &PhysicalBone3D::set_joint_offset);
839 ClassDB::bind_method(D_METHOD("get_joint_offset"), &PhysicalBone3D::get_joint_offset);
840 ClassDB::bind_method(D_METHOD("set_joint_rotation", "euler"), &PhysicalBone3D::set_joint_rotation);
841 ClassDB::bind_method(D_METHOD("get_joint_rotation"), &PhysicalBone3D::get_joint_rotation);
842
843 ClassDB::bind_method(D_METHOD("set_body_offset", "offset"), &PhysicalBone3D::set_body_offset);
844 ClassDB::bind_method(D_METHOD("get_body_offset"), &PhysicalBone3D::get_body_offset);
845
846 ClassDB::bind_method(D_METHOD("get_simulate_physics"), &PhysicalBone3D::get_simulate_physics);
847
848 ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBone3D::is_simulating_physics);
849
850 ClassDB::bind_method(D_METHOD("get_bone_id"), &PhysicalBone3D::get_bone_id);
851
852 ClassDB::bind_method(D_METHOD("set_mass", "mass"), &PhysicalBone3D::set_mass);
853 ClassDB::bind_method(D_METHOD("get_mass"), &PhysicalBone3D::get_mass);
854
855 ClassDB::bind_method(D_METHOD("set_friction", "friction"), &PhysicalBone3D::set_friction);
856 ClassDB::bind_method(D_METHOD("get_friction"), &PhysicalBone3D::get_friction);
857
858 ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &PhysicalBone3D::set_bounce);
859 ClassDB::bind_method(D_METHOD("get_bounce"), &PhysicalBone3D::get_bounce);
860
861 ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &PhysicalBone3D::set_gravity_scale);
862 ClassDB::bind_method(D_METHOD("get_gravity_scale"), &PhysicalBone3D::get_gravity_scale);
863
864 ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &PhysicalBone3D::set_linear_damp_mode);
865 ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &PhysicalBone3D::get_linear_damp_mode);
866
867 ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &PhysicalBone3D::set_angular_damp_mode);
868 ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &PhysicalBone3D::get_angular_damp_mode);
869
870 ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &PhysicalBone3D::set_linear_damp);
871 ClassDB::bind_method(D_METHOD("get_linear_damp"), &PhysicalBone3D::get_linear_damp);
872
873 ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &PhysicalBone3D::set_angular_damp);
874 ClassDB::bind_method(D_METHOD("get_angular_damp"), &PhysicalBone3D::get_angular_damp);
875
876 ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &PhysicalBone3D::set_linear_velocity);
877 ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicalBone3D::get_linear_velocity);
878
879 ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &PhysicalBone3D::set_angular_velocity);
880 ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicalBone3D::get_angular_velocity);
881
882 ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &PhysicalBone3D::set_use_custom_integrator);
883 ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &PhysicalBone3D::is_using_custom_integrator);
884
885 ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &PhysicalBone3D::set_can_sleep);
886 ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &PhysicalBone3D::is_able_to_sleep);
887
888 GDVIRTUAL_BIND(_integrate_forces, "state");
889
890 ADD_GROUP("Joint", "joint_");
891 ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type", PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint"), "set_joint_type", "get_joint_type");
892 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "joint_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_joint_offset", "get_joint_offset");
893 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "joint_rotation", PROPERTY_HINT_RANGE, "-360,360,0.01,or_less,or_greater,radians_as_degrees"), "set_joint_rotation", "get_joint_rotation");
894
895 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "body_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_body_offset", "get_body_offset");
896
897 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
898 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction");
899 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce");
900 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-8,8,0.001,or_less,or_greater"), "set_gravity_scale", "get_gravity_scale");
901 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
902 ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
903 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
904 ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
905 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
906 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_linear_velocity", "get_linear_velocity");
907 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
908 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
909
910 BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
911 BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
912
913 BIND_ENUM_CONSTANT(JOINT_TYPE_NONE);
914 BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
915 BIND_ENUM_CONSTANT(JOINT_TYPE_CONE);
916 BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE);
917 BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER);
918 BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF);
919 }
920
921 void PhysicalBone3D::_update_joint_offset() {
922 _fix_joint_offset();
923
924 set_ignore_transform_notification(true);
925 reset_to_rest_position();
926 set_ignore_transform_notification(false);
927
928 #ifdef TOOLS_ENABLED
929 update_gizmos();
930 #endif
931 }
932
933 void PhysicalBone3D::_fix_joint_offset() {
934 // Clamp joint origin to bone origin
935 PhysicalBoneSimulator3D *simulator = get_simulator();
936 if (simulator) {
937 joint_offset.origin = body_offset.affine_inverse().origin;
938 }
939 }
940
941 void PhysicalBone3D::_reload_joint() {
942 PhysicalBoneSimulator3D *simulator = get_simulator();
943 if (!simulator || !simulator->get_skeleton()) {
944 PhysicsServer3D::get_singleton()->joint_clear(joint);
945 return;
946 }
947
948 PhysicalBone3D *body_a = simulator->get_physical_bone_parent(bone_id);
949 if (!body_a) {
950 PhysicsServer3D::get_singleton()->joint_clear(joint);
951 return;
952 }
953
954 Transform3D joint_transf = get_global_transform() * joint_offset;
955 Transform3D local_a = body_a->get_global_transform().affine_inverse() * joint_transf;
956 local_a.orthonormalize();
957
958 switch (get_joint_type()) {
959 case JOINT_TYPE_PIN: {
960 PhysicsServer3D::get_singleton()->joint_make_pin(joint, body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin);
961 const PinJointData *pjd(static_cast<const PinJointData *>(joint_data));
962 PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_BIAS, pjd->bias);
963 PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_DAMPING, pjd->damping);
964 PhysicsServer3D::get_singleton()->pin_joint_set_param(joint, PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp);
965
966 } break;
967 case JOINT_TYPE_CONE: {
968 PhysicsServer3D::get_singleton()->joint_make_cone_twist(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
969 const ConeJointData *cjd(static_cast<const ConeJointData *>(joint_data));
970 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span);
971 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span);
972 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_BIAS, cjd->bias);
973 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS, cjd->softness);
974 PhysicsServer3D::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation);
975
976 } break;
977 case JOINT_TYPE_HINGE: {
978 PhysicsServer3D::get_singleton()->joint_make_hinge(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
979 const HingeJointData *hjd(static_cast<const HingeJointData *>(joint_data));
980 PhysicsServer3D::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled);
981 PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper);
982 PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower);
983 PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias);
984 PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness);
985 PhysicsServer3D::get_singleton()->hinge_joint_set_param(joint, PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation);
986
987 } break;
988 case JOINT_TYPE_SLIDER: {
989 PhysicsServer3D::get_singleton()->joint_make_slider(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
990 const SliderJointData *sjd(static_cast<const SliderJointData *>(joint_data));
991 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper);
992 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower);
993 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness);
994 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution);
995 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution);
996 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper);
997 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower);
998 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
999 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness);
1000 PhysicsServer3D::get_singleton()->slider_joint_set_param(joint, PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping);
1001
1002 } break;
1003 case JOINT_TYPE_6DOF: {
1004 PhysicsServer3D::get_singleton()->joint_make_generic_6dof(joint, body_a->get_rid(), local_a, get_rid(), joint_offset);
1005 const SixDOFJointData *g6dofjd(static_cast<const SixDOFJointData *>(joint_data));
1006 for (int axis = 0; axis < 3; ++axis) {
1007 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled);
1008 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper);
1009 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower);
1010 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness);
1011 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled);
1012 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness);
1013 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping);
1014 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point);
1015 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution);
1016 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping);
1017 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled);
1018 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper);
1019 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower);
1020 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness);
1021 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution);
1022 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping);
1023 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp);
1024 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled);
1025 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness);
1026 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping);
1027 PhysicsServer3D::get_singleton()->generic_6dof_joint_set_param(joint, static_cast<Vector3::Axis>(axis), PhysicsServer3D::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point);
1028 }
1029
1030 } break;
1031 case JOINT_TYPE_NONE: {
1032 } break;
1033 }
1034 }
1035
1036 void PhysicalBone3D::_on_bone_parent_changed() {
1037 _reload_joint();
1038 }
1039
1040 void PhysicalBone3D::_update_simulator_path() {
1041 simulator_id = ObjectID();
1042 PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(get_parent());
1043 if (sim) {
1044 simulator_id = sim->get_instance_id();
1045 return;
1046 }
1047 #ifndef DISABLE_DEPRECATED
1048 Skeleton3D *sk = cast_to<Skeleton3D>(get_parent());
1049 if (sk) {
1050 PhysicalBoneSimulator3D *ssim = cast_to<PhysicalBoneSimulator3D>(sk->get_simulator());
1051 if (ssim) {
1052 simulator_id = ssim->get_instance_id();
1053 }
1054 }
1055 #endif // _DISABLE_DEPRECATED
1056 }
1057
1058 PhysicalBoneSimulator3D *PhysicalBone3D::get_simulator() const {
1059 return ObjectDB::get_instance<PhysicalBoneSimulator3D>(simulator_id);
1060 }
1061
1062 Skeleton3D *PhysicalBone3D::get_skeleton() const {
1063 PhysicalBoneSimulator3D *simulator = get_simulator();
1064 if (simulator) {
1065 return simulator->get_skeleton();
1066 }
1067 return nullptr;
1068 }
1069
1070 #ifdef TOOLS_ENABLED
1071 void PhysicalBone3D::_set_gizmo_move_joint(bool p_move_joint) {
1072 gizmo_move_joint = p_move_joint;
1073 }
1074
1075 Transform3D PhysicalBone3D::get_global_gizmo_transform() const {
1076 return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform();
1077 }
1078
1079 Transform3D PhysicalBone3D::get_local_gizmo_transform() const {
1080 return gizmo_move_joint ? get_transform() * joint_offset : get_transform();
1081 }
1082 #endif
1083
1084 const PhysicalBone3D::JointData *PhysicalBone3D::get_joint_data() const {
1085 return joint_data;
1086 }
1087
1088 void PhysicalBone3D::set_joint_type(JointType p_joint_type) {
1089 if (p_joint_type == get_joint_type()) {
1090 return;
1091 }
1092
1093 if (joint_data) {
1094 memdelete(joint_data);
1095 }
1096 joint_data = nullptr;
1097 switch (p_joint_type) {
1098 case JOINT_TYPE_PIN:
1099 joint_data = memnew(PinJointData);
1100 break;
1101 case JOINT_TYPE_CONE:
1102 joint_data = memnew(ConeJointData);
1103 break;
1104 case JOINT_TYPE_HINGE:
1105 joint_data = memnew(HingeJointData);
1106 break;
1107 case JOINT_TYPE_SLIDER:
1108 joint_data = memnew(SliderJointData);
1109 break;
1110 case JOINT_TYPE_6DOF:
1111 joint_data = memnew(SixDOFJointData);
1112 break;
1113 case JOINT_TYPE_NONE:
1114 break;
1115 }
1116
1117 _reload_joint();
1118
1119 #ifdef TOOLS_ENABLED
1120 notify_property_list_changed();
1121 update_gizmos();
1122 #endif
1123 }
1124
1125 PhysicalBone3D::JointType PhysicalBone3D::get_joint_type() const {
1126 return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE;
1127 }
1128
1129 void PhysicalBone3D::set_joint_offset(const Transform3D &p_offset) {
1130 joint_offset = p_offset;
1131
1132 _update_joint_offset();
1133 }
1134
1135 const Transform3D &PhysicalBone3D::get_joint_offset() const {
1136 return joint_offset;
1137 }
1138
1139 void PhysicalBone3D::set_joint_rotation(const Vector3 &p_euler_rad) {
1140 joint_offset.basis.set_euler_scale(p_euler_rad, joint_offset.basis.get_scale());
1141
1142 _update_joint_offset();
1143 }
1144
1145 Vector3 PhysicalBone3D::get_joint_rotation() const {
1146 return joint_offset.basis.get_euler_normalized();
1147 }
1148
1149 const Transform3D &PhysicalBone3D::get_body_offset() const {
1150 return body_offset;
1151 }
1152
1153 void PhysicalBone3D::set_body_offset(const Transform3D &p_offset) {
1154 body_offset = p_offset;
1155 body_offset_inverse = body_offset.affine_inverse();
1156
1157 _update_joint_offset();
1158 }
1159
1160 void PhysicalBone3D::set_simulate_physics(bool p_simulate) {
1161 if (simulate_physics == p_simulate) {
1162 return;
1163 }
1164
1165 simulate_physics = p_simulate;
1166 reset_physics_simulation_state();
1167 }
1168
1169 bool PhysicalBone3D::get_simulate_physics() {
1170 return simulate_physics;
1171 }
1172
1173 bool PhysicalBone3D::is_simulating_physics() {
1174 return _internal_simulate_physics;
1175 }
1176
1177 void PhysicalBone3D::set_bone_name(const String &p_name) {
1178 bone_name = p_name;
1179 bone_id = -1;
1180
1181 update_bone_id();
1182 reset_to_rest_position();
1183 }
1184
1185 const String &PhysicalBone3D::get_bone_name() const {
1186 return bone_name;
1187 }
1188
1189 void PhysicalBone3D::set_mass(real_t p_mass) {
1190 ERR_FAIL_COND(p_mass <= 0);
1191 mass = p_mass;
1192 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass);
1193 }
1194
1195 real_t PhysicalBone3D::get_mass() const {
1196 return mass;
1197 }
1198
1199 void PhysicalBone3D::set_friction(real_t p_friction) {
1200 ERR_FAIL_COND(p_friction < 0 || p_friction > 1);
1201
1202 friction = p_friction;
1203 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, friction);
1204 }
1205
1206 real_t PhysicalBone3D::get_friction() const {
1207 return friction;
1208 }
1209
1210 void PhysicalBone3D::set_bounce(real_t p_bounce) {
1211 ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
1212
1213 bounce = p_bounce;
1214 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, bounce);
1215 }
1216
1217 real_t PhysicalBone3D::get_bounce() const {
1218 return bounce;
1219 }
1220
1221 void PhysicalBone3D::set_gravity_scale(real_t p_gravity_scale) {
1222 gravity_scale = p_gravity_scale;
1223 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
1224 }
1225
1226 real_t PhysicalBone3D::get_gravity_scale() const {
1227 return gravity_scale;
1228 }
1229
1230 void PhysicalBone3D::set_linear_damp_mode(DampMode p_mode) {
1231 linear_damp_mode = p_mode;
1232 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
1233 }
1234
1235 PhysicalBone3D::DampMode PhysicalBone3D::get_linear_damp_mode() const {
1236 return linear_damp_mode;
1237 }
1238
1239 void PhysicalBone3D::set_angular_damp_mode(DampMode p_mode) {
1240 angular_damp_mode = p_mode;
1241 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
1242 }
1243
1244 PhysicalBone3D::DampMode PhysicalBone3D::get_angular_damp_mode() const {
1245 return angular_damp_mode;
1246 }
1247
1248 void PhysicalBone3D::set_linear_damp(real_t p_linear_damp) {
1249 ERR_FAIL_COND(p_linear_damp < 0);
1250
1251 linear_damp = p_linear_damp;
1252 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp);
1253 }
1254
1255 real_t PhysicalBone3D::get_linear_damp() const {
1256 return linear_damp;
1257 }
1258
1259 void PhysicalBone3D::set_angular_damp(real_t p_angular_damp) {
1260 ERR_FAIL_COND(p_angular_damp < 0);
1261
1262 angular_damp = p_angular_damp;
1263 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
1264 }
1265
1266 real_t PhysicalBone3D::get_angular_damp() const {
1267 return angular_damp;
1268 }
1269
1270 void PhysicalBone3D::set_can_sleep(bool p_active) {
1271 can_sleep = p_active;
1272 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active);
1273 }
1274
1275 bool PhysicalBone3D::is_able_to_sleep() const {
1276 return can_sleep;
1277 }
1278
1279 PhysicalBone3D::PhysicalBone3D() :
1280 PhysicsBody3D(PhysicsServer3D::BODY_MODE_STATIC) {
1281 joint = PhysicsServer3D::get_singleton()->joint_create();
1282 reset_physics_simulation_state();
1283 }
1284
1285 PhysicalBone3D::~PhysicalBone3D() {
1286 if (joint_data) {
1287 memdelete(joint_data);
1288 }
1289 ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
1290 PhysicsServer3D::get_singleton()->free_rid(joint);
1291 }
1292
1293 void PhysicalBone3D::update_bone_id() {
1294 PhysicalBoneSimulator3D *simulator = get_simulator();
1295 if (!simulator) {
1296 return;
1297 }
1298
1299 const int new_bone_id = simulator->find_bone(bone_name);
1300
1301 if (new_bone_id != bone_id) {
1302 if (bone_id != -1) {
1303 // Assert the unbind from old node
1304 simulator->unbind_physical_bone_from_bone(bone_id);
1305 }
1306
1307 bone_id = new_bone_id;
1308
1309 simulator->bind_physical_bone_to_bone(bone_id, this);
1310
1311 _fix_joint_offset();
1312 reset_physics_simulation_state();
1313 }
1314 }
1315
1316 void PhysicalBone3D::update_offset() {
1317 #ifdef TOOLS_ENABLED
1318 PhysicalBoneSimulator3D *simulator = get_simulator();
1319 Skeleton3D *skeleton = get_skeleton();
1320 if (simulator && skeleton) {
1321 Transform3D bone_transform(skeleton->get_global_transform());
1322 if (bone_id != -1) {
1323 bone_transform *= simulator->get_bone_global_pose(bone_id);
1324 }
1325
1326 if (gizmo_move_joint) {
1327 bone_transform *= body_offset;
1328 set_joint_offset(bone_transform.affine_inverse() * get_global_transform());
1329 } else {
1330 set_body_offset(bone_transform.affine_inverse() * get_global_transform());
1331 }
1332 }
1333 #endif
1334 }
1335
1336 void PhysicalBone3D::_start_physics_simulation() {
1337 if (_internal_simulate_physics || !simulator_id.is_valid() || bone_id == -1) {
1338 return;
1339 }
1340 reset_to_rest_position();
1341 set_body_mode(PhysicsServer3D::BODY_MODE_RIGID);
1342 PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
1343 PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
1344 PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
1345 PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &PhysicalBone3D::_body_state_changed));
1346 set_as_top_level(true);
1347 _internal_simulate_physics = true;
1348 }
1349
1350 void PhysicalBone3D::_stop_physics_simulation() {
1351 PhysicalBoneSimulator3D *simulator = get_simulator();
1352 if (simulator) {
1353 if (simulator->is_active() && bone_id != -1) {
1354 set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
1355 PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
1356 PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
1357 PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), get_collision_priority());
1358 } else {
1359 set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
1360 PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), 0);
1361 PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), 0);
1362 PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), 1.0);
1363 }
1364 }
1365 if (_internal_simulate_physics) {
1366 PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), Callable());
1367 set_as_top_level(false);
1368 _internal_simulate_physics = false;
1369 }
1370 }