godot_joints_2d.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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_NULL_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. // Compute max impulse.
  130. jn_max = get_max_force() * p_step;
  131. return true;
  132. }
  133. inline Vector2 custom_cross(const Vector2 &p_vec, real_t p_other) {
  134. return Vector2(p_other * p_vec.y, -p_other * p_vec.x);
  135. }
  136. bool GodotPinJoint2D::pre_solve(real_t p_step) {
  137. // Apply accumulated impulse.
  138. if (dynamic_A) {
  139. A->apply_impulse(-P, rA);
  140. }
  141. if (B && dynamic_B) {
  142. B->apply_impulse(P, rB);
  143. }
  144. // Angle limits joint pre_solve step taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpRotaryLimitJoint.c
  145. real_t i_sum_local = A->get_inv_inertia();
  146. if (B) {
  147. i_sum_local += B->get_inv_inertia();
  148. }
  149. i_sum = 1.0 / (i_sum_local);
  150. if (angular_limit_enabled && B) {
  151. Vector2 diff_vector = B->get_transform().get_origin() - A->get_transform().get_origin();
  152. diff_vector = diff_vector.rotated(-initial_angle);
  153. real_t dist = diff_vector.angle();
  154. real_t pdist = 0.0;
  155. if (dist > angular_limit_upper) {
  156. pdist = dist - angular_limit_upper;
  157. } else if (dist < angular_limit_lower) {
  158. pdist = dist - angular_limit_lower;
  159. }
  160. real_t error_bias = Math::pow(1.0 - 0.15, 60.0);
  161. // Calculate bias velocity.
  162. bias_velocity = -CLAMP((-1.0 - Math::pow(error_bias, p_step)) * pdist / p_step, -get_max_bias(), get_max_bias());
  163. // If the bias velocity is 0, the joint is not at a limit.
  164. if (bias_velocity >= -CMP_EPSILON && bias_velocity <= CMP_EPSILON) {
  165. j_acc = 0;
  166. is_joint_at_limit = false;
  167. } else {
  168. is_joint_at_limit = true;
  169. }
  170. } else {
  171. bias_velocity = 0.0;
  172. }
  173. return true;
  174. }
  175. void GodotPinJoint2D::solve(real_t p_step) {
  176. // Compute relative velocity.
  177. Vector2 vA = A->get_linear_velocity() - custom_cross(rA - A->get_center_of_mass(), A->get_angular_velocity());
  178. Vector2 rel_vel;
  179. if (B) {
  180. rel_vel = B->get_linear_velocity() - custom_cross(rB - B->get_center_of_mass(), B->get_angular_velocity()) - vA;
  181. } else {
  182. rel_vel = -vA;
  183. }
  184. // Angle limits joint solve step taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpRotaryLimitJoint.c
  185. if ((angular_limit_enabled || motor_enabled) && B) {
  186. // Compute relative rotational velocity.
  187. real_t wr = B->get_angular_velocity() - A->get_angular_velocity();
  188. // Motor solve part taken from https://github.com/slembcke/Chipmunk2D/blob/d0239ef4599b3688a5a336373f7d0a68426414ba/src/cpSimpleMotor.c
  189. if (motor_enabled) {
  190. wr -= motor_target_velocity;
  191. }
  192. real_t j_max = jn_max;
  193. // Compute normal impulse.
  194. real_t j = -(bias_velocity + wr) * i_sum;
  195. real_t j_old = j_acc;
  196. // Only enable the limits if we have to.
  197. if (angular_limit_enabled && is_joint_at_limit) {
  198. if (bias_velocity < 0.0) {
  199. j_acc = CLAMP(j_old + j, 0.0, j_max);
  200. } else {
  201. j_acc = CLAMP(j_old + j, -j_max, 0.0);
  202. }
  203. } else {
  204. j_acc = CLAMP(j_old + j, -j_max, j_max);
  205. }
  206. j = j_acc - j_old;
  207. A->apply_torque_impulse(-j * A->get_inv_inertia());
  208. B->apply_torque_impulse(j * B->get_inv_inertia());
  209. }
  210. Vector2 impulse = M.basis_xform(bias - rel_vel - Vector2(softness, softness) * P);
  211. if (dynamic_A) {
  212. A->apply_impulse(-impulse, rA);
  213. }
  214. if (B && dynamic_B) {
  215. B->apply_impulse(impulse, rB);
  216. }
  217. P += impulse;
  218. }
  219. void GodotPinJoint2D::set_param(PhysicsServer2D::PinJointParam p_param, real_t p_value) {
  220. switch (p_param) {
  221. case PhysicsServer2D::PIN_JOINT_SOFTNESS: {
  222. softness = p_value;
  223. } break;
  224. case PhysicsServer2D::PIN_JOINT_LIMIT_UPPER: {
  225. angular_limit_upper = p_value;
  226. } break;
  227. case PhysicsServer2D::PIN_JOINT_LIMIT_LOWER: {
  228. angular_limit_lower = p_value;
  229. } break;
  230. case PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY: {
  231. motor_target_velocity = p_value;
  232. } break;
  233. }
  234. }
  235. real_t GodotPinJoint2D::get_param(PhysicsServer2D::PinJointParam p_param) const {
  236. switch (p_param) {
  237. case PhysicsServer2D::PIN_JOINT_SOFTNESS: {
  238. return softness;
  239. }
  240. case PhysicsServer2D::PIN_JOINT_LIMIT_UPPER: {
  241. return angular_limit_upper;
  242. }
  243. case PhysicsServer2D::PIN_JOINT_LIMIT_LOWER: {
  244. return angular_limit_lower;
  245. }
  246. case PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY: {
  247. return motor_target_velocity;
  248. }
  249. }
  250. ERR_FAIL_V(0);
  251. }
  252. void GodotPinJoint2D::set_flag(PhysicsServer2D::PinJointFlag p_flag, bool p_enabled) {
  253. switch (p_flag) {
  254. case PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED: {
  255. angular_limit_enabled = p_enabled;
  256. } break;
  257. case PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED: {
  258. motor_enabled = p_enabled;
  259. } break;
  260. }
  261. }
  262. bool GodotPinJoint2D::get_flag(PhysicsServer2D::PinJointFlag p_flag) const {
  263. switch (p_flag) {
  264. case PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED: {
  265. return angular_limit_enabled;
  266. }
  267. case PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED: {
  268. return motor_enabled;
  269. }
  270. }
  271. ERR_FAIL_V(false);
  272. }
  273. GodotPinJoint2D::GodotPinJoint2D(const Vector2 &p_pos, GodotBody2D *p_body_a, GodotBody2D *p_body_b) :
  274. GodotJoint2D(_arr, p_body_b ? 2 : 1) {
  275. A = p_body_a;
  276. B = p_body_b;
  277. anchor_A = p_body_a->get_inv_transform().xform(p_pos);
  278. anchor_B = p_body_b ? p_body_b->get_inv_transform().xform(p_pos) : p_pos;
  279. p_body_a->add_constraint(this, 0);
  280. if (p_body_b) {
  281. p_body_b->add_constraint(this, 1);
  282. initial_angle = A->get_transform().get_origin().angle_to_point(B->get_transform().get_origin());
  283. }
  284. }
  285. //////////////////////////////////////////////
  286. //////////////////////////////////////////////
  287. //////////////////////////////////////////////
  288. static inline void
  289. k_tensor(GodotBody2D *a, GodotBody2D *b, Vector2 r1, Vector2 r2, Vector2 *k1, Vector2 *k2) {
  290. // calculate mass matrix
  291. // If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
  292. real_t k11, k12, k21, k22;
  293. real_t m_sum = a->get_inv_mass() + b->get_inv_mass();
  294. // start with I*m_sum
  295. k11 = m_sum;
  296. k12 = 0.0f;
  297. k21 = 0.0f;
  298. k22 = m_sum;
  299. r1 -= a->get_center_of_mass();
  300. r2 -= b->get_center_of_mass();
  301. // add the influence from r1
  302. real_t a_i_inv = a->get_inv_inertia();
  303. real_t r1xsq = r1.x * r1.x * a_i_inv;
  304. real_t r1ysq = r1.y * r1.y * a_i_inv;
  305. real_t r1nxy = -r1.x * r1.y * a_i_inv;
  306. k11 += r1ysq;
  307. k12 += r1nxy;
  308. k21 += r1nxy;
  309. k22 += r1xsq;
  310. // add the influnce from r2
  311. real_t b_i_inv = b->get_inv_inertia();
  312. real_t r2xsq = r2.x * r2.x * b_i_inv;
  313. real_t r2ysq = r2.y * r2.y * b_i_inv;
  314. real_t r2nxy = -r2.x * r2.y * b_i_inv;
  315. k11 += r2ysq;
  316. k12 += r2nxy;
  317. k21 += r2nxy;
  318. k22 += r2xsq;
  319. // invert
  320. real_t determinant = k11 * k22 - k12 * k21;
  321. ERR_FAIL_COND(determinant == 0.0);
  322. real_t det_inv = 1.0f / determinant;
  323. *k1 = Vector2(k22 * det_inv, -k12 * det_inv);
  324. *k2 = Vector2(-k21 * det_inv, k11 * det_inv);
  325. }
  326. static _FORCE_INLINE_ Vector2
  327. mult_k(const Vector2 &vr, const Vector2 &k1, const Vector2 &k2) {
  328. return Vector2(vr.dot(k1), vr.dot(k2));
  329. }
  330. bool GodotGrooveJoint2D::setup(real_t p_step) {
  331. dynamic_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC);
  332. dynamic_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC);
  333. if (!dynamic_A && !dynamic_B) {
  334. return false;
  335. }
  336. GodotSpace2D *space = A->get_space();
  337. ERR_FAIL_NULL_V(space, false);
  338. // calculate endpoints in worldspace
  339. Vector2 ta = A->get_transform().xform(A_groove_1);
  340. Vector2 tb = A->get_transform().xform(A_groove_2);
  341. // calculate axis
  342. Vector2 n = -(tb - ta).orthogonal().normalized();
  343. real_t d = ta.dot(n);
  344. xf_normal = n;
  345. rB = B->get_transform().basis_xform(B_anchor);
  346. // calculate tangential distance along the axis of rB
  347. real_t td = (B->get_transform().get_origin() + rB).cross(n);
  348. // calculate clamping factor and rB
  349. if (td <= ta.cross(n)) {
  350. clamp = 1.0f;
  351. rA = ta - A->get_transform().get_origin();
  352. } else if (td >= tb.cross(n)) {
  353. clamp = -1.0f;
  354. rA = tb - A->get_transform().get_origin();
  355. } else {
  356. clamp = 0.0f;
  357. //joint->r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a->p);
  358. rA = ((-n.orthogonal() * -td) + n * d) - A->get_transform().get_origin();
  359. }
  360. // Calculate mass tensor
  361. k_tensor(A, B, rA, rB, &k1, &k2);
  362. // compute max impulse
  363. jn_max = get_max_force() * p_step;
  364. // calculate bias velocity
  365. //cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
  366. //joint->bias = cpvclamp(cpvmult(delta, -joint->constraint.biasCoef*dt_inv), joint->constraint.maxBias);
  367. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  368. real_t _b = get_bias();
  369. gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).limit_length(get_max_bias());
  370. correct = true;
  371. return true;
  372. }
  373. bool GodotGrooveJoint2D::pre_solve(real_t p_step) {
  374. // Apply accumulated impulse.
  375. if (dynamic_A) {
  376. A->apply_impulse(-jn_acc, rA);
  377. }
  378. if (dynamic_B) {
  379. B->apply_impulse(jn_acc, rB);
  380. }
  381. return true;
  382. }
  383. void GodotGrooveJoint2D::solve(real_t p_step) {
  384. // compute impulse
  385. Vector2 vr = relative_velocity(A, B, rA, rB);
  386. Vector2 j = mult_k(gbias - vr, k1, k2);
  387. Vector2 jOld = jn_acc;
  388. j += jOld;
  389. jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).limit_length(jn_max);
  390. j = jn_acc - jOld;
  391. if (dynamic_A) {
  392. A->apply_impulse(-j, rA);
  393. }
  394. if (dynamic_B) {
  395. B->apply_impulse(j, rB);
  396. }
  397. }
  398. 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) :
  399. GodotJoint2D(_arr, 2) {
  400. A = p_body_a;
  401. B = p_body_b;
  402. A_groove_1 = A->get_inv_transform().xform(p_a_groove1);
  403. A_groove_2 = A->get_inv_transform().xform(p_a_groove2);
  404. B_anchor = B->get_inv_transform().xform(p_b_anchor);
  405. A_groove_normal = -(A_groove_2 - A_groove_1).normalized().orthogonal();
  406. A->add_constraint(this, 0);
  407. B->add_constraint(this, 1);
  408. }
  409. //////////////////////////////////////////////
  410. //////////////////////////////////////////////
  411. //////////////////////////////////////////////
  412. bool GodotDampedSpringJoint2D::setup(real_t p_step) {
  413. dynamic_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC);
  414. dynamic_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC);
  415. if (!dynamic_A && !dynamic_B) {
  416. return false;
  417. }
  418. rA = A->get_transform().basis_xform(anchor_A);
  419. rB = B->get_transform().basis_xform(anchor_B);
  420. Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA);
  421. real_t dist = delta.length();
  422. if (dist) {
  423. n = delta / dist;
  424. } else {
  425. n = Vector2();
  426. }
  427. real_t k = k_scalar(A, B, rA, rB, n);
  428. n_mass = 1.0f / k;
  429. target_vrn = 0.0f;
  430. v_coef = 1.0f - Math::exp(-damping * (p_step)*k);
  431. // Calculate spring force.
  432. real_t f_spring = (rest_length - dist) * stiffness;
  433. j = n * f_spring * (p_step);
  434. return true;
  435. }
  436. bool GodotDampedSpringJoint2D::pre_solve(real_t p_step) {
  437. // Apply spring force.
  438. if (dynamic_A) {
  439. A->apply_impulse(-j, rA);
  440. }
  441. if (dynamic_B) {
  442. B->apply_impulse(j, rB);
  443. }
  444. return true;
  445. }
  446. void GodotDampedSpringJoint2D::solve(real_t p_step) {
  447. // compute relative velocity
  448. real_t vrn = normal_relative_velocity(A, B, rA, rB, n) - target_vrn;
  449. // compute velocity loss from drag
  450. // not 100% certain this is derived correctly, though it makes sense
  451. real_t v_damp = -vrn * v_coef;
  452. target_vrn = vrn + v_damp;
  453. Vector2 j_new = n * v_damp * n_mass;
  454. if (dynamic_A) {
  455. A->apply_impulse(-j_new, rA);
  456. }
  457. if (dynamic_B) {
  458. B->apply_impulse(j_new, rB);
  459. }
  460. }
  461. void GodotDampedSpringJoint2D::set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value) {
  462. switch (p_param) {
  463. case PhysicsServer2D::DAMPED_SPRING_REST_LENGTH: {
  464. rest_length = p_value;
  465. } break;
  466. case PhysicsServer2D::DAMPED_SPRING_DAMPING: {
  467. damping = p_value;
  468. } break;
  469. case PhysicsServer2D::DAMPED_SPRING_STIFFNESS: {
  470. stiffness = p_value;
  471. } break;
  472. }
  473. }
  474. real_t GodotDampedSpringJoint2D::get_param(PhysicsServer2D::DampedSpringParam p_param) const {
  475. switch (p_param) {
  476. case PhysicsServer2D::DAMPED_SPRING_REST_LENGTH: {
  477. return rest_length;
  478. } break;
  479. case PhysicsServer2D::DAMPED_SPRING_DAMPING: {
  480. return damping;
  481. } break;
  482. case PhysicsServer2D::DAMPED_SPRING_STIFFNESS: {
  483. return stiffness;
  484. } break;
  485. }
  486. ERR_FAIL_V(0);
  487. }
  488. GodotDampedSpringJoint2D::GodotDampedSpringJoint2D(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, GodotBody2D *p_body_a, GodotBody2D *p_body_b) :
  489. GodotJoint2D(_arr, 2) {
  490. A = p_body_a;
  491. B = p_body_b;
  492. anchor_A = A->get_inv_transform().xform(p_anchor_a);
  493. anchor_B = B->get_inv_transform().xform(p_anchor_b);
  494. rest_length = p_anchor_a.distance_to(p_anchor_b);
  495. A->add_constraint(this, 0);
  496. B->add_constraint(this, 1);
  497. }