Debian Codegraph @

modules/jolt_physics/joints/jolt_slider_joint_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_slider_joint_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 "jolt_slider_joint_3d.h"
32
33 #include "../misc/jolt_type_conversions.h"
34 #include "../objects/jolt_body_3d.h"
35 #include "../spaces/jolt_space_3d.h"
36
37 #include "Jolt/Physics/Constraints/FixedConstraint.h"
38 #include "Jolt/Physics/Constraints/SliderConstraint.h"
39
40 namespace {
41
42 constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS = 1.0;
43 constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION = 0.7;
44 constexpr double SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING = 1.0;
45
46 constexpr double SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS = 1.0;
47 constexpr double SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION = 0.7;
48 constexpr double SLIDER_DEFAULT_LINEAR_MOTION_DAMPING = 0.0;
49
50 constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS = 1.0;
51 constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION = 0.7;
52 constexpr double SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING = 1.0;
53
54 constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER = 0.0;
55 constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER = 0.0;
56 constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS = 1.0;
57 constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION = 0.7;
58 constexpr double SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING = 0.0;
59
60 constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS = 1.0;
61 constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION = 0.7;
62 constexpr double SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING = 1.0;
63
64 constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS = 1.0;
65 constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION = 0.7;
66 constexpr double SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING = 1.0;
67
68 } // namespace
69
70 JPH::Constraint *JoltSliderJoint3D::_build_slider(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b, float p_limit) const {
71 JPH::SliderConstraintSettings constraint_settings;
72
73 constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
74 constraint_settings.mAutoDetectPoint = false;
75 constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);
76 constraint_settings.mSliderAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));
77 constraint_settings.mNormalAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Z));
78 constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);
79 constraint_settings.mSliderAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));
80 constraint_settings.mNormalAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Z));
81 constraint_settings.mLimitsMin = -p_limit;
82 constraint_settings.mLimitsMax = p_limit;
83
84 if (limit_spring_enabled) {
85 constraint_settings.mLimitsSpringSettings.mFrequency = (float)limit_spring_frequency;
86 constraint_settings.mLimitsSpringSettings.mDamping = (float)limit_spring_damping;
87 }
88
89 if (p_jolt_body_a == nullptr) {
90 return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
91 } else if (p_jolt_body_b == nullptr) {
92 return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
93 } else {
94 return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
95 }
96 }
97
98 JPH::Constraint *JoltSliderJoint3D::_build_fixed(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b) const {
99 JPH::FixedConstraintSettings constraint_settings;
100
101 constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
102 constraint_settings.mAutoDetectPoint = false;
103 constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);
104 constraint_settings.mAxisX1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));
105 constraint_settings.mAxisY1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Y));
106 constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);
107 constraint_settings.mAxisX2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));
108 constraint_settings.mAxisY2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Y));
109
110 if (p_jolt_body_a == nullptr) {
111 return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
112 } else if (p_jolt_body_b == nullptr) {
113 return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
114 } else {
115 return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
116 }
117 }
118
119 void JoltSliderJoint3D::_update_motor_state() {
120 if (unlikely(_is_fixed())) {
121 return;
122 }
123
124 if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {
125 constraint->SetMotorState(motor_enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
126 }
127 }
128
129 void JoltSliderJoint3D::_update_motor_velocity() {
130 if (unlikely(_is_fixed())) {
131 return;
132 }
133
134 if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {
135 constraint->SetTargetVelocity((float)motor_target_speed);
136 }
137 }
138
139 void JoltSliderJoint3D::_update_motor_limit() {
140 if (unlikely(_is_fixed())) {
141 return;
142 }
143
144 if (JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr())) {
145 JPH::MotorSettings &motor_settings = constraint->GetMotorSettings();
146 motor_settings.mMinForceLimit = (float)-motor_max_force;
147 motor_settings.mMaxForceLimit = (float)motor_max_force;
148 }
149 }
150
151 void JoltSliderJoint3D::_limits_changed() {
152 rebuild();
153 _wake_up_bodies();
154 }
155
156 void JoltSliderJoint3D::_limit_spring_changed() {
157 rebuild();
158 _wake_up_bodies();
159 }
160
161 void JoltSliderJoint3D::_motor_state_changed() {
162 _update_motor_state();
163 _wake_up_bodies();
164 }
165
166 void JoltSliderJoint3D::_motor_speed_changed() {
167 _update_motor_velocity();
168 _wake_up_bodies();
169 }
170
171 void JoltSliderJoint3D::_motor_limit_changed() {
172 _update_motor_limit();
173 _wake_up_bodies();
174 }
175
176 JoltSliderJoint3D::JoltSliderJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Transform3D &p_local_ref_a, const Transform3D &p_local_ref_b) :
177 JoltJoint3D(p_old_joint, p_body_a, p_body_b, p_local_ref_a, p_local_ref_b) {
178 rebuild();
179 }
180
181 double JoltSliderJoint3D::get_param(PhysicsServer3D::SliderJointParam p_param) const {
182 switch (p_param) {
183 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER: {
184 return limit_upper;
185 }
186 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER: {
187 return limit_lower;
188 }
189 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS: {
190 return SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS;
191 }
192 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION: {
193 return SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION;
194 }
195 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING: {
196 return SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING;
197 }
198 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS: {
199 return SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS;
200 }
201 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION: {
202 return SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION;
203 }
204 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_DAMPING: {
205 return SLIDER_DEFAULT_LINEAR_MOTION_DAMPING;
206 }
207 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS: {
208 return SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS;
209 }
210 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION: {
211 return SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION;
212 }
213 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING: {
214 return SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING;
215 }
216 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER: {
217 return SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER;
218 }
219 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER: {
220 return SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER;
221 }
222 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS: {
223 return SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS;
224 }
225 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION: {
226 return SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION;
227 }
228 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING: {
229 return SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING;
230 }
231 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS: {
232 return SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS;
233 }
234 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION: {
235 return SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION;
236 }
237 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_DAMPING: {
238 return SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING;
239 }
240 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS: {
241 return SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS;
242 }
243 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION: {
244 return SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION;
245 }
246 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING: {
247 return SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING;
248 }
249 default: {
250 ERR_FAIL_V_MSG(0.0, vformat("Unhandled slider joint parameter: '%d'. This should not happen. Please report this.", p_param));
251 }
252 }
253 }
254
255 void JoltSliderJoint3D::set_param(PhysicsServer3D::SliderJointParam p_param, double p_value) {
256 switch (p_param) {
257 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_UPPER: {
258 limit_upper = p_value;
259 _limits_changed();
260 } break;
261 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_LOWER: {
262 limit_lower = p_value;
263 _limits_changed();
264 } break;
265 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS: {
266 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_SOFTNESS)) {
267 WARN_PRINT(vformat("Slider joint linear limit softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
268 }
269 } break;
270 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION: {
271 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_RESTITUTION)) {
272 WARN_PRINT(vformat("Slider joint linear limit restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
273 }
274 } break;
275 case PhysicsServer3D::SLIDER_JOINT_LINEAR_LIMIT_DAMPING: {
276 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_LIMIT_DAMPING)) {
277 WARN_PRINT(vformat("Slider joint linear limit damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
278 }
279 } break;
280 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_SOFTNESS: {
281 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_SOFTNESS)) {
282 WARN_PRINT(vformat("Slider joint linear motion softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
283 }
284 } break;
285 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_RESTITUTION: {
286 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_RESTITUTION)) {
287 WARN_PRINT(vformat("Slider joint linear motion restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
288 }
289 } break;
290 case PhysicsServer3D::SLIDER_JOINT_LINEAR_MOTION_DAMPING: {
291 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_MOTION_DAMPING)) {
292 WARN_PRINT(vformat("Slider joint linear motion damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
293 }
294 } break;
295 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS: {
296 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_SOFTNESS)) {
297 WARN_PRINT(vformat("Slider joint linear ortho softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
298 }
299 } break;
300 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION: {
301 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_RESTITUTION)) {
302 WARN_PRINT(vformat("Slider joint linear ortho restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
303 }
304 } break;
305 case PhysicsServer3D::SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING: {
306 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_LINEAR_ORTHO_DAMPING)) {
307 WARN_PRINT(vformat("Slider joint linear ortho damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
308 }
309 } break;
310 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_UPPER: {
311 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_UPPER)) {
312 WARN_PRINT(vformat("Slider joint angular limits are not supported when using Jolt Physics. Any such value will be ignored. Try using Generic6DOFJoint3D instead. This joint connects %s.", _bodies_to_string()));
313 }
314 } break;
315 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_LOWER: {
316 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_LOWER)) {
317 WARN_PRINT(vformat("Slider joint angular limits are not supported when using Jolt Physics. Any such value will be ignored. Try using Generic6DOFJoint3D instead. This joint connects %s.", _bodies_to_string()));
318 }
319 } break;
320 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS: {
321 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_SOFTNESS)) {
322 WARN_PRINT(vformat("Slider joint angular limit softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
323 }
324 } break;
325 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION: {
326 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_RESTITUTION)) {
327 WARN_PRINT(vformat("Slider joint angular limit restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
328 }
329 } break;
330 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING: {
331 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_LIMIT_DAMPING)) {
332 WARN_PRINT(vformat("Slider joint angular limit damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
333 }
334 } break;
335 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS: {
336 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_SOFTNESS)) {
337 WARN_PRINT(vformat("Slider joint angular motion softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
338 }
339 } break;
340 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION: {
341 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_RESTITUTION)) {
342 WARN_PRINT(vformat("Slider joint angular motion restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
343 }
344 } break;
345 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_MOTION_DAMPING: {
346 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_MOTION_DAMPING)) {
347 WARN_PRINT(vformat("Slider joint angular motion damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
348 }
349 } break;
350 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS: {
351 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_SOFTNESS)) {
352 WARN_PRINT(vformat("Slider joint angular ortho softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
353 }
354 } break;
355 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION: {
356 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_RESTITUTION)) {
357 WARN_PRINT(vformat("Slider joint angular ortho restitution is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
358 }
359 } break;
360 case PhysicsServer3D::SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING: {
361 if (!Math::is_equal_approx(p_value, SLIDER_DEFAULT_ANGULAR_ORTHO_DAMPING)) {
362 WARN_PRINT(vformat("Slider joint angular ortho damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
363 }
364 } break;
365 default: {
366 ERR_FAIL_MSG(vformat("Unhandled slider joint parameter: '%d'. This should not happen. Please report this.", p_param));
367 } break;
368 }
369 }
370
371 double JoltSliderJoint3D::get_jolt_param(JoltParameter p_param) const {
372 switch (p_param) {
373 case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_FREQUENCY: {
374 return limit_spring_frequency;
375 }
376 case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_DAMPING: {
377 return limit_spring_damping;
378 }
379 case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_TARGET_VELOCITY: {
380 return motor_target_speed;
381 }
382 case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_MAX_FORCE: {
383 return motor_max_force;
384 }
385 default: {
386 ERR_FAIL_V_MSG(0.0, vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
387 }
388 }
389 }
390
391 void JoltSliderJoint3D::set_jolt_param(JoltParameter p_param, double p_value) {
392 switch (p_param) {
393 case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_FREQUENCY: {
394 limit_spring_frequency = p_value;
395 _limit_spring_changed();
396 } break;
397 case JoltPhysicsServer3D::SLIDER_JOINT_LIMIT_SPRING_DAMPING: {
398 limit_spring_damping = p_value;
399 _limit_spring_changed();
400 } break;
401 case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_TARGET_VELOCITY: {
402 motor_target_speed = p_value;
403 _motor_speed_changed();
404 } break;
405 case JoltPhysicsServer3D::SLIDER_JOINT_MOTOR_MAX_FORCE: {
406 motor_max_force = p_value;
407 _motor_limit_changed();
408 } break;
409 default: {
410 ERR_FAIL_MSG(vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
411 } break;
412 }
413 }
414
415 bool JoltSliderJoint3D::get_jolt_flag(JoltFlag p_flag) const {
416 switch (p_flag) {
417 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT: {
418 return limits_enabled;
419 }
420 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT_SPRING: {
421 return limit_spring_enabled;
422 }
423 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_ENABLE_MOTOR: {
424 return motor_enabled;
425 }
426 default: {
427 ERR_FAIL_V_MSG(false, vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
428 }
429 }
430 }
431
432 void JoltSliderJoint3D::set_jolt_flag(JoltFlag p_flag, bool p_enabled) {
433 switch (p_flag) {
434 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT: {
435 limits_enabled = p_enabled;
436 _limits_changed();
437 } break;
438 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_USE_LIMIT_SPRING: {
439 limit_spring_enabled = p_enabled;
440 _limit_spring_changed();
441 } break;
442 case JoltPhysicsServer3D::SLIDER_JOINT_FLAG_ENABLE_MOTOR: {
443 motor_enabled = p_enabled;
444 _motor_state_changed();
445 } break;
446 default: {
447 ERR_FAIL_MSG(vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
448 } break;
449 }
450 }
451
452 float JoltSliderJoint3D::get_applied_force() const {
453 ERR_FAIL_NULL_V(jolt_ref, 0.0f);
454
455 JoltSpace3D *space = get_space();
456 ERR_FAIL_NULL_V(space, 0.0f);
457
458 const float last_step = space->get_last_step();
459 if (unlikely(last_step == 0.0f)) {
460 return 0.0f;
461 }
462
463 if (_is_fixed()) {
464 JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());
465 return constraint->GetTotalLambdaPosition().Length() / last_step;
466 } else {
467 JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr());
468 const JPH::Vec3 total_lambda = JPH::Vec3(constraint->GetTotalLambdaPosition()[0], constraint->GetTotalLambdaPosition()[1], constraint->GetTotalLambdaPositionLimits() + constraint->GetTotalLambdaMotor());
469 return total_lambda.Length() / last_step;
470 }
471 }
472
473 float JoltSliderJoint3D::get_applied_torque() const {
474 ERR_FAIL_NULL_V(jolt_ref, 0.0f);
475
476 JoltSpace3D *space = get_space();
477 ERR_FAIL_NULL_V(space, 0.0f);
478
479 const float last_step = space->get_last_step();
480 if (unlikely(last_step == 0.0f)) {
481 return 0.0f;
482 }
483
484 if (_is_fixed()) {
485 JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());
486 return constraint->GetTotalLambdaRotation().Length() / last_step;
487 } else {
488 JPH::SliderConstraint *constraint = static_cast<JPH::SliderConstraint *>(jolt_ref.GetPtr());
489 return constraint->GetTotalLambdaRotation().Length() / last_step;
490 }
491 }
492
493 void JoltSliderJoint3D::rebuild() {
494 destroy();
495
496 JoltSpace3D *space = get_space();
497 if (space == nullptr) {
498 return;
499 }
500
501 JPH::Body *jolt_body_a = body_a != nullptr ? body_a->get_jolt_body() : nullptr;
502 JPH::Body *jolt_body_b = body_b != nullptr ? body_b->get_jolt_body() : nullptr;
503 ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);
504
505 float ref_shift = 0.0f;
506 float limit = FLT_MAX;
507
508 if (limits_enabled && limit_lower <= limit_upper) {
509 const double limit_midpoint = (limit_lower + limit_upper) / 2.0f;
510
511 ref_shift = float(-limit_midpoint);
512 limit = float(limit_upper - limit_midpoint);
513 }
514
515 Transform3D shifted_ref_a;
516 Transform3D shifted_ref_b;
517
518 _shift_reference_frames(Vector3(ref_shift, 0.0f, 0.0f), Vector3(), shifted_ref_a, shifted_ref_b);
519
520 if (_is_fixed()) {
521 jolt_ref = _build_fixed(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b);
522 } else {
523 jolt_ref = _build_slider(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b, limit);
524 }
525
526 space->add_joint(this);
527
528 _update_enabled();
529 _update_iterations();
530 _update_motor_state();
531 _update_motor_velocity();
532 _update_motor_limit();
533 }