jolt_hinge_joint_3d.cpp 15 KB

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