godot_cone_twist_joint_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**************************************************************************/
  2. /* godot_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. /*
  31. Adapted to Godot from the Bullet library.
  32. */
  33. /*
  34. Bullet Continuous Collision Detection and Physics Library
  35. ConeTwistJointSW is Copyright (c) 2007 Starbreeze Studios
  36. This software is provided 'as-is', without any express or implied warranty.
  37. In no event will the authors be held liable for any damages arising from the use of this software.
  38. Permission is granted to anyone to use this software for any purpose,
  39. including commercial applications, and to alter it and redistribute it freely,
  40. subject to the following restrictions:
  41. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  42. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  43. 3. This notice may not be removed or altered from any source distribution.
  44. Written by: Marcus Hennix
  45. */
  46. #include "godot_cone_twist_joint_3d.h"
  47. GodotConeTwistJoint3D::GodotConeTwistJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &rbAFrame, const Transform3D &rbBFrame) :
  48. GodotJoint3D(_arr, 2) {
  49. A = rbA;
  50. B = rbB;
  51. m_rbAFrame = rbAFrame;
  52. m_rbBFrame = rbBFrame;
  53. A->add_constraint(this, 0);
  54. B->add_constraint(this, 1);
  55. }
  56. bool GodotConeTwistJoint3D::setup(real_t p_timestep) {
  57. dynamic_A = (A->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC);
  58. dynamic_B = (B->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC);
  59. if (!dynamic_A && !dynamic_B) {
  60. return false;
  61. }
  62. m_appliedImpulse = real_t(0.);
  63. //set bias, sign, clear accumulator
  64. m_swingCorrection = real_t(0.);
  65. m_twistLimitSign = real_t(0.);
  66. m_solveTwistLimit = false;
  67. m_solveSwingLimit = false;
  68. m_accTwistLimitImpulse = real_t(0.);
  69. m_accSwingLimitImpulse = real_t(0.);
  70. if (!m_angularOnly) {
  71. Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
  72. Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);
  73. Vector3 relPos = pivotBInW - pivotAInW;
  74. Vector3 normal[3];
  75. if (Math::is_zero_approx(relPos.length_squared())) {
  76. normal[0] = Vector3(real_t(1.0), 0, 0);
  77. } else {
  78. normal[0] = relPos.normalized();
  79. }
  80. plane_space(normal[0], normal[1], normal[2]);
  81. for (int i = 0; i < 3; i++) {
  82. memnew_placement(
  83. &m_jac[i],
  84. GodotJacobianEntry3D(
  85. A->get_principal_inertia_axes().transposed(),
  86. B->get_principal_inertia_axes().transposed(),
  87. pivotAInW - A->get_transform().origin - A->get_center_of_mass(),
  88. pivotBInW - B->get_transform().origin - B->get_center_of_mass(),
  89. normal[i],
  90. A->get_inv_inertia(),
  91. A->get_inv_mass(),
  92. B->get_inv_inertia(),
  93. B->get_inv_mass()));
  94. }
  95. }
  96. Vector3 b1Axis1, b1Axis2, b1Axis3;
  97. Vector3 b2Axis1, b2Axis2;
  98. b1Axis1 = A->get_transform().basis.xform(m_rbAFrame.basis.get_column(0));
  99. b2Axis1 = B->get_transform().basis.xform(m_rbBFrame.basis.get_column(0));
  100. real_t swing1 = real_t(0.), swing2 = real_t(0.);
  101. real_t swx = real_t(0.), swy = real_t(0.);
  102. real_t thresh = real_t(10.);
  103. real_t fact;
  104. // Get Frame into world space
  105. if (m_swingSpan1 >= real_t(0.05f)) {
  106. b1Axis2 = A->get_transform().basis.xform(m_rbAFrame.basis.get_column(1));
  107. //swing1 = btAtan2Fast( b2Axis1.dot(b1Axis2),b2Axis1.dot(b1Axis1) );
  108. swx = b2Axis1.dot(b1Axis1);
  109. swy = b2Axis1.dot(b1Axis2);
  110. swing1 = atan2fast(swy, swx);
  111. fact = (swy * swy + swx * swx) * thresh * thresh;
  112. fact = fact / (fact + real_t(1.0));
  113. swing1 *= fact;
  114. }
  115. if (m_swingSpan2 >= real_t(0.05f)) {
  116. b1Axis3 = A->get_transform().basis.xform(m_rbAFrame.basis.get_column(2));
  117. //swing2 = btAtan2Fast( b2Axis1.dot(b1Axis3),b2Axis1.dot(b1Axis1) );
  118. swx = b2Axis1.dot(b1Axis1);
  119. swy = b2Axis1.dot(b1Axis3);
  120. swing2 = atan2fast(swy, swx);
  121. fact = (swy * swy + swx * swx) * thresh * thresh;
  122. fact = fact / (fact + real_t(1.0));
  123. swing2 *= fact;
  124. }
  125. real_t RMaxAngle1Sq = 1.0f / (m_swingSpan1 * m_swingSpan1);
  126. real_t RMaxAngle2Sq = 1.0f / (m_swingSpan2 * m_swingSpan2);
  127. real_t EllipseAngle = Math::abs(swing1 * swing1) * RMaxAngle1Sq + Math::abs(swing2 * swing2) * RMaxAngle2Sq;
  128. if (EllipseAngle > 1.0f) {
  129. m_swingCorrection = EllipseAngle - 1.0f;
  130. m_solveSwingLimit = true;
  131. // Calculate necessary axis & factors
  132. m_swingAxis = b2Axis1.cross(b1Axis2 * b2Axis1.dot(b1Axis2) + b1Axis3 * b2Axis1.dot(b1Axis3));
  133. m_swingAxis.normalize();
  134. real_t swingAxisSign = (b2Axis1.dot(b1Axis1) >= 0.0f) ? 1.0f : -1.0f;
  135. m_swingAxis *= swingAxisSign;
  136. m_kSwing = real_t(1.) / (A->compute_angular_impulse_denominator(m_swingAxis) + B->compute_angular_impulse_denominator(m_swingAxis));
  137. }
  138. // Twist limits
  139. if (m_twistSpan >= real_t(0.)) {
  140. Vector3 b2Axis22 = B->get_transform().basis.xform(m_rbBFrame.basis.get_column(1));
  141. Quaternion rotationArc = Quaternion(b2Axis1, b1Axis1);
  142. Vector3 TwistRef = rotationArc.xform(b2Axis22);
  143. real_t twist = atan2fast(TwistRef.dot(b1Axis3), TwistRef.dot(b1Axis2));
  144. real_t lockedFreeFactor = (m_twistSpan > real_t(0.05f)) ? m_limitSoftness : real_t(0.);
  145. if (twist <= -m_twistSpan * lockedFreeFactor) {
  146. m_twistCorrection = -(twist + m_twistSpan);
  147. m_solveTwistLimit = true;
  148. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  149. m_twistAxis.normalize();
  150. m_twistAxis *= -1.0f;
  151. m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) + B->compute_angular_impulse_denominator(m_twistAxis));
  152. } else if (twist > m_twistSpan * lockedFreeFactor) {
  153. m_twistCorrection = (twist - m_twistSpan);
  154. m_solveTwistLimit = true;
  155. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  156. m_twistAxis.normalize();
  157. m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) + B->compute_angular_impulse_denominator(m_twistAxis));
  158. }
  159. }
  160. return true;
  161. }
  162. void GodotConeTwistJoint3D::solve(real_t p_timestep) {
  163. Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
  164. Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);
  165. real_t tau = real_t(0.3);
  166. //linear part
  167. if (!m_angularOnly) {
  168. Vector3 rel_pos1 = pivotAInW - A->get_transform().origin;
  169. Vector3 rel_pos2 = pivotBInW - B->get_transform().origin;
  170. Vector3 vel1 = A->get_velocity_in_local_point(rel_pos1);
  171. Vector3 vel2 = B->get_velocity_in_local_point(rel_pos2);
  172. Vector3 vel = vel1 - vel2;
  173. for (int i = 0; i < 3; i++) {
  174. const Vector3 &normal = m_jac[i].m_linearJointAxis;
  175. real_t jacDiagABInv = real_t(1.) / m_jac[i].getDiagonal();
  176. real_t rel_vel;
  177. rel_vel = normal.dot(vel);
  178. //positional error (zeroth order error)
  179. real_t depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal
  180. real_t impulse = depth * tau / p_timestep * jacDiagABInv - rel_vel * jacDiagABInv;
  181. m_appliedImpulse += impulse;
  182. Vector3 impulse_vector = normal * impulse;
  183. if (dynamic_A) {
  184. A->apply_impulse(impulse_vector, pivotAInW - A->get_transform().origin);
  185. }
  186. if (dynamic_B) {
  187. B->apply_impulse(-impulse_vector, pivotBInW - B->get_transform().origin);
  188. }
  189. }
  190. }
  191. {
  192. ///solve angular part
  193. const Vector3 &angVelA = A->get_angular_velocity();
  194. const Vector3 &angVelB = B->get_angular_velocity();
  195. // solve swing limit
  196. if (m_solveSwingLimit) {
  197. real_t amplitude = ((angVelB - angVelA).dot(m_swingAxis) * m_relaxationFactor * m_relaxationFactor + m_swingCorrection * (real_t(1.) / p_timestep) * m_biasFactor);
  198. real_t impulseMag = amplitude * m_kSwing;
  199. // Clamp the accumulated impulse
  200. real_t temp = m_accSwingLimitImpulse;
  201. m_accSwingLimitImpulse = MAX(m_accSwingLimitImpulse + impulseMag, real_t(0.0));
  202. impulseMag = m_accSwingLimitImpulse - temp;
  203. Vector3 impulse = m_swingAxis * impulseMag;
  204. if (dynamic_A) {
  205. A->apply_torque_impulse(impulse);
  206. }
  207. if (dynamic_B) {
  208. B->apply_torque_impulse(-impulse);
  209. }
  210. }
  211. // solve twist limit
  212. if (m_solveTwistLimit) {
  213. real_t amplitude = ((angVelB - angVelA).dot(m_twistAxis) * m_relaxationFactor * m_relaxationFactor + m_twistCorrection * (real_t(1.) / p_timestep) * m_biasFactor);
  214. real_t impulseMag = amplitude * m_kTwist;
  215. // Clamp the accumulated impulse
  216. real_t temp = m_accTwistLimitImpulse;
  217. m_accTwistLimitImpulse = MAX(m_accTwistLimitImpulse + impulseMag, real_t(0.0));
  218. impulseMag = m_accTwistLimitImpulse - temp;
  219. Vector3 impulse = m_twistAxis * impulseMag;
  220. if (dynamic_A) {
  221. A->apply_torque_impulse(impulse);
  222. }
  223. if (dynamic_B) {
  224. B->apply_torque_impulse(-impulse);
  225. }
  226. }
  227. }
  228. }
  229. void GodotConeTwistJoint3D::set_param(PhysicsServer3D::ConeTwistJointParam p_param, real_t p_value) {
  230. switch (p_param) {
  231. case PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN: {
  232. m_swingSpan1 = p_value;
  233. m_swingSpan2 = p_value;
  234. } break;
  235. case PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN: {
  236. m_twistSpan = p_value;
  237. } break;
  238. case PhysicsServer3D::CONE_TWIST_JOINT_BIAS: {
  239. m_biasFactor = p_value;
  240. } break;
  241. case PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS: {
  242. m_limitSoftness = p_value;
  243. } break;
  244. case PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION: {
  245. m_relaxationFactor = p_value;
  246. } break;
  247. case PhysicsServer3D::CONE_TWIST_MAX:
  248. break; // Can't happen, but silences warning
  249. }
  250. }
  251. real_t GodotConeTwistJoint3D::get_param(PhysicsServer3D::ConeTwistJointParam p_param) const {
  252. switch (p_param) {
  253. case PhysicsServer3D::CONE_TWIST_JOINT_SWING_SPAN: {
  254. return m_swingSpan1;
  255. } break;
  256. case PhysicsServer3D::CONE_TWIST_JOINT_TWIST_SPAN: {
  257. return m_twistSpan;
  258. } break;
  259. case PhysicsServer3D::CONE_TWIST_JOINT_BIAS: {
  260. return m_biasFactor;
  261. } break;
  262. case PhysicsServer3D::CONE_TWIST_JOINT_SOFTNESS: {
  263. return m_limitSoftness;
  264. } break;
  265. case PhysicsServer3D::CONE_TWIST_JOINT_RELAXATION: {
  266. return m_relaxationFactor;
  267. } break;
  268. case PhysicsServer3D::CONE_TWIST_MAX:
  269. break; // Can't happen, but silences warning
  270. }
  271. return 0;
  272. }