joints_2d_sw.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*************************************************************************/
  2. /* joints_2d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "joints_2d_sw.h"
  31. #include "space_2d_sw.h"
  32. //based on chipmunk joint constraints
  33. /* Copyright (c) 2007 Scott Lembcke
  34. *
  35. * Permission is hereby granted, free of charge, to any person obtaining a copy
  36. * of this software and associated documentation files (the "Software"), to deal
  37. * in the Software without restriction, including without limitation the rights
  38. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  39. * copies of the Software, and to permit persons to whom the Software is
  40. * furnished to do so, subject to the following conditions:
  41. *
  42. * The above copyright notice and this permission notice shall be included in
  43. * all copies or substantial portions of the Software.
  44. *
  45. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  46. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  47. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  48. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  49. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  50. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  51. * SOFTWARE.
  52. */
  53. static inline real_t k_scalar(Body2DSW *a, Body2DSW *b, const Vector2 &rA, const Vector2 &rB, const Vector2 &n) {
  54. real_t value = 0;
  55. {
  56. value += a->get_inv_mass();
  57. real_t rcn = rA.cross(n);
  58. value += a->get_inv_inertia() * rcn * rcn;
  59. }
  60. if (b) {
  61. value += b->get_inv_mass();
  62. real_t rcn = rB.cross(n);
  63. value += b->get_inv_inertia() * rcn * rcn;
  64. }
  65. return value;
  66. }
  67. static inline Vector2
  68. relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB) {
  69. Vector2 sum = a->get_linear_velocity() - rA.tangent() * a->get_angular_velocity();
  70. if (b)
  71. return (b->get_linear_velocity() - rB.tangent() * b->get_angular_velocity()) - sum;
  72. else
  73. return -sum;
  74. }
  75. static inline real_t
  76. normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n) {
  77. return relative_velocity(a, b, rA, rB).dot(n);
  78. }
  79. bool PinJoint2DSW::setup(real_t p_step) {
  80. Space2DSW *space = A->get_space();
  81. ERR_FAIL_COND_V(!space, false);
  82. rA = A->get_transform().basis_xform(anchor_A);
  83. rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B;
  84. real_t B_inv_mass = B ? B->get_inv_mass() : 0.0;
  85. Transform2D K1;
  86. K1[0].x = A->get_inv_mass() + B_inv_mass;
  87. K1[1].x = 0.0f;
  88. K1[0].y = 0.0f;
  89. K1[1].y = A->get_inv_mass() + B_inv_mass;
  90. Transform2D K2;
  91. K2[0].x = A->get_inv_inertia() * rA.y * rA.y;
  92. K2[1].x = -A->get_inv_inertia() * rA.x * rA.y;
  93. K2[0].y = -A->get_inv_inertia() * rA.x * rA.y;
  94. K2[1].y = A->get_inv_inertia() * rA.x * rA.x;
  95. Transform2D K;
  96. K[0] = K1[0] + K2[0];
  97. K[1] = K1[1] + K2[1];
  98. if (B) {
  99. Transform2D K3;
  100. K3[0].x = B->get_inv_inertia() * rB.y * rB.y;
  101. K3[1].x = -B->get_inv_inertia() * rB.x * rB.y;
  102. K3[0].y = -B->get_inv_inertia() * rB.x * rB.y;
  103. K3[1].y = B->get_inv_inertia() * rB.x * rB.x;
  104. K[0] += K3[0];
  105. K[1] += K3[1];
  106. }
  107. K[0].x += softness;
  108. K[1].y += softness;
  109. M = K.affine_inverse();
  110. Vector2 gA = rA + A->get_transform().get_origin();
  111. Vector2 gB = B ? rB + B->get_transform().get_origin() : rB;
  112. Vector2 delta = gB - gA;
  113. bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step);
  114. // apply accumulated impulse
  115. A->apply_impulse(rA, -P);
  116. if (B)
  117. B->apply_impulse(rB, P);
  118. return true;
  119. }
  120. inline Vector2 custom_cross(const Vector2 &p_vec, real_t p_other) {
  121. return Vector2(p_other * p_vec.y, -p_other * p_vec.x);
  122. }
  123. void PinJoint2DSW::solve(real_t p_step) {
  124. // compute relative velocity
  125. Vector2 vA = A->get_linear_velocity() - custom_cross(rA, A->get_angular_velocity());
  126. Vector2 rel_vel;
  127. if (B)
  128. rel_vel = B->get_linear_velocity() - custom_cross(rB, B->get_angular_velocity()) - vA;
  129. else
  130. rel_vel = -vA;
  131. Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P);
  132. A->apply_impulse(rA, -impulse);
  133. if (B)
  134. B->apply_impulse(rB, impulse);
  135. P += impulse;
  136. }
  137. void PinJoint2DSW::set_param(Physics2DServer::PinJointParam p_param, real_t p_value) {
  138. if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
  139. softness = p_value;
  140. }
  141. real_t PinJoint2DSW::get_param(Physics2DServer::PinJointParam p_param) const {
  142. if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS)
  143. return softness;
  144. ERR_FAIL_V(0);
  145. }
  146. PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b) :
  147. Joint2DSW(_arr, p_body_b ? 2 : 1) {
  148. A = p_body_a;
  149. B = p_body_b;
  150. anchor_A = p_body_a->get_inv_transform().xform(p_pos);
  151. anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;
  152. softness = 0;
  153. p_body_a->add_constraint(this, 0);
  154. if (p_body_b)
  155. p_body_b->add_constraint(this, 1);
  156. }
  157. PinJoint2DSW::~PinJoint2DSW() {
  158. if (A)
  159. A->remove_constraint(this);
  160. if (B)
  161. B->remove_constraint(this);
  162. }
  163. //////////////////////////////////////////////
  164. //////////////////////////////////////////////
  165. //////////////////////////////////////////////
  166. static inline void
  167. k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) {
  168. // calculate mass matrix
  169. // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
  170. real_t k11, k12, k21, k22;
  171. real_t m_sum = a->get_inv_mass() + b->get_inv_mass();
  172. // start with I*m_sum
  173. k11 = m_sum;
  174. k12 = 0.0f;
  175. k21 = 0.0f;
  176. k22 = m_sum;
  177. // add the influence from r1
  178. real_t a_i_inv = a->get_inv_inertia();
  179. real_t r1xsq = r1.x * r1.x * a_i_inv;
  180. real_t r1ysq = r1.y * r1.y * a_i_inv;
  181. real_t r1nxy = -r1.x * r1.y * a_i_inv;
  182. k11 += r1ysq;
  183. k12 += r1nxy;
  184. k21 += r1nxy;
  185. k22 += r1xsq;
  186. // add the influnce from r2
  187. real_t b_i_inv = b->get_inv_inertia();
  188. real_t r2xsq = r2.x * r2.x * b_i_inv;
  189. real_t r2ysq = r2.y * r2.y * b_i_inv;
  190. real_t r2nxy = -r2.x * r2.y * b_i_inv;
  191. k11 += r2ysq;
  192. k12 += r2nxy;
  193. k21 += r2nxy;
  194. k22 += r2xsq;
  195. // invert
  196. real_t determinant = k11 * k22 - k12 * k21;
  197. ERR_FAIL_COND(determinant == 0.0);
  198. real_t det_inv = 1.0f / determinant;
  199. *k1 = Vector2(k22 * det_inv, -k12 * det_inv);
  200. *k2 = Vector2(-k21 * det_inv, k11 * det_inv);
  201. }
  202. static _FORCE_INLINE_ Vector2
  203. mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) {
  204. return Vector2(vr.dot(k1), vr.dot(k2));
  205. }
  206. bool GrooveJoint2DSW::setup(real_t p_step) {
  207. // calculate endpoints in worldspace
  208. Vector2 ta = A->get_transform().xform(A_groove_1);
  209. Vector2 tb = A->get_transform().xform(A_groove_2);
  210. Space2DSW *space = A->get_space();
  211. // calculate axis
  212. Vector2 n = -(tb - ta).tangent().normalized();
  213. real_t d = ta.dot(n);
  214. xf_normal = n;
  215. rB = B->get_transform().basis_xform(B_anchor);
  216. // calculate tangential distance along the axis of rB
  217. real_t td = (B->get_transform().get_origin() + rB).cross(n);
  218. // calculate clamping factor and rB
  219. if (td <= ta.cross(n)) {
  220. clamp = 1.0f;
  221. rA = ta - A->get_transform().get_origin();
  222. } else if (td >= tb.cross(n)) {
  223. clamp = -1.0f;
  224. rA = tb - A->get_transform().get_origin();
  225. } else {
  226. clamp = 0.0f;
  227. //joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
  228. rA = ((-n.tangent() * -td) + n * d) - A->get_transform().get_origin();
  229. }
  230. // Calculate mass tensor
  231. k_tensor(A, B, rA, rB, &k1, &k2);
  232. // compute max impulse
  233. jn_max = get_max_force() * p_step;
  234. // calculate bias velocity
  235. //cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
  236. //joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
  237. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  238. real_t _b = get_bias();
  239. gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).clamped(get_max_bias());
  240. // apply accumulated impulse
  241. A->apply_impulse(rA, -jn_acc);
  242. B->apply_impulse(rB, jn_acc);
  243. correct = true;
  244. return true;
  245. }
  246. void GrooveJoint2DSW::solve(real_t p_step) {
  247. // compute impulse
  248. Vector2 vr = relative_velocity(A, B, rA, rB);
  249. Vector2 j = mult_k(gbias - vr, k1, k2);
  250. Vector2 jOld = jn_acc;
  251. j += jOld;
  252. jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).clamped(jn_max);
  253. j = jn_acc - jOld;
  254. A->apply_impulse(rA, -j);
  255. B->apply_impulse(rB, j);
  256. }
  257. GrooveJoint2DSW::GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b) :
  258. Joint2DSW(_arr, 2) {
  259. A = p_body_a;
  260. B = p_body_b;
  261. A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
  262. A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
  263. B_anchor = B->get_inv_transform().xform(p_b_anchor);
  264. A_groove_normal = -(A_groove_2 - A_groove_1).normalized().tangent();
  265. A->add_constraint(this, 0);
  266. B->add_constraint(this, 1);
  267. }
  268. GrooveJoint2DSW::~GrooveJoint2DSW() {
  269. A->remove_constraint(this);
  270. B->remove_constraint(this);
  271. }
  272. //////////////////////////////////////////////
  273. //////////////////////////////////////////////
  274. //////////////////////////////////////////////
  275. bool DampedSpringJoint2DSW::setup(real_t p_step) {
  276. rA = A->get_transform().basis_xform(anchor_A);
  277. rB = B->get_transform().basis_xform(anchor_B);
  278. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  279. real_t dist = delta.length();
  280. if (dist)
  281. n = delta / dist;
  282. else
  283. n = Vector2();
  284. real_t k = k_scalar(A, B, rA, rB, n);
  285. n_mass = 1.0f / k;
  286. target_vrn = 0.0f;
  287. v_coef = 1.0f - Math::exp(-damping * (p_step)*k);
  288. // apply spring force
  289. real_t f_spring = (rest_length - dist) * stiffness;
  290. Vector2 j = n * f_spring * (p_step);
  291. A->apply_impulse(rA, -j);
  292. B->apply_impulse(rB, j);
  293. return true;
  294. }
  295. void DampedSpringJoint2DSW::solve(real_t p_step) {
  296. // compute relative velocity
  297. real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn;
  298. // compute velocity loss from drag
  299. // not 100% certain this is derived correctly, though it makes sense
  300. real_t v_damp = -vrn * v_coef;
  301. target_vrn = vrn + v_damp;
  302. Vector2 j = n * v_damp * n_mass;
  303. A->apply_impulse(rA, -j);
  304. B->apply_impulse(rB, j);
  305. }
  306. void DampedSpringJoint2DSW::set_param(Physics2DServer::DampedStringParam p_param, real_t p_value) {
  307. switch (p_param) {
  308. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  309. rest_length = p_value;
  310. } break;
  311. case Physics2DServer::DAMPED_STRING_DAMPING: {
  312. damping = p_value;
  313. } break;
  314. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  315. stiffness = p_value;
  316. } break;
  317. }
  318. }
  319. real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const {
  320. switch (p_param) {
  321. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  322. return rest_length;
  323. } break;
  324. case Physics2DServer::DAMPED_STRING_DAMPING: {
  325. return damping;
  326. } break;
  327. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  328. return stiffness;
  329. } break;
  330. }
  331. ERR_FAIL_V(0);
  332. }
  333. DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b) :
  334. Joint2DSW(_arr, 2) {
  335. A = p_body_a;
  336. B = p_body_b;
  337. anchor_A = A->get_inv_transform().xform(p_anchor_a);
  338. anchor_B = B->get_inv_transform().xform(p_anchor_b);
  339. rest_length = p_anchor_a.distance_to(p_anchor_b);
  340. stiffness = 20;
  341. damping = 1.5;
  342. A->add_constraint(this, 0);
  343. B->add_constraint(this, 1);
  344. }
  345. DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
  346. A->remove_constraint(this);
  347. B->remove_constraint(this);
  348. }