jolt_pin_joint_3d.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**************************************************************************/
  2. /* jolt_pin_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_pin_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 "Jolt/Physics/Constraints/PointConstraint.h"
  35. namespace {
  36. constexpr double DEFAULT_BIAS = 0.3;
  37. constexpr double DEFAULT_DAMPING = 1.0;
  38. constexpr double DEFAULT_IMPULSE_CLAMP = 0.0;
  39. } // namespace
  40. JPH::Constraint *JoltPinJoint3D::_build_pin(JPH::Body *p_jolt_body_a, JPH::Body *p_jolt_body_b, const Transform3D &p_shifted_ref_a, const Transform3D &p_shifted_ref_b) {
  41. JPH::PointConstraintSettings constraint_settings;
  42. constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
  43. constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin);
  44. constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin);
  45. if (p_jolt_body_a == nullptr) {
  46. return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
  47. } else if (p_jolt_body_b == nullptr) {
  48. return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
  49. } else {
  50. return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
  51. }
  52. }
  53. void JoltPinJoint3D::_points_changed() {
  54. rebuild();
  55. _wake_up_bodies();
  56. }
  57. JoltPinJoint3D::JoltPinJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, JoltBody3D *p_body_b, const Vector3 &p_local_a, const Vector3 &p_local_b) :
  58. JoltJoint3D(p_old_joint, p_body_a, p_body_b, Transform3D({}, p_local_a), Transform3D({}, p_local_b)) {
  59. rebuild();
  60. }
  61. void JoltPinJoint3D::set_local_a(const Vector3 &p_local_a) {
  62. local_ref_a = Transform3D({}, p_local_a);
  63. _points_changed();
  64. }
  65. void JoltPinJoint3D::set_local_b(const Vector3 &p_local_b) {
  66. local_ref_b = Transform3D({}, p_local_b);
  67. _points_changed();
  68. }
  69. double JoltPinJoint3D::get_param(PhysicsServer3D::PinJointParam p_param) const {
  70. switch (p_param) {
  71. case PhysicsServer3D::PIN_JOINT_BIAS: {
  72. return DEFAULT_BIAS;
  73. }
  74. case PhysicsServer3D::PIN_JOINT_DAMPING: {
  75. return DEFAULT_DAMPING;
  76. }
  77. case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP: {
  78. return DEFAULT_IMPULSE_CLAMP;
  79. }
  80. default: {
  81. ERR_FAIL_V_MSG(0.0, vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param));
  82. }
  83. }
  84. }
  85. void JoltPinJoint3D::set_param(PhysicsServer3D::PinJointParam p_param, double p_value) {
  86. switch (p_param) {
  87. case PhysicsServer3D::PIN_JOINT_BIAS: {
  88. if (!Math::is_equal_approx(p_value, DEFAULT_BIAS)) {
  89. WARN_PRINT(vformat("Pin joint bias is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  90. }
  91. } break;
  92. case PhysicsServer3D::PIN_JOINT_DAMPING: {
  93. if (!Math::is_equal_approx(p_value, DEFAULT_DAMPING)) {
  94. WARN_PRINT(vformat("Pin joint damping is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  95. }
  96. } break;
  97. case PhysicsServer3D::PIN_JOINT_IMPULSE_CLAMP: {
  98. if (!Math::is_equal_approx(p_value, DEFAULT_IMPULSE_CLAMP)) {
  99. WARN_PRINT(vformat("Pin joint impulse clamp is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  100. }
  101. } break;
  102. default: {
  103. ERR_FAIL_MSG(vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param));
  104. } break;
  105. }
  106. }
  107. float JoltPinJoint3D::get_applied_force() const {
  108. JPH::PointConstraint *constraint = static_cast<JPH::PointConstraint *>(jolt_ref.GetPtr());
  109. ERR_FAIL_NULL_V(constraint, 0.0f);
  110. JoltSpace3D *space = get_space();
  111. ERR_FAIL_NULL_V(space, 0.0f);
  112. const float last_step = space->get_last_step();
  113. if (unlikely(last_step == 0.0f)) {
  114. return 0.0f;
  115. }
  116. return constraint->GetTotalLambdaPosition().Length() / last_step;
  117. }
  118. void JoltPinJoint3D::rebuild() {
  119. destroy();
  120. JoltSpace3D *space = get_space();
  121. if (space == nullptr) {
  122. return;
  123. }
  124. const JPH::BodyID body_ids[2] = {
  125. body_a != nullptr ? body_a->get_jolt_id() : JPH::BodyID(),
  126. body_b != nullptr ? body_b->get_jolt_id() : JPH::BodyID()
  127. };
  128. const JoltWritableBodies3D jolt_bodies = space->write_bodies(body_ids, 2);
  129. JPH::Body *jolt_body_a = static_cast<JPH::Body *>(jolt_bodies[0]);
  130. JPH::Body *jolt_body_b = static_cast<JPH::Body *>(jolt_bodies[1]);
  131. ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);
  132. Transform3D shifted_ref_a;
  133. Transform3D shifted_ref_b;
  134. _shift_reference_frames(Vector3(), Vector3(), shifted_ref_a, shifted_ref_b);
  135. jolt_ref = _build_pin(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b);
  136. space->add_joint(this);
  137. _update_enabled();
  138. _update_iterations();
  139. }