cone_twist_joint_sw.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*************************************************************************/
  2. /* cone_twist_joint_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "cone_twist_joint_sw.h"
  47. static void plane_space(const Vector3 &n, Vector3 &p, Vector3 &q) {
  48. if (Math::abs(n.z) > 0.707106781186547524400844362) {
  49. // choose p in y-z plane
  50. real_t a = n[1] * n[1] + n[2] * n[2];
  51. real_t k = 1.0 / Math::sqrt(a);
  52. p = Vector3(0, -n[2] * k, n[1] * k);
  53. // set q = n x p
  54. q = Vector3(a * k, -n[0] * p[2], n[0] * p[1]);
  55. } else {
  56. // choose p in x-y plane
  57. real_t a = n.x * n.x + n.y * n.y;
  58. real_t k = 1.0 / Math::sqrt(a);
  59. p = Vector3(-n.y * k, n.x * k, 0);
  60. // set q = n x p
  61. q = Vector3(-n.z * p.y, n.z * p.x, a * k);
  62. }
  63. }
  64. static _FORCE_INLINE_ real_t atan2fast(real_t y, real_t x) {
  65. real_t coeff_1 = Math_PI / 4.0f;
  66. real_t coeff_2 = 3.0f * coeff_1;
  67. real_t abs_y = Math::abs(y);
  68. real_t angle;
  69. if (x >= 0.0f) {
  70. real_t r = (x - abs_y) / (x + abs_y);
  71. angle = coeff_1 - coeff_1 * r;
  72. } else {
  73. real_t r = (x + abs_y) / (abs_y - x);
  74. angle = coeff_2 - coeff_1 * r;
  75. }
  76. return (y < 0.0f) ? -angle : angle;
  77. }
  78. ConeTwistJointSW::ConeTwistJointSW(BodySW *rbA, BodySW *rbB, const Transform &rbAFrame, const Transform &rbBFrame) :
  79. JointSW(_arr, 2) {
  80. A = rbA;
  81. B = rbB;
  82. m_rbAFrame = rbAFrame;
  83. m_rbBFrame = rbBFrame;
  84. m_swingSpan1 = Math_PI / 4.0;
  85. m_swingSpan2 = Math_PI / 4.0;
  86. m_twistSpan = Math_PI * 2;
  87. m_biasFactor = 0.3f;
  88. m_relaxationFactor = 1.0f;
  89. m_angularOnly = false;
  90. m_solveTwistLimit = false;
  91. m_solveSwingLimit = false;
  92. A->add_constraint(this, 0);
  93. B->add_constraint(this, 1);
  94. m_appliedImpulse = 0;
  95. }
  96. bool ConeTwistJointSW::setup(real_t p_timestep) {
  97. m_appliedImpulse = real_t(0.);
  98. //set bias, sign, clear accumulator
  99. m_swingCorrection = real_t(0.);
  100. m_twistLimitSign = real_t(0.);
  101. m_solveTwistLimit = false;
  102. m_solveSwingLimit = false;
  103. m_accTwistLimitImpulse = real_t(0.);
  104. m_accSwingLimitImpulse = real_t(0.);
  105. if (!m_angularOnly) {
  106. Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
  107. Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);
  108. Vector3 relPos = pivotBInW - pivotAInW;
  109. Vector3 normal[3];
  110. if (relPos.length_squared() > CMP_EPSILON) {
  111. normal[0] = relPos.normalized();
  112. } else {
  113. normal[0] = Vector3(real_t(1.0), 0, 0);
  114. }
  115. plane_space(normal[0], normal[1], normal[2]);
  116. for (int i = 0; i < 3; i++) {
  117. memnew_placement(&m_jac[i], JacobianEntrySW(
  118. A->get_principal_inertia_axes().transposed(),
  119. B->get_principal_inertia_axes().transposed(),
  120. pivotAInW - A->get_transform().origin - A->get_center_of_mass(),
  121. pivotBInW - B->get_transform().origin - B->get_center_of_mass(),
  122. normal[i],
  123. A->get_inv_inertia(),
  124. A->get_inv_mass(),
  125. B->get_inv_inertia(),
  126. B->get_inv_mass()));
  127. }
  128. }
  129. Vector3 b1Axis1, b1Axis2, b1Axis3;
  130. Vector3 b2Axis1, b2Axis2;
  131. b1Axis1 = A->get_transform().basis.xform(this->m_rbAFrame.basis.get_axis(0));
  132. b2Axis1 = B->get_transform().basis.xform(this->m_rbBFrame.basis.get_axis(0));
  133. real_t swing1 = real_t(0.), swing2 = real_t(0.);
  134. real_t swx = real_t(0.), swy = real_t(0.);
  135. real_t thresh = real_t(10.);
  136. real_t fact;
  137. // Get Frame into world space
  138. if (m_swingSpan1 >= real_t(0.05f)) {
  139. b1Axis2 = A->get_transform().basis.xform(this->m_rbAFrame.basis.get_axis(1));
  140. //swing1 = btAtan2Fast( b2Axis1.dot(b1Axis2),b2Axis1.dot(b1Axis1) );
  141. swx = b2Axis1.dot(b1Axis1);
  142. swy = b2Axis1.dot(b1Axis2);
  143. swing1 = atan2fast(swy, swx);
  144. fact = (swy * swy + swx * swx) * thresh * thresh;
  145. fact = fact / (fact + real_t(1.0));
  146. swing1 *= fact;
  147. }
  148. if (m_swingSpan2 >= real_t(0.05f)) {
  149. b1Axis3 = A->get_transform().basis.xform(this->m_rbAFrame.basis.get_axis(2));
  150. //swing2 = btAtan2Fast( b2Axis1.dot(b1Axis3),b2Axis1.dot(b1Axis1) );
  151. swx = b2Axis1.dot(b1Axis1);
  152. swy = b2Axis1.dot(b1Axis3);
  153. swing2 = atan2fast(swy, swx);
  154. fact = (swy * swy + swx * swx) * thresh * thresh;
  155. fact = fact / (fact + real_t(1.0));
  156. swing2 *= fact;
  157. }
  158. real_t RMaxAngle1Sq = 1.0f / (m_swingSpan1 * m_swingSpan1);
  159. real_t RMaxAngle2Sq = 1.0f / (m_swingSpan2 * m_swingSpan2);
  160. real_t EllipseAngle = Math::abs(swing1 * swing1) * RMaxAngle1Sq + Math::abs(swing2 * swing2) * RMaxAngle2Sq;
  161. if (EllipseAngle > 1.0f) {
  162. m_swingCorrection = EllipseAngle - 1.0f;
  163. m_solveSwingLimit = true;
  164. // Calculate necessary axis & factors
  165. m_swingAxis = b2Axis1.cross(b1Axis2 * b2Axis1.dot(b1Axis2) + b1Axis3 * b2Axis1.dot(b1Axis3));
  166. m_swingAxis.normalize();
  167. real_t swingAxisSign = (b2Axis1.dot(b1Axis1) >= 0.0f) ? 1.0f : -1.0f;
  168. m_swingAxis *= swingAxisSign;
  169. m_kSwing = real_t(1.) / (A->compute_angular_impulse_denominator(m_swingAxis) +
  170. B->compute_angular_impulse_denominator(m_swingAxis));
  171. }
  172. // Twist limits
  173. if (m_twistSpan >= real_t(0.)) {
  174. Vector3 b2Axis2 = B->get_transform().basis.xform(this->m_rbBFrame.basis.get_axis(1));
  175. Quat rotationArc = Quat(b2Axis1, b1Axis1);
  176. Vector3 TwistRef = rotationArc.xform(b2Axis2);
  177. real_t twist = atan2fast(TwistRef.dot(b1Axis3), TwistRef.dot(b1Axis2));
  178. real_t lockedFreeFactor = (m_twistSpan > real_t(0.05f)) ? m_limitSoftness : real_t(0.);
  179. if (twist <= -m_twistSpan * lockedFreeFactor) {
  180. m_twistCorrection = -(twist + m_twistSpan);
  181. m_solveTwistLimit = true;
  182. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  183. m_twistAxis.normalize();
  184. m_twistAxis *= -1.0f;
  185. m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) +
  186. B->compute_angular_impulse_denominator(m_twistAxis));
  187. } else if (twist > m_twistSpan * lockedFreeFactor) {
  188. m_twistCorrection = (twist - m_twistSpan);
  189. m_solveTwistLimit = true;
  190. m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
  191. m_twistAxis.normalize();
  192. m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) +
  193. B->compute_angular_impulse_denominator(m_twistAxis));
  194. }
  195. }
  196. return true;
  197. }
  198. void ConeTwistJointSW::solve(real_t p_timestep) {
  199. Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
  200. Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);
  201. real_t tau = real_t(0.3);
  202. //linear part
  203. if (!m_angularOnly) {
  204. Vector3 rel_pos1 = pivotAInW - A->get_transform().origin;
  205. Vector3 rel_pos2 = pivotBInW - B->get_transform().origin;
  206. Vector3 vel1 = A->get_velocity_in_local_point(rel_pos1);
  207. Vector3 vel2 = B->get_velocity_in_local_point(rel_pos2);
  208. Vector3 vel = vel1 - vel2;
  209. for (int i = 0; i < 3; i++) {
  210. const Vector3 &normal = m_jac[i].m_linearJointAxis;
  211. real_t jacDiagABInv = real_t(1.) / m_jac[i].getDiagonal();
  212. real_t rel_vel;
  213. rel_vel = normal.dot(vel);
  214. //positional error (zeroth order error)
  215. real_t depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal
  216. real_t impulse = depth * tau / p_timestep * jacDiagABInv - rel_vel * jacDiagABInv;
  217. m_appliedImpulse += impulse;
  218. Vector3 impulse_vector = normal * impulse;
  219. A->apply_impulse(pivotAInW - A->get_transform().origin, impulse_vector);
  220. B->apply_impulse(pivotBInW - B->get_transform().origin, -impulse_vector);
  221. }
  222. }
  223. {
  224. ///solve angular part
  225. const Vector3 &angVelA = A->get_angular_velocity();
  226. const Vector3 &angVelB = B->get_angular_velocity();
  227. // solve swing limit
  228. if (m_solveSwingLimit) {
  229. real_t amplitude = ((angVelB - angVelA).dot(m_swingAxis) * m_relaxationFactor * m_relaxationFactor + m_swingCorrection * (real_t(1.) / p_timestep) * m_biasFactor);
  230. real_t impulseMag = amplitude * m_kSwing;
  231. // Clamp the accumulated impulse
  232. real_t temp = m_accSwingLimitImpulse;
  233. m_accSwingLimitImpulse = MAX(m_accSwingLimitImpulse + impulseMag, real_t(0.0));
  234. impulseMag = m_accSwingLimitImpulse - temp;
  235. Vector3 impulse = m_swingAxis * impulseMag;
  236. A->apply_torque_impulse(impulse);
  237. B->apply_torque_impulse(-impulse);
  238. }
  239. // solve twist limit
  240. if (m_solveTwistLimit) {
  241. real_t amplitude = ((angVelB - angVelA).dot(m_twistAxis) * m_relaxationFactor * m_relaxationFactor + m_twistCorrection * (real_t(1.) / p_timestep) * m_biasFactor);
  242. real_t impulseMag = amplitude * m_kTwist;
  243. // Clamp the accumulated impulse
  244. real_t temp = m_accTwistLimitImpulse;
  245. m_accTwistLimitImpulse = MAX(m_accTwistLimitImpulse + impulseMag, real_t(0.0));
  246. impulseMag = m_accTwistLimitImpulse - temp;
  247. Vector3 impulse = m_twistAxis * impulseMag;
  248. A->apply_torque_impulse(impulse);
  249. B->apply_torque_impulse(-impulse);
  250. }
  251. }
  252. }
  253. void ConeTwistJointSW::set_param(PhysicsServer::ConeTwistJointParam p_param, real_t p_value) {
  254. switch (p_param) {
  255. case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN: {
  256. m_swingSpan1 = p_value;
  257. m_swingSpan2 = p_value;
  258. } break;
  259. case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN: {
  260. m_twistSpan = p_value;
  261. } break;
  262. case PhysicsServer::CONE_TWIST_JOINT_BIAS: {
  263. m_biasFactor = p_value;
  264. } break;
  265. case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS: {
  266. m_limitSoftness = p_value;
  267. } break;
  268. case PhysicsServer::CONE_TWIST_JOINT_RELAXATION: {
  269. m_relaxationFactor = p_value;
  270. } break;
  271. case PhysicsServer::CONE_TWIST_MAX: break; // Can't happen, but silences warning
  272. }
  273. }
  274. real_t ConeTwistJointSW::get_param(PhysicsServer::ConeTwistJointParam p_param) const {
  275. switch (p_param) {
  276. case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN: {
  277. return m_swingSpan1;
  278. } break;
  279. case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN: {
  280. return m_twistSpan;
  281. } break;
  282. case PhysicsServer::CONE_TWIST_JOINT_BIAS: {
  283. return m_biasFactor;
  284. } break;
  285. case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS: {
  286. return m_limitSoftness;
  287. } break;
  288. case PhysicsServer::CONE_TWIST_JOINT_RELAXATION: {
  289. return m_relaxationFactor;
  290. } break;
  291. case PhysicsServer::CONE_TWIST_MAX: break; // Can't happen, but silences warning
  292. }
  293. return 0;
  294. }