0.2.0-dev Download ZIP
159 lines ·6.4 KB Raw
| 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 | |
| 31 | #include "jolt_pin_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/PointConstraint.h> |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | constexpr double PIN_DEFAULT_BIAS = 0.3; |
| 42 | constexpr double PIN_DEFAULT_DAMPING = 1.0; |
| 43 | constexpr double PIN_DEFAULT_IMPULSE_CLAMP = 0.0; |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | 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) { |
| 48 | JPH::PointConstraintSettings constraint_settings; |
| 49 | constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM; |
| 50 | constraint_settings.mPoint1 = to_jolt_r(p_shifted_ref_a.origin); |
| 51 | constraint_settings.mPoint2 = to_jolt_r(p_shifted_ref_b.origin); |
| 52 | |
| 53 | if (p_jolt_body_a == nullptr) { |
| 54 | return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b); |
| 55 | } else if (p_jolt_body_b == nullptr) { |
| 56 | return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld); |
| 57 | } else { |
| 58 | return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void JoltPinJoint3D::_points_changed() { |
| 63 | rebuild(); |
| 64 | _wake_up_bodies(); |
| 65 | } |
| 66 | |
| 67 | 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) : |
| 68 | JoltJoint3D(p_old_joint, p_body_a, p_body_b, Transform3D({}, p_local_a), Transform3D({}, p_local_b)) { |
| 69 | rebuild(); |
| 70 | } |
| 71 | |
| 72 | void JoltPinJoint3D::set_local_a(const Vector3 &p_local_a) { |
| 73 | local_ref_a = Transform3D({}, p_local_a); |
| 74 | _points_changed(); |
| 75 | } |
| 76 | |
| 77 | void JoltPinJoint3D::set_local_b(const Vector3 &p_local_b) { |
| 78 | local_ref_b = Transform3D({}, p_local_b); |
| 79 | _points_changed(); |
| 80 | } |
| 81 | |
| 82 | double JoltPinJoint3D::get_param(PS3DE::PinJointParam p_param) const { |
| 83 | switch (p_param) { |
| 84 | case PS3DE::PIN_JOINT_BIAS: { |
| 85 | return PIN_DEFAULT_BIAS; |
| 86 | } |
| 87 | case PS3DE::PIN_JOINT_DAMPING: { |
| 88 | return PIN_DEFAULT_DAMPING; |
| 89 | } |
| 90 | case PS3DE::PIN_JOINT_IMPULSE_CLAMP: { |
| 91 | return PIN_DEFAULT_IMPULSE_CLAMP; |
| 92 | } |
| 93 | default: { |
| 94 | ERR_FAIL_V_MSG(0.0, vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param)); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void JoltPinJoint3D::set_param(PS3DE::PinJointParam p_param, double p_value) { |
| 100 | switch (p_param) { |
| 101 | case PS3DE::PIN_JOINT_BIAS: { |
| 102 | if (!Math::is_equal_approx(p_value, PIN_DEFAULT_BIAS)) { |
| 103 | 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())); |
| 104 | } |
| 105 | } break; |
| 106 | case PS3DE::PIN_JOINT_DAMPING: { |
| 107 | if (!Math::is_equal_approx(p_value, PIN_DEFAULT_DAMPING)) { |
| 108 | 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())); |
| 109 | } |
| 110 | } break; |
| 111 | case PS3DE::PIN_JOINT_IMPULSE_CLAMP: { |
| 112 | if (!Math::is_equal_approx(p_value, PIN_DEFAULT_IMPULSE_CLAMP)) { |
| 113 | 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())); |
| 114 | } |
| 115 | } break; |
| 116 | default: { |
| 117 | ERR_FAIL_MSG(vformat("Unhandled pin joint parameter: '%d'. This should not happen. Please report this.", p_param)); |
| 118 | } break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | float JoltPinJoint3D::get_applied_force() const { |
| 123 | JPH::PointConstraint *constraint = static_cast<JPH::PointConstraint *>(jolt_ref.GetPtr()); |
| 124 | ERR_FAIL_NULL_V(constraint, 0.0f); |
| 125 | |
| 126 | JoltSpace3D *space = get_space(); |
| 127 | ERR_FAIL_NULL_V(space, 0.0f); |
| 128 | |
| 129 | const float last_step = space->get_last_step(); |
| 130 | if (unlikely(last_step == 0.0f)) { |
| 131 | return 0.0f; |
| 132 | } |
| 133 | |
| 134 | return constraint->GetTotalLambdaPosition().Length() / last_step; |
| 135 | } |
| 136 | |
| 137 | void JoltPinJoint3D::rebuild() { |
| 138 | destroy(); |
| 139 | |
| 140 | JoltSpace3D *space = get_space(); |
| 141 | if (space == nullptr) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | JPH::Body *jolt_body_a = body_a != nullptr ? body_a->get_jolt_body() : nullptr; |
| 146 | JPH::Body *jolt_body_b = body_b != nullptr ? body_b->get_jolt_body() : nullptr; |
| 147 | ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr); |
| 148 | |
| 149 | Transform3D shifted_ref_a; |
| 150 | Transform3D shifted_ref_b; |
| 151 | |
| 152 | _shift_reference_frames(Vector3(), Vector3(), shifted_ref_a, shifted_ref_b); |
| 153 | |
| 154 | jolt_ref = _build_pin(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b); |
| 155 | |
| 156 | space->add_joint(this); |
| 157 | |
| 158 | _update_joint(); |
| 159 | } |