godot_joints_2d.cpp 15 KB

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