godot_joints_2d.cpp 15 KB

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