joints_2d_sw.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**************************************************************************/
  2. /* joints_2d_sw.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 "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. }
  76. static inline real_t
  77. normal_relative_velocity(Body2DSW *a, Body2DSW *b, Vector2 rA, Vector2 rB, Vector2 n) {
  78. return relative_velocity(a, b, rA, rB).dot(n);
  79. }
  80. bool PinJoint2DSW::setup(real_t p_step) {
  81. if ((A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC) && (B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC)) {
  82. return false;
  83. }
  84. Space2DSW *space = A->get_space();
  85. ERR_FAIL_COND_V(!space, false);
  86. rA = A->get_transform().basis_xform(anchor_A);
  87. rB = B ? B->get_transform().basis_xform(anchor_B) : anchor_B;
  88. real_t B_inv_mass = B ? B->get_inv_mass() : 0.0;
  89. Transform2D K1;
  90. K1[0].x = A->get_inv_mass() + B_inv_mass;
  91. K1[1].x = 0.0f;
  92. K1[0].y = 0.0f;
  93. K1[1].y = A->get_inv_mass() + B_inv_mass;
  94. Transform2D K2;
  95. K2[0].x = A->get_inv_inertia() * rA.y * rA.y;
  96. K2[1].x = -A->get_inv_inertia() * rA.x * rA.y;
  97. K2[0].y = -A->get_inv_inertia() * rA.x * rA.y;
  98. K2[1].y = A->get_inv_inertia() * rA.x * rA.x;
  99. Transform2D K;
  100. K[0] = K1[0] + K2[0];
  101. K[1] = K1[1] + K2[1];
  102. if (B) {
  103. Transform2D K3;
  104. K3[0].x = B->get_inv_inertia() * rB.y * rB.y;
  105. K3[1].x = -B->get_inv_inertia() * rB.x * rB.y;
  106. K3[0].y = -B->get_inv_inertia() * rB.x * rB.y;
  107. K3[1].y = B->get_inv_inertia() * rB.x * rB.x;
  108. K[0] += K3[0];
  109. K[1] += K3[1];
  110. }
  111. K[0].x += softness;
  112. K[1].y += softness;
  113. M = K.affine_inverse();
  114. Vector2 gA = rA + A->get_transform().get_origin();
  115. Vector2 gB = B ? rB + B->get_transform().get_origin() : rB;
  116. Vector2 delta = gB - gA;
  117. bias = delta * -(get_bias() == 0 ? space->get_constraint_bias() : get_bias()) * (1.0 / p_step);
  118. // apply accumulated impulse
  119. A->apply_impulse(rA, -P);
  120. if (B) {
  121. B->apply_impulse(rB, P);
  122. }
  123. return true;
  124. }
  125. inline Vector2 custom_cross(const Vector2 &p_vec, real_t p_other) {
  126. return Vector2(p_other * p_vec.y, -p_other * p_vec.x);
  127. }
  128. void PinJoint2DSW::solve(real_t p_step) {
  129. // compute relative velocity
  130. Vector2 vA = A->get_linear_velocity() - custom_cross(rA, A->get_angular_velocity());
  131. Vector2 rel_vel;
  132. if (B) {
  133. rel_vel = B->get_linear_velocity() - custom_cross(rB, B->get_angular_velocity()) - vA;
  134. } else {
  135. rel_vel = -vA;
  136. }
  137. Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P);
  138. A->apply_impulse(rA, -impulse);
  139. if (B) {
  140. B->apply_impulse(rB, impulse);
  141. }
  142. P += impulse;
  143. }
  144. void PinJoint2DSW::set_param(Physics2DServer::PinJointParam p_param, real_t p_value) {
  145. if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS) {
  146. softness = p_value;
  147. }
  148. }
  149. real_t PinJoint2DSW::get_param(Physics2DServer::PinJointParam p_param) const {
  150. if (p_param == Physics2DServer::PIN_JOINT_SOFTNESS) {
  151. return softness;
  152. }
  153. ERR_FAIL_V(0);
  154. }
  155. PinJoint2DSW::PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b) :
  156. Joint2DSW(_arr, p_body_b ? 2 : 1) {
  157. A = p_body_a;
  158. B = p_body_b;
  159. anchor_A = p_body_a->get_inv_transform().xform(p_pos);
  160. anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;
  161. softness = 0;
  162. p_body_a->add_constraint(this, 0);
  163. if (p_body_b) {
  164. p_body_b->add_constraint(this, 1);
  165. }
  166. }
  167. PinJoint2DSW::~PinJoint2DSW() {
  168. if (A) {
  169. A->remove_constraint(this);
  170. }
  171. if (B) {
  172. B->remove_constraint(this);
  173. }
  174. }
  175. //////////////////////////////////////////////
  176. //////////////////////////////////////////////
  177. //////////////////////////////////////////////
  178. static inline void
  179. k_tensor(Body2DSW *a, Body2DSW *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) {
  180. // calculate mass matrix
  181. // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
  182. real_t k11, k12, k21, k22;
  183. real_t m_sum = a->get_inv_mass() + b->get_inv_mass();
  184. // start with I*m_sum
  185. k11 = m_sum;
  186. k12 = 0.0f;
  187. k21 = 0.0f;
  188. k22 = m_sum;
  189. // add the influence from r1
  190. real_t a_i_inv = a->get_inv_inertia();
  191. real_t r1xsq = r1.x * r1.x * a_i_inv;
  192. real_t r1ysq = r1.y * r1.y * a_i_inv;
  193. real_t r1nxy = -r1.x * r1.y * a_i_inv;
  194. k11 += r1ysq;
  195. k12 += r1nxy;
  196. k21 += r1nxy;
  197. k22 += r1xsq;
  198. // add the influnce from r2
  199. real_t b_i_inv = b->get_inv_inertia();
  200. real_t r2xsq = r2.x * r2.x * b_i_inv;
  201. real_t r2ysq = r2.y * r2.y * b_i_inv;
  202. real_t r2nxy = -r2.x * r2.y * b_i_inv;
  203. k11 += r2ysq;
  204. k12 += r2nxy;
  205. k21 += r2nxy;
  206. k22 += r2xsq;
  207. // invert
  208. real_t determinant = k11 * k22 - k12 * k21;
  209. ERR_FAIL_COND(determinant == 0.0);
  210. real_t det_inv = 1.0f / determinant;
  211. *k1 = Vector2(k22 * det_inv, -k12 * det_inv);
  212. *k2 = Vector2(-k21 * det_inv, k11 * det_inv);
  213. }
  214. static _FORCE_INLINE_ Vector2
  215. mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) {
  216. return Vector2(vr.dot(k1), vr.dot(k2));
  217. }
  218. bool GrooveJoint2DSW::setup(real_t p_step) {
  219. if ((A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC) && (B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC)) {
  220. return false;
  221. }
  222. // calculate endpoints in worldspace
  223. Vector2 ta = A->get_transform().xform(A_groove_1);
  224. Vector2 tb = A->get_transform().xform(A_groove_2);
  225. Space2DSW *space = A->get_space();
  226. // calculate axis
  227. Vector2 n = -(tb - ta).tangent().normalized();
  228. real_t d = ta.dot(n);
  229. xf_normal = n;
  230. rB = B->get_transform().basis_xform(B_anchor);
  231. // calculate tangential distance along the axis of rB
  232. real_t td = (B->get_transform().get_origin() + rB).cross(n);
  233. // calculate clamping factor and rB
  234. if (td <= ta.cross(n)) {
  235. clamp = 1.0f;
  236. rA = ta - A->get_transform().get_origin();
  237. } else if (td >= tb.cross(n)) {
  238. clamp = -1.0f;
  239. rA = tb - A->get_transform().get_origin();
  240. } else {
  241. clamp = 0.0f;
  242. //joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
  243. rA = ((-n.tangent() * -td) + n * d) - A->get_transform().get_origin();
  244. }
  245. // Calculate mass tensor
  246. k_tensor(A, B, rA, rB, &k1, &k2);
  247. // compute max impulse
  248. jn_max = get_max_force() * p_step;
  249. // calculate bias velocity
  250. //cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
  251. //joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
  252. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  253. real_t _b = get_bias();
  254. gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).limit_length(get_max_bias());
  255. // apply accumulated impulse
  256. A->apply_impulse(rA, -jn_acc);
  257. B->apply_impulse(rB, jn_acc);
  258. correct = true;
  259. return true;
  260. }
  261. void GrooveJoint2DSW::solve(real_t p_step) {
  262. // compute impulse
  263. Vector2 vr = relative_velocity(A, B, rA, rB);
  264. Vector2 j = mult_k(gbias - vr, k1, k2);
  265. Vector2 jOld = jn_acc;
  266. j += jOld;
  267. jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).limit_length(jn_max);
  268. j = jn_acc - jOld;
  269. A->apply_impulse(rA, -j);
  270. B->apply_impulse(rB, j);
  271. }
  272. 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) :
  273. Joint2DSW(_arr, 2) {
  274. A = p_body_a;
  275. B = p_body_b;
  276. A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
  277. A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
  278. B_anchor = B->get_inv_transform().xform(p_b_anchor);
  279. A_groove_normal = -(A_groove_2 - A_groove_1).normalized().tangent();
  280. A->add_constraint(this, 0);
  281. B->add_constraint(this, 1);
  282. }
  283. GrooveJoint2DSW::~GrooveJoint2DSW() {
  284. A->remove_constraint(this);
  285. B->remove_constraint(this);
  286. }
  287. //////////////////////////////////////////////
  288. //////////////////////////////////////////////
  289. //////////////////////////////////////////////
  290. bool DampedSpringJoint2DSW::setup(real_t p_step) {
  291. if ((A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC) && (B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC)) {
  292. return false;
  293. }
  294. rA = A->get_transform().basis_xform(anchor_A);
  295. rB = B->get_transform().basis_xform(anchor_B);
  296. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  297. real_t dist = delta.length();
  298. if (dist) {
  299. n = delta / dist;
  300. } else {
  301. n = Vector2();
  302. }
  303. real_t k = k_scalar(A, B, rA, rB, n);
  304. n_mass = 1.0f / k;
  305. target_vrn = 0.0f;
  306. v_coef = 1.0f - Math::exp(-damping * (p_step)*k);
  307. // apply spring force
  308. real_t f_spring = (rest_length - dist) * stiffness;
  309. Vector2 j = n * f_spring * (p_step);
  310. A->apply_impulse(rA, -j);
  311. B->apply_impulse(rB, j);
  312. return true;
  313. }
  314. void DampedSpringJoint2DSW::solve(real_t p_step) {
  315. // compute relative velocity
  316. real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn;
  317. // compute velocity loss from drag
  318. // not 100% certain this is derived correctly, though it makes sense
  319. real_t v_damp = -vrn * v_coef;
  320. target_vrn = vrn + v_damp;
  321. Vector2 j = n * v_damp * n_mass;
  322. A->apply_impulse(rA, -j);
  323. B->apply_impulse(rB, j);
  324. }
  325. void DampedSpringJoint2DSW::set_param(Physics2DServer::DampedStringParam p_param, real_t p_value) {
  326. switch (p_param) {
  327. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  328. rest_length = p_value;
  329. } break;
  330. case Physics2DServer::DAMPED_STRING_DAMPING: {
  331. damping = p_value;
  332. } break;
  333. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  334. stiffness = p_value;
  335. } break;
  336. }
  337. }
  338. real_t DampedSpringJoint2DSW::get_param(Physics2DServer::DampedStringParam p_param) const {
  339. switch (p_param) {
  340. case Physics2DServer::DAMPED_STRING_REST_LENGTH: {
  341. return rest_length;
  342. } break;
  343. case Physics2DServer::DAMPED_STRING_DAMPING: {
  344. return damping;
  345. } break;
  346. case Physics2DServer::DAMPED_STRING_STIFFNESS: {
  347. return stiffness;
  348. } break;
  349. }
  350. ERR_FAIL_V(0);
  351. }
  352. DampedSpringJoint2DSW::DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b) :
  353. Joint2DSW(_arr, 2) {
  354. A = p_body_a;
  355. B = p_body_b;
  356. anchor_A = A->get_inv_transform().xform(p_anchor_a);
  357. anchor_B = B->get_inv_transform().xform(p_anchor_b);
  358. rest_length = p_anchor_a.distance_to(p_anchor_b);
  359. stiffness = 20;
  360. damping = 1.5;
  361. A->add_constraint(this, 0);
  362. B->add_constraint(this, 1);
  363. }
  364. DampedSpringJoint2DSW::~DampedSpringJoint2DSW() {
  365. A->remove_constraint(this);
  366. B->remove_constraint(this);
  367. }