Skip to content
0.2.0-dev Download ZIP
420 lines ·15.1 KB Raw
1/**************************************************************************/
2/* jolt_hinge_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_hinge_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 "core/config/engine.h"
38
39#include <Jolt/Physics/Constraints/FixedConstraint.h>
40#include <Jolt/Physics/Constraints/HingeConstraint.h>
41
42namespace {
43
44constexpr double HINGE_DEFAULT_BIAS = 0.3;
45constexpr double HINGE_DEFAULT_LIMIT_BIAS = 0.3;
46constexpr double HINGE_DEFAULT_SOFTNESS = 0.9;
47constexpr double HINGE_DEFAULT_RELAXATION = 1.0;
48
49double estimate_physics_step() {
50 Engine *engine = Engine::get_singleton();
51
52 const double step = 1.0 / engine->get_user_physics_ticks_per_second();
53 const double step_scaled = step * engine->get_effective_time_scale();
54
55 return step_scaled;
56}
57
58} // namespace
59
60JPH::Constraint *JoltHingeJoint3D::_build_hinge(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 {
61 JPH::HingeConstraintSettings constraint_settings;
62
63 constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
64 constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);
65 constraint_settings.mHingeAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Z));
66 constraint_settings.mNormalAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));
67 constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);
68 constraint_settings.mHingeAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Z));
69 constraint_settings.mNormalAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));
70 constraint_settings.mLimitsMin = -p_limit;
71 constraint_settings.mLimitsMax = p_limit;
72
73 if (limit_spring_enabled) {
74 constraint_settings.mLimitsSpringSettings.mFrequency = (float)limit_spring_frequency;
75 constraint_settings.mLimitsSpringSettings.mDamping = (float)limit_spring_damping;
76 }
77
78 if (p_jolt_body_a == nullptr) {
79 return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
80 } else if (p_jolt_body_b == nullptr) {
81 return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
82 } else {
83 return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
84 }
85}
86
87JPH::Constraint *JoltHingeJoint3D::_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 {
88 JPH::FixedConstraintSettings constraint_settings;
89
90 constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
91 constraint_settings.mAutoDetectPoint = false;
92 constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);
93 constraint_settings.mAxisX1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));
94 constraint_settings.mAxisY1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Y));
95 constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);
96 constraint_settings.mAxisX2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));
97 constraint_settings.mAxisY2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Y));
98
99 if (p_jolt_body_a == nullptr) {
100 return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
101 } else if (p_jolt_body_b == nullptr) {
102 return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
103 } else {
104 return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
105 }
106}
107
108void JoltHingeJoint3D::_update_motor_state() {
109 if (unlikely(_is_fixed())) {
110 return;
111 }
112
113 if (JPH::HingeConstraint *constraint = static_cast<JPH::HingeConstraint *>(jolt_ref.GetPtr())) {
114 constraint->SetMotorState(motor_enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
115 }
116}
117
118void JoltHingeJoint3D::_update_motor_velocity() {
119 if (unlikely(_is_fixed())) {
120 return;
121 }
122
123 if (JPH::HingeConstraint *constraint = static_cast<JPH::HingeConstraint *>(jolt_ref.GetPtr())) {
124 // We flip the direction since Jolt is CCW but Godot is CW.
125 constraint->SetTargetAngularVelocity((float)-motor_target_speed);
126 }
127}
128
129void JoltHingeJoint3D::_update_motor_limit() {
130 if (unlikely(_is_fixed())) {
131 return;
132 }
133
134 if (JPH::HingeConstraint *constraint = static_cast<JPH::HingeConstraint *>(jolt_ref.GetPtr())) {
135 JPH::MotorSettings &motor_settings = constraint->GetMotorSettings();
136 motor_settings.mMinTorqueLimit = (float)-motor_max_torque;
137 motor_settings.mMaxTorqueLimit = (float)motor_max_torque;
138 }
139}
140
141void JoltHingeJoint3D::_limits_changed() {
142 rebuild();
143 _wake_up_bodies();
144}
145
146void JoltHingeJoint3D::_limit_spring_changed() {
147 rebuild();
148 _wake_up_bodies();
149}
150
151void JoltHingeJoint3D::_motor_state_changed() {
152 _update_motor_state();
153 _wake_up_bodies();
154}
155
156void JoltHingeJoint3D::_motor_speed_changed() {
157 _update_motor_velocity();
158 _wake_up_bodies();
159}
160
161void JoltHingeJoint3D::_motor_limit_changed() {
162 _update_motor_limit();
163 _wake_up_bodies();
164}
165
166JoltHingeJoint3D::JoltHingeJoint3D(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) :
167 JoltJoint3D(p_old_joint, p_body_a, p_body_b, p_local_ref_a, p_local_ref_b) {
168 rebuild();
169}
170
171double JoltHingeJoint3D::get_param(Parameter p_param) const {
172 switch (p_param) {
173 case PS3DE::HINGE_JOINT_BIAS: {
174 return HINGE_DEFAULT_BIAS;
175 }
176 case PS3DE::HINGE_JOINT_LIMIT_UPPER: {
177 return limit_upper;
178 }
179 case PS3DE::HINGE_JOINT_LIMIT_LOWER: {
180 return limit_lower;
181 }
182 case PS3DE::HINGE_JOINT_LIMIT_BIAS: {
183 return HINGE_DEFAULT_LIMIT_BIAS;
184 }
185 case PS3DE::HINGE_JOINT_LIMIT_SOFTNESS: {
186 return HINGE_DEFAULT_SOFTNESS;
187 }
188 case PS3DE::HINGE_JOINT_LIMIT_RELAXATION: {
189 return HINGE_DEFAULT_RELAXATION;
190 }
191 case PS3DE::HINGE_JOINT_MOTOR_TARGET_VELOCITY: {
192 return motor_target_speed;
193 }
194 case PS3DE::HINGE_JOINT_MOTOR_MAX_IMPULSE: {
195 // With Godot using max impulse instead of max torque we don't have much choice but to calculate this and hope the timestep doesn't change.
196 return motor_max_torque * estimate_physics_step();
197 }
198 default: {
199 ERR_FAIL_V_MSG(0.0, vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
200 }
201 }
202}
203
204void JoltHingeJoint3D::set_param(Parameter p_param, double p_value) {
205 switch (p_param) {
206 case PS3DE::HINGE_JOINT_BIAS: {
207 if (!Math::is_equal_approx(p_value, HINGE_DEFAULT_BIAS)) {
208 WARN_PRINT(vformat("Hinge joint bias is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
209 }
210 } break;
211 case PS3DE::HINGE_JOINT_LIMIT_UPPER: {
212 limit_upper = p_value;
213 _limits_changed();
214 } break;
215 case PS3DE::HINGE_JOINT_LIMIT_LOWER: {
216 limit_lower = p_value;
217 _limits_changed();
218 } break;
219 case PS3DE::HINGE_JOINT_LIMIT_BIAS: {
220 if (!Math::is_equal_approx(p_value, HINGE_DEFAULT_LIMIT_BIAS)) {
221 WARN_PRINT(vformat("Hinge joint bias limit is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
222 }
223 } break;
224 case PS3DE::HINGE_JOINT_LIMIT_SOFTNESS: {
225 if (!Math::is_equal_approx(p_value, HINGE_DEFAULT_SOFTNESS)) {
226 WARN_PRINT(vformat("Hinge joint softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
227 }
228 } break;
229 case PS3DE::HINGE_JOINT_LIMIT_RELAXATION: {
230 if (!Math::is_equal_approx(p_value, HINGE_DEFAULT_RELAXATION)) {
231 WARN_PRINT(vformat("Hinge joint relaxation is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
232 }
233 } break;
234 case PS3DE::HINGE_JOINT_MOTOR_TARGET_VELOCITY: {
235 motor_target_speed = p_value;
236 _motor_speed_changed();
237 } break;
238 case PS3DE::HINGE_JOINT_MOTOR_MAX_IMPULSE: {
239 // With Godot using max impulse instead of max torque we don't have much choice but to calculate this and hope the timestep doesn't change.
240 motor_max_torque = p_value / estimate_physics_step();
241 _motor_limit_changed();
242 } break;
243 default: {
244 ERR_FAIL_MSG(vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
245 } break;
246 }
247}
248
249double JoltHingeJoint3D::get_jolt_param(JoltParameter p_param) const {
250 switch (p_param) {
251 case JoltPhysicsServer3D::HINGE_JOINT_LIMIT_SPRING_FREQUENCY: {
252 return limit_spring_frequency;
253 }
254 case JoltPhysicsServer3D::HINGE_JOINT_LIMIT_SPRING_DAMPING: {
255 return limit_spring_damping;
256 }
257 case JoltPhysicsServer3D::HINGE_JOINT_MOTOR_MAX_TORQUE: {
258 return motor_max_torque;
259 }
260 default: {
261 ERR_FAIL_V_MSG(0.0, vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
262 }
263 }
264}
265
266void JoltHingeJoint3D::set_jolt_param(JoltParameter p_param, double p_value) {
267 switch (p_param) {
268 case JoltPhysicsServer3D::HINGE_JOINT_LIMIT_SPRING_FREQUENCY: {
269 limit_spring_frequency = p_value;
270 _limit_spring_changed();
271 } break;
272 case JoltPhysicsServer3D::HINGE_JOINT_LIMIT_SPRING_DAMPING: {
273 limit_spring_damping = p_value;
274 _limit_spring_changed();
275 } break;
276 case JoltPhysicsServer3D::HINGE_JOINT_MOTOR_MAX_TORQUE: {
277 motor_max_torque = p_value;
278 _motor_limit_changed();
279 } break;
280 default: {
281 ERR_FAIL_MSG(vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
282 } break;
283 }
284}
285
286bool JoltHingeJoint3D::get_flag(Flag p_flag) const {
287 switch (p_flag) {
288 case PS3DE::HINGE_JOINT_FLAG_USE_LIMIT: {
289 return limits_enabled;
290 }
291 case PS3DE::HINGE_JOINT_FLAG_ENABLE_MOTOR: {
292 return motor_enabled;
293 }
294 default: {
295 ERR_FAIL_V_MSG(false, vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
296 }
297 }
298}
299
300void JoltHingeJoint3D::set_flag(Flag p_flag, bool p_enabled) {
301 switch (p_flag) {
302 case PS3DE::HINGE_JOINT_FLAG_USE_LIMIT: {
303 limits_enabled = p_enabled;
304 _limits_changed();
305 } break;
306 case PS3DE::HINGE_JOINT_FLAG_ENABLE_MOTOR: {
307 motor_enabled = p_enabled;
308 _motor_state_changed();
309 } break;
310 default: {
311 ERR_FAIL_MSG(vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
312 } break;
313 }
314}
315
316bool JoltHingeJoint3D::get_jolt_flag(JoltFlag p_flag) const {
317 switch (p_flag) {
318 case JoltPhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT_SPRING: {
319 return limit_spring_enabled;
320 }
321 default: {
322 ERR_FAIL_V_MSG(false, vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
323 }
324 }
325}
326
327void JoltHingeJoint3D::set_jolt_flag(JoltFlag p_flag, bool p_enabled) {
328 switch (p_flag) {
329 case JoltPhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT_SPRING: {
330 limit_spring_enabled = p_enabled;
331 _limit_spring_changed();
332 } break;
333 default: {
334 ERR_FAIL_MSG(vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
335 } break;
336 }
337}
338
339float JoltHingeJoint3D::get_applied_force() const {
340 ERR_FAIL_NULL_V(jolt_ref, 0.0f);
341
342 JoltSpace3D *space = get_space();
343 ERR_FAIL_NULL_V(space, 0.0f);
344
345 const float last_step = space->get_last_step();
346 if (unlikely(last_step == 0.0f)) {
347 return 0.0f;
348 }
349
350 if (_is_fixed()) {
351 JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());
352 return constraint->GetTotalLambdaPosition().Length() / last_step;
353 } else {
354 JPH::HingeConstraint *constraint = static_cast<JPH::HingeConstraint *>(jolt_ref.GetPtr());
355 const JPH::Vec3 total_lambda = JPH::Vec3(constraint->GetTotalLambdaRotation()[0], constraint->GetTotalLambdaRotation()[1], constraint->GetTotalLambdaRotationLimits() + constraint->GetTotalLambdaMotor());
356 return total_lambda.Length() / last_step;
357 }
358}
359
360float JoltHingeJoint3D::get_applied_torque() const {
361 ERR_FAIL_NULL_V(jolt_ref, 0.0f);
362
363 JoltSpace3D *space = get_space();
364 ERR_FAIL_NULL_V(space, 0.0f);
365
366 const float last_step = space->get_last_step();
367 if (unlikely(last_step == 0.0f)) {
368 return 0.0f;
369 }
370
371 if (_is_fixed()) {
372 JPH::FixedConstraint *constraint = static_cast<JPH::FixedConstraint *>(jolt_ref.GetPtr());
373 return constraint->GetTotalLambdaRotation().Length() / last_step;
374 } else {
375 JPH::HingeConstraint *constraint = static_cast<JPH::HingeConstraint *>(jolt_ref.GetPtr());
376 return constraint->GetTotalLambdaRotation().Length() / last_step;
377 }
378}
379
380void JoltHingeJoint3D::rebuild() {
381 destroy();
382
383 JoltSpace3D *space = get_space();
384 if (space == nullptr) {
385 return;
386 }
387
388 JPH::Body *jolt_body_a = body_a != nullptr ? body_a->get_jolt_body() : nullptr;
389 JPH::Body *jolt_body_b = body_b != nullptr ? body_b->get_jolt_body() : nullptr;
390 ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);
391
392 float ref_shift = 0.0f;
393 float limit = JPH::JPH_PI;
394
395 if (limits_enabled && limit_lower <= limit_upper) {
396 const double limit_midpoint = (limit_lower + limit_upper) / 2.0f;
397
398 ref_shift = float(-limit_midpoint);
399 limit = float(limit_upper - limit_midpoint);
400 }
401
402 Transform3D shifted_ref_a;
403 Transform3D shifted_ref_b;
404
405 _shift_reference_frames(Vector3(), Vector3(0.0f, 0.0f, ref_shift), shifted_ref_a, shifted_ref_b);
406
407 if (_is_fixed()) {
408 jolt_ref = _build_fixed(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b);
409 } else {
410 jolt_ref = _build_hinge(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b, limit);
411 }
412
413 space->add_joint(this);
414
415 _update_joint();
416
417 _update_motor_state();
418 _update_motor_velocity();
419 _update_motor_limit();
420}