jolt_cone_twist_joint_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************/
  2. /* jolt_cone_twist_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_cone_twist_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/SwingTwistConstraint.h"
  35. namespace {
  36. constexpr double DEFAULT_BIAS = 0.3;
  37. constexpr double DEFAULT_SOFTNESS = 0.8;
  38. constexpr double DEFAULT_RELAXATION = 1.0;
  39. } // namespace
  40. JPH::Constraint *JoltConeTwistJoint3D::_build_swing_twist(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_swing_limit_span, float p_twist_limit_span) const {
  41. JPH::SwingTwistConstraintSettings constraint_settings;
  42. const bool twist_span_valid = p_twist_limit_span >= 0 && p_twist_limit_span <= JPH::JPH_PI;
  43. const bool swing_span_valid = p_swing_limit_span >= 0 && p_swing_limit_span <= JPH::JPH_PI;
  44. if (twist_limit_enabled && twist_span_valid) {
  45. constraint_settings.mTwistMinAngle = -p_twist_limit_span;
  46. constraint_settings.mTwistMaxAngle = p_twist_limit_span;
  47. } else {
  48. constraint_settings.mTwistMinAngle = -JPH::JPH_PI;
  49. constraint_settings.mTwistMaxAngle = JPH::JPH_PI;
  50. }
  51. if (swing_limit_enabled && swing_span_valid) {
  52. constraint_settings.mNormalHalfConeAngle = p_swing_limit_span;
  53. constraint_settings.mPlaneHalfConeAngle = p_swing_limit_span;
  54. } else {
  55. constraint_settings.mNormalHalfConeAngle = JPH::JPH_PI;
  56. constraint_settings.mPlaneHalfConeAngle = JPH::JPH_PI;
  57. if (!swing_span_valid) {
  58. constraint_settings.mTwistMinAngle = -JPH::JPH_PI;
  59. constraint_settings.mTwistMaxAngle = JPH::JPH_PI;
  60. }
  61. }
  62. constraint_settings.mSpace = JPH::EConstraintSpace::LocalToBodyCOM;
  63. constraint_settings.mPosition1 = to_jolt_r(p_shifted_ref_a.origin);
  64. constraint_settings.mTwistAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_X));
  65. constraint_settings.mPlaneAxis1 = to_jolt(p_shifted_ref_a.basis.get_column(Vector3::AXIS_Z));
  66. constraint_settings.mPosition2 = to_jolt_r(p_shifted_ref_b.origin);
  67. constraint_settings.mTwistAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_X));
  68. constraint_settings.mPlaneAxis2 = to_jolt(p_shifted_ref_b.basis.get_column(Vector3::AXIS_Z));
  69. constraint_settings.mSwingType = JPH::ESwingType::Pyramid;
  70. if (p_jolt_body_a == nullptr) {
  71. return constraint_settings.Create(JPH::Body::sFixedToWorld, *p_jolt_body_b);
  72. } else if (p_jolt_body_b == nullptr) {
  73. return constraint_settings.Create(*p_jolt_body_a, JPH::Body::sFixedToWorld);
  74. } else {
  75. return constraint_settings.Create(*p_jolt_body_a, *p_jolt_body_b);
  76. }
  77. }
  78. void JoltConeTwistJoint3D::_update_swing_motor_state() {
  79. if (JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr())) {
  80. constraint->SetSwingMotorState(swing_motor_enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
  81. }
  82. }
  83. void JoltConeTwistJoint3D::_update_twist_motor_state() {
  84. if (JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr())) {
  85. constraint->SetTwistMotorState(twist_motor_enabled ? JPH::EMotorState::Velocity : JPH::EMotorState::Off);
  86. }
  87. }
  88. void JoltConeTwistJoint3D::_update_motor_velocity() {
  89. if (JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr())) {
  90. // We flip the direction since Jolt is CCW but Godot is CW.
  91. constraint->SetTargetAngularVelocityCS({ (float)-twist_motor_target_speed, (float)-swing_motor_target_speed_y, (float)-swing_motor_target_speed_z });
  92. }
  93. }
  94. void JoltConeTwistJoint3D::_update_swing_motor_limit() {
  95. if (JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr())) {
  96. JPH::MotorSettings &motor_settings = constraint->GetSwingMotorSettings();
  97. motor_settings.mMinTorqueLimit = (float)-swing_motor_max_torque;
  98. motor_settings.mMaxTorqueLimit = (float)swing_motor_max_torque;
  99. }
  100. }
  101. void JoltConeTwistJoint3D::_update_twist_motor_limit() {
  102. if (JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr())) {
  103. JPH::MotorSettings &motor_settings = constraint->GetTwistMotorSettings();
  104. motor_settings.mMinTorqueLimit = (float)-twist_motor_max_torque;
  105. motor_settings.mMaxTorqueLimit = (float)twist_motor_max_torque;
  106. }
  107. }
  108. void JoltConeTwistJoint3D::_limits_changed() {
  109. rebuild();
  110. _wake_up_bodies();
  111. }
  112. void JoltConeTwistJoint3D::_swing_motor_state_changed() {
  113. _update_swing_motor_state();
  114. _wake_up_bodies();
  115. }
  116. void JoltConeTwistJoint3D::_twist_motor_state_changed() {
  117. _update_twist_motor_state();
  118. _wake_up_bodies();
  119. }
  120. void JoltConeTwistJoint3D::_motor_velocity_changed() {
  121. _update_motor_velocity();
  122. _wake_up_bodies();
  123. }
  124. void JoltConeTwistJoint3D::_swing_motor_limit_changed() {
  125. _update_swing_motor_limit();
  126. _wake_up_bodies();
  127. }
  128. void JoltConeTwistJoint3D::_twist_motor_limit_changed() {
  129. _update_twist_motor_limit();
  130. _wake_up_bodies();
  131. }
  132. JoltConeTwistJoint3D::JoltConeTwistJoint3D(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) :
  133. JoltJoint3D(p_old_joint, p_body_a, p_body_b, p_local_ref_a, p_local_ref_b) {
  134. rebuild();
  135. }
  136. double JoltConeTwistJoint3D::get_param(PhysicsServer3D::ConeTwistJointParam p_param) const {
  137. switch (p_param) {
  138. case PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN: {
  139. return swing_limit_span;
  140. }
  141. case PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN: {
  142. return twist_limit_span;
  143. }
  144. case PhysicsServer3D::CONE_TWIST_JOINT_BIAS: {
  145. return DEFAULT_BIAS;
  146. }
  147. case PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS: {
  148. return DEFAULT_SOFTNESS;
  149. }
  150. case PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION: {
  151. return DEFAULT_RELAXATION;
  152. }
  153. default: {
  154. ERR_FAIL_V_MSG(0.0, vformat("Unhandled cone twist joint parameter: '%d'. This should not happen. Please report this.", p_param));
  155. }
  156. }
  157. }
  158. void JoltConeTwistJoint3D::set_param(PhysicsServer3D::ConeTwistJointParam p_param, double p_value) {
  159. switch (p_param) {
  160. case PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN: {
  161. swing_limit_span = p_value;
  162. _limits_changed();
  163. } break;
  164. case PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN: {
  165. twist_limit_span = p_value;
  166. _limits_changed();
  167. } break;
  168. case PhysicsServer3D::CONE_TWIST_JOINT_BIAS: {
  169. if (!Math::is_equal_approx(p_value, DEFAULT_BIAS)) {
  170. WARN_PRINT(vformat("Cone twist joint bias is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  171. }
  172. } break;
  173. case PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS: {
  174. if (!Math::is_equal_approx(p_value, DEFAULT_SOFTNESS)) {
  175. WARN_PRINT(vformat("Cone twist joint softness is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  176. }
  177. } break;
  178. case PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION: {
  179. if (!Math::is_equal_approx(p_value, DEFAULT_RELAXATION)) {
  180. WARN_PRINT(vformat("Cone twist joint relaxation is not supported when using Jolt Physics. Any such value will be ignored. This joint connects %s.", _bodies_to_string()));
  181. }
  182. } break;
  183. default: {
  184. ERR_FAIL_MSG(vformat("Unhandled cone twist joint parameter: '%d'. This should not happen. Please report this.", p_param));
  185. } break;
  186. }
  187. }
  188. double JoltConeTwistJoint3D::get_jolt_param(JoltParameter p_param) const {
  189. switch (p_param) {
  190. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_TARGET_VELOCITY_Y: {
  191. return swing_motor_target_speed_y;
  192. }
  193. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_TARGET_VELOCITY_Z: {
  194. return swing_motor_target_speed_z;
  195. }
  196. case JoltPhysicsServer3D::CONE_TWIST_JOINT_TWIST_MOTOR_TARGET_VELOCITY: {
  197. return twist_motor_target_speed;
  198. }
  199. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_MAX_TORQUE: {
  200. return swing_motor_max_torque;
  201. }
  202. case JoltPhysicsServer3D::CONE_TWIST_JOINT_TWIST_MOTOR_MAX_TORQUE: {
  203. return twist_motor_max_torque;
  204. }
  205. default: {
  206. ERR_FAIL_V_MSG(0.0, vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
  207. }
  208. }
  209. }
  210. void JoltConeTwistJoint3D::set_jolt_param(JoltParameter p_param, double p_value) {
  211. switch (p_param) {
  212. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_TARGET_VELOCITY_Y: {
  213. swing_motor_target_speed_y = p_value;
  214. _motor_velocity_changed();
  215. } break;
  216. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_TARGET_VELOCITY_Z: {
  217. swing_motor_target_speed_z = p_value;
  218. _motor_velocity_changed();
  219. } break;
  220. case JoltPhysicsServer3D::CONE_TWIST_JOINT_TWIST_MOTOR_TARGET_VELOCITY: {
  221. twist_motor_target_speed = p_value;
  222. _motor_velocity_changed();
  223. } break;
  224. case JoltPhysicsServer3D::CONE_TWIST_JOINT_SWING_MOTOR_MAX_TORQUE: {
  225. swing_motor_max_torque = p_value;
  226. _swing_motor_limit_changed();
  227. } break;
  228. case JoltPhysicsServer3D::CONE_TWIST_JOINT_TWIST_MOTOR_MAX_TORQUE: {
  229. twist_motor_max_torque = p_value;
  230. _twist_motor_limit_changed();
  231. } break;
  232. default: {
  233. ERR_FAIL_MSG(vformat("Unhandled parameter: '%d'. This should not happen. Please report this.", p_param));
  234. } break;
  235. }
  236. }
  237. bool JoltConeTwistJoint3D::get_jolt_flag(JoltFlag p_flag) const {
  238. switch (p_flag) {
  239. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_USE_SWING_LIMIT: {
  240. return swing_limit_enabled;
  241. }
  242. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_USE_TWIST_LIMIT: {
  243. return twist_limit_enabled;
  244. }
  245. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_ENABLE_SWING_MOTOR: {
  246. return swing_motor_enabled;
  247. }
  248. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_ENABLE_TWIST_MOTOR: {
  249. return twist_motor_enabled;
  250. }
  251. default: {
  252. ERR_FAIL_V_MSG(false, vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
  253. }
  254. }
  255. }
  256. void JoltConeTwistJoint3D::set_jolt_flag(JoltFlag p_flag, bool p_enabled) {
  257. switch (p_flag) {
  258. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_USE_SWING_LIMIT: {
  259. swing_limit_enabled = p_enabled;
  260. _limits_changed();
  261. } break;
  262. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_USE_TWIST_LIMIT: {
  263. twist_limit_enabled = p_enabled;
  264. _limits_changed();
  265. } break;
  266. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_ENABLE_SWING_MOTOR: {
  267. swing_motor_enabled = p_enabled;
  268. _swing_motor_state_changed();
  269. } break;
  270. case JoltPhysicsServer3D::CONE_TWIST_JOINT_FLAG_ENABLE_TWIST_MOTOR: {
  271. twist_motor_enabled = p_enabled;
  272. _twist_motor_state_changed();
  273. } break;
  274. default: {
  275. ERR_FAIL_MSG(vformat("Unhandled flag: '%d'. This should not happen. Please report this.", p_flag));
  276. } break;
  277. }
  278. }
  279. float JoltConeTwistJoint3D::get_applied_force() const {
  280. JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr());
  281. ERR_FAIL_NULL_V(constraint, 0.0f);
  282. JoltSpace3D *space = get_space();
  283. ERR_FAIL_NULL_V(space, 0.0f);
  284. const float last_step = space->get_last_step();
  285. if (unlikely(last_step == 0.0f)) {
  286. return 0.0f;
  287. }
  288. return constraint->GetTotalLambdaPosition().Length() / last_step;
  289. }
  290. float JoltConeTwistJoint3D::get_applied_torque() const {
  291. JPH::SwingTwistConstraint *constraint = static_cast<JPH::SwingTwistConstraint *>(jolt_ref.GetPtr());
  292. ERR_FAIL_NULL_V(constraint, 0.0f);
  293. JoltSpace3D *space = get_space();
  294. ERR_FAIL_NULL_V(space, 0.0f);
  295. const float last_step = space->get_last_step();
  296. if (unlikely(last_step == 0.0f)) {
  297. return 0.0f;
  298. }
  299. const JPH::Vec3 swing_twist_lambda = JPH::Vec3(constraint->GetTotalLambdaTwist(), constraint->GetTotalLambdaSwingY(), constraint->GetTotalLambdaSwingZ());
  300. // Note that the motor lambda is in a different space than the swing twist lambda, and since the two forces can cancel each other it is
  301. // technically incorrect to just add them. The bodies themselves have moved, so we can't transform one into the space of the other anymore.
  302. const float total_lambda = swing_twist_lambda.Length() + constraint->GetTotalLambdaMotor().Length();
  303. return total_lambda / last_step;
  304. }
  305. void JoltConeTwistJoint3D::rebuild() {
  306. destroy();
  307. JoltSpace3D *space = get_space();
  308. if (space == nullptr) {
  309. return;
  310. }
  311. const JPH::BodyID body_ids[2] = {
  312. body_a != nullptr ? body_a->get_jolt_id() : JPH::BodyID(),
  313. body_b != nullptr ? body_b->get_jolt_id() : JPH::BodyID()
  314. };
  315. const JoltWritableBodies3D jolt_bodies = space->write_bodies(body_ids, 2);
  316. JPH::Body *jolt_body_a = static_cast<JPH::Body *>(jolt_bodies[0]);
  317. JPH::Body *jolt_body_b = static_cast<JPH::Body *>(jolt_bodies[1]);
  318. ERR_FAIL_COND(jolt_body_a == nullptr && jolt_body_b == nullptr);
  319. Transform3D shifted_ref_a;
  320. Transform3D shifted_ref_b;
  321. _shift_reference_frames(Vector3(), Vector3(), shifted_ref_a, shifted_ref_b);
  322. jolt_ref = _build_swing_twist(jolt_body_a, jolt_body_b, shifted_ref_a, shifted_ref_b, (float)swing_limit_span, (float)twist_limit_span);
  323. space->add_joint(this);
  324. _update_enabled();
  325. _update_iterations();
  326. _update_swing_motor_state();
  327. _update_twist_motor_state();
  328. _update_motor_velocity();
  329. _update_swing_motor_limit();
  330. _update_twist_motor_limit();
  331. }