godot_body_pair_2d.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*************************************************************************/
  2. /* godot_body_pair_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_body_pair_2d.h"
  31. #include "godot_collision_solver_2d.h"
  32. #include "godot_space_2d.h"
  33. #define ACCUMULATE_IMPULSES
  34. #define MIN_VELOCITY 0.001
  35. #define MAX_BIAS_ROTATION (Math_PI / 8)
  36. void GodotBodyPair2D::_add_contact(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_self) {
  37. GodotBodyPair2D *self = static_cast<GodotBodyPair2D *>(p_self);
  38. self->_contact_added_callback(p_point_A, p_point_B);
  39. }
  40. void GodotBodyPair2D::_contact_added_callback(const Vector2 &p_point_A, const Vector2 &p_point_B) {
  41. Vector2 local_A = A->get_inv_transform().basis_xform(p_point_A);
  42. Vector2 local_B = B->get_inv_transform().basis_xform(p_point_B - offset_B);
  43. int new_index = contact_count;
  44. ERR_FAIL_COND(new_index >= (MAX_CONTACTS + 1));
  45. Contact contact;
  46. contact.local_A = local_A;
  47. contact.local_B = local_B;
  48. contact.normal = (p_point_A - p_point_B).normalized();
  49. contact.used = true;
  50. // Attempt to determine if the contact will be reused.
  51. real_t recycle_radius_2 = space->get_contact_recycle_radius() * space->get_contact_recycle_radius();
  52. for (int i = 0; i < contact_count; i++) {
  53. Contact &c = contacts[i];
  54. if (c.local_A.distance_squared_to(local_A) < (recycle_radius_2) &&
  55. c.local_B.distance_squared_to(local_B) < (recycle_radius_2)) {
  56. contact.acc_normal_impulse = c.acc_normal_impulse;
  57. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  58. contact.acc_bias_impulse = c.acc_bias_impulse;
  59. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  60. c = contact;
  61. return;
  62. }
  63. }
  64. // Figure out if the contact amount must be reduced to fit the new contact.
  65. if (new_index == MAX_CONTACTS) {
  66. // Remove the contact with the minimum depth.
  67. const Transform2D &transform_A = A->get_transform();
  68. const Transform2D &transform_B = B->get_transform();
  69. int least_deep = -1;
  70. real_t min_depth;
  71. // Start with depth for new contact.
  72. {
  73. Vector2 global_A = transform_A.basis_xform(contact.local_A);
  74. Vector2 global_B = transform_B.basis_xform(contact.local_B) + offset_B;
  75. Vector2 axis = global_A - global_B;
  76. min_depth = axis.dot(contact.normal);
  77. }
  78. for (int i = 0; i < contact_count; i++) {
  79. const Contact &c = contacts[i];
  80. Vector2 global_A = transform_A.basis_xform(c.local_A);
  81. Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
  82. Vector2 axis = global_A - global_B;
  83. real_t depth = axis.dot(c.normal);
  84. if (depth < min_depth) {
  85. min_depth = depth;
  86. least_deep = i;
  87. }
  88. }
  89. if (least_deep > -1) {
  90. // Replace the least deep contact by the new one.
  91. contacts[least_deep] = contact;
  92. }
  93. return;
  94. }
  95. contacts[new_index] = contact;
  96. contact_count++;
  97. }
  98. void GodotBodyPair2D::_validate_contacts() {
  99. // Make sure to erase contacts that are no longer valid.
  100. real_t max_separation = space->get_contact_max_separation();
  101. real_t max_separation2 = max_separation * max_separation;
  102. const Transform2D &transform_A = A->get_transform();
  103. const Transform2D &transform_B = B->get_transform();
  104. for (int i = 0; i < contact_count; i++) {
  105. Contact &c = contacts[i];
  106. bool erase = false;
  107. if (!c.used) {
  108. // Was left behind in previous frame.
  109. erase = true;
  110. } else {
  111. c.used = false;
  112. Vector2 global_A = transform_A.basis_xform(c.local_A);
  113. Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
  114. Vector2 axis = global_A - global_B;
  115. real_t depth = axis.dot(c.normal);
  116. if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {
  117. erase = true;
  118. }
  119. }
  120. if (erase) {
  121. // Contact no longer needed, remove.
  122. if ((i + 1) < contact_count) {
  123. // Swap with the last one.
  124. SWAP(contacts[i], contacts[contact_count - 1]);
  125. }
  126. i--;
  127. contact_count--;
  128. }
  129. }
  130. }
  131. bool GodotBodyPair2D::_test_ccd(real_t p_step, GodotBody2D *p_A, int p_shape_A, const Transform2D &p_xform_A, GodotBody2D *p_B, int p_shape_B, const Transform2D &p_xform_B) {
  132. Vector2 motion = p_A->get_linear_velocity() * p_step;
  133. real_t mlen = motion.length();
  134. if (mlen < CMP_EPSILON) {
  135. return false;
  136. }
  137. Vector2 mnormal = motion / mlen;
  138. real_t min, max;
  139. p_A->get_shape(p_shape_A)->project_rangev(mnormal, p_xform_A, min, max);
  140. // Did it move enough in this direction to even attempt raycast?
  141. // Let's say it should move more than 1/3 the size of the object in that axis.
  142. bool fast_object = mlen > (max - min) * 0.3;
  143. if (!fast_object) {
  144. return false;
  145. }
  146. // Going too fast in that direction.
  147. // Cast a segment from support in motion normal, in the same direction of motion by motion length.
  148. // Support is the worst case collision point, so real collision happened before.
  149. int a;
  150. Vector2 s[2];
  151. p_A->get_shape(p_shape_A)->get_supports(p_xform_A.basis_xform(mnormal).normalized(), s, a);
  152. Vector2 from = p_xform_A.xform(s[0]);
  153. Vector2 to = from + motion;
  154. Transform2D from_inv = p_xform_B.affine_inverse();
  155. // Start from a little inside the bounding box.
  156. Vector2 local_from = from_inv.xform(from - mnormal * mlen * 0.1);
  157. Vector2 local_to = from_inv.xform(to);
  158. Vector2 rpos, rnorm;
  159. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  160. return false;
  161. }
  162. // Check one-way collision based on motion direction.
  163. if (p_A->get_shape(p_shape_A)->allows_one_way_collision() && p_B->is_shape_set_as_one_way_collision(p_shape_B)) {
  164. Vector2 direction = p_xform_B.get_axis(1).normalized();
  165. if (direction.dot(mnormal) < CMP_EPSILON) {
  166. collided = false;
  167. oneway_disabled = true;
  168. return false;
  169. }
  170. }
  171. // Shorten the linear velocity so it does not hit, but gets close enough,
  172. // next frame will hit softly or soft enough.
  173. Vector2 hitpos = p_xform_B.xform(rpos);
  174. real_t newlen = hitpos.distance_to(from) - (max - min) * 0.01;
  175. p_A->set_linear_velocity(mnormal * (newlen / p_step));
  176. return true;
  177. }
  178. real_t combine_bounce(GodotBody2D *A, GodotBody2D *B) {
  179. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  180. }
  181. real_t combine_friction(GodotBody2D *A, GodotBody2D *B) {
  182. return ABS(MIN(A->get_friction(), B->get_friction()));
  183. }
  184. bool GodotBodyPair2D::setup(real_t p_step) {
  185. check_ccd = false;
  186. if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
  187. collided = false;
  188. return false;
  189. }
  190. collide_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && A->collides_with(B);
  191. collide_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && B->collides_with(A);
  192. report_contacts_only = false;
  193. if (!collide_A && !collide_B) {
  194. if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
  195. report_contacts_only = true;
  196. } else {
  197. collided = false;
  198. return false;
  199. }
  200. }
  201. //use local A coordinates to avoid numerical issues on collision detection
  202. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  203. _validate_contacts();
  204. const Vector2 &offset_A = A->get_transform().get_origin();
  205. Transform2D xform_Au = A->get_transform().untranslated();
  206. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  207. Transform2D xform_Bu = B->get_transform();
  208. xform_Bu.columns[2] -= offset_A;
  209. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  210. GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
  211. GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
  212. Vector2 motion_A, motion_B;
  213. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
  214. motion_A = A->get_motion();
  215. }
  216. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
  217. motion_B = B->get_motion();
  218. }
  219. bool prev_collided = collided;
  220. collided = GodotCollisionSolver2D::solve(shape_A_ptr, xform_A, motion_A, shape_B_ptr, xform_B, motion_B, _add_contact, this, &sep_axis);
  221. if (!collided) {
  222. oneway_disabled = false;
  223. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
  224. check_ccd = true;
  225. return true;
  226. }
  227. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
  228. check_ccd = true;
  229. return true;
  230. }
  231. return false;
  232. }
  233. if (oneway_disabled) {
  234. return false;
  235. }
  236. if (!prev_collided) {
  237. if (shape_B_ptr->allows_one_way_collision() && A->is_shape_set_as_one_way_collision(shape_A)) {
  238. Vector2 direction = xform_A.get_axis(1).normalized();
  239. bool valid = false;
  240. for (int i = 0; i < contact_count; i++) {
  241. Contact &c = contacts[i];
  242. if (c.normal.dot(direction) > -CMP_EPSILON) { // Greater (normal inverted).
  243. continue;
  244. }
  245. valid = true;
  246. break;
  247. }
  248. if (!valid) {
  249. collided = false;
  250. oneway_disabled = true;
  251. return false;
  252. }
  253. }
  254. if (shape_A_ptr->allows_one_way_collision() && B->is_shape_set_as_one_way_collision(shape_B)) {
  255. Vector2 direction = xform_B.get_axis(1).normalized();
  256. bool valid = false;
  257. for (int i = 0; i < contact_count; i++) {
  258. Contact &c = contacts[i];
  259. if (c.normal.dot(direction) < CMP_EPSILON) { // Less (normal ok).
  260. continue;
  261. }
  262. valid = true;
  263. break;
  264. }
  265. if (!valid) {
  266. collided = false;
  267. oneway_disabled = true;
  268. return false;
  269. }
  270. }
  271. }
  272. return true;
  273. }
  274. bool GodotBodyPair2D::pre_solve(real_t p_step) {
  275. if (oneway_disabled) {
  276. return false;
  277. }
  278. if (!collided) {
  279. if (check_ccd) {
  280. const Vector2 &offset_A = A->get_transform().get_origin();
  281. Transform2D xform_Au = A->get_transform().untranslated();
  282. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  283. Transform2D xform_Bu = B->get_transform();
  284. xform_Bu.columns[2] -= offset_A;
  285. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  286. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
  287. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  288. }
  289. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
  290. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  291. }
  292. }
  293. return false;
  294. }
  295. real_t max_penetration = space->get_contact_max_allowed_penetration();
  296. real_t bias = space->get_contact_bias();
  297. GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
  298. GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
  299. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  300. if (shape_A_ptr->get_custom_bias() == 0) {
  301. bias = shape_B_ptr->get_custom_bias();
  302. } else if (shape_B_ptr->get_custom_bias() == 0) {
  303. bias = shape_A_ptr->get_custom_bias();
  304. } else {
  305. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  306. }
  307. }
  308. real_t inv_dt = 1.0 / p_step;
  309. bool do_process = false;
  310. const Vector2 &offset_A = A->get_transform().get_origin();
  311. const Transform2D &transform_A = A->get_transform();
  312. const Transform2D &transform_B = B->get_transform();
  313. real_t inv_inertia_A = collide_A ? A->get_inv_inertia() : 0.0;
  314. real_t inv_inertia_B = collide_B ? B->get_inv_inertia() : 0.0;
  315. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  316. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  317. for (int i = 0; i < contact_count; i++) {
  318. Contact &c = contacts[i];
  319. c.active = false;
  320. Vector2 global_A = transform_A.basis_xform(c.local_A);
  321. Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
  322. Vector2 axis = global_A - global_B;
  323. real_t depth = axis.dot(c.normal);
  324. if (depth <= 0.0) {
  325. continue;
  326. }
  327. #ifdef DEBUG_ENABLED
  328. if (space->is_debugging_contacts()) {
  329. space->add_debug_contact(global_A + offset_A);
  330. space->add_debug_contact(global_B + offset_A);
  331. }
  332. #endif
  333. c.rA = global_A - A->get_center_of_mass();
  334. c.rB = global_B - B->get_center_of_mass() - offset_B;
  335. if (A->can_report_contacts()) {
  336. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  337. A->add_contact(global_A + offset_A, -c.normal, depth, shape_A, global_B + offset_A, shape_B, B->get_instance_id(), B->get_self(), crB + B->get_linear_velocity());
  338. }
  339. if (B->can_report_contacts()) {
  340. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  341. B->add_contact(global_B + offset_A, c.normal, depth, shape_B, global_A + offset_A, shape_A, A->get_instance_id(), A->get_self(), crA + A->get_linear_velocity());
  342. }
  343. if (report_contacts_only) {
  344. collided = false;
  345. continue;
  346. }
  347. // Precompute normal mass, tangent mass, and bias.
  348. real_t rnA = c.rA.dot(c.normal);
  349. real_t rnB = c.rB.dot(c.normal);
  350. real_t kNormal = inv_mass_A + inv_mass_B;
  351. kNormal += inv_inertia_A * (c.rA.dot(c.rA) - rnA * rnA) + inv_inertia_B * (c.rB.dot(c.rB) - rnB * rnB);
  352. c.mass_normal = 1.0f / kNormal;
  353. Vector2 tangent = c.normal.orthogonal();
  354. real_t rtA = c.rA.dot(tangent);
  355. real_t rtB = c.rB.dot(tangent);
  356. real_t kTangent = inv_mass_A + inv_mass_B;
  357. kTangent += inv_inertia_A * (c.rA.dot(c.rA) - rtA * rtA) + inv_inertia_B * (c.rB.dot(c.rB) - rtB * rtB);
  358. c.mass_tangent = 1.0f / kTangent;
  359. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  360. c.depth = depth;
  361. #ifdef ACCUMULATE_IMPULSES
  362. {
  363. // Apply normal + friction impulse
  364. Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent;
  365. if (collide_A) {
  366. A->apply_impulse(-P, c.rA + A->get_center_of_mass());
  367. }
  368. if (collide_B) {
  369. B->apply_impulse(P, c.rB + B->get_center_of_mass());
  370. }
  371. }
  372. #endif
  373. c.bounce = combine_bounce(A, B);
  374. if (c.bounce) {
  375. Vector2 crA(-A->get_prev_angular_velocity() * c.rA.y, A->get_prev_angular_velocity() * c.rA.x);
  376. Vector2 crB(-B->get_prev_angular_velocity() * c.rB.y, B->get_prev_angular_velocity() * c.rB.x);
  377. Vector2 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;
  378. c.bounce = c.bounce * dv.dot(c.normal);
  379. }
  380. c.active = true;
  381. do_process = true;
  382. }
  383. return do_process;
  384. }
  385. void GodotBodyPair2D::solve(real_t p_step) {
  386. if (!collided || oneway_disabled) {
  387. return;
  388. }
  389. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  390. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  391. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  392. for (int i = 0; i < contact_count; ++i) {
  393. Contact &c = contacts[i];
  394. if (!c.active) {
  395. continue;
  396. }
  397. // Relative velocity at contact
  398. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  399. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  400. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  401. Vector2 crbA(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  402. Vector2 crbB(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  403. Vector2 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  404. real_t vn = dv.dot(c.normal);
  405. real_t vbn = dbv.dot(c.normal);
  406. Vector2 tangent = c.normal.orthogonal();
  407. real_t vt = dv.dot(tangent);
  408. real_t jbn = (c.bias - vbn) * c.mass_normal;
  409. real_t jbnOld = c.acc_bias_impulse;
  410. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  411. Vector2 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  412. if (collide_A) {
  413. A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);
  414. }
  415. if (collide_B) {
  416. B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);
  417. }
  418. crbA = Vector2(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  419. crbB = Vector2(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  420. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  421. vbn = dbv.dot(c.normal);
  422. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  423. real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);
  424. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  425. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  426. Vector2 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  427. if (collide_A) {
  428. A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);
  429. }
  430. if (collide_B) {
  431. B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);
  432. }
  433. }
  434. real_t jn = -(c.bounce + vn) * c.mass_normal;
  435. real_t jnOld = c.acc_normal_impulse;
  436. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  437. real_t friction = combine_friction(A, B);
  438. real_t jtMax = friction * c.acc_normal_impulse;
  439. real_t jt = -vt * c.mass_tangent;
  440. real_t jtOld = c.acc_tangent_impulse;
  441. c.acc_tangent_impulse = CLAMP(jtOld + jt, -jtMax, jtMax);
  442. Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld);
  443. if (collide_A) {
  444. A->apply_impulse(-j, c.rA + A->get_center_of_mass());
  445. }
  446. if (collide_B) {
  447. B->apply_impulse(j, c.rB + B->get_center_of_mass());
  448. }
  449. }
  450. }
  451. GodotBodyPair2D::GodotBodyPair2D(GodotBody2D *p_A, int p_shape_A, GodotBody2D *p_B, int p_shape_B) :
  452. GodotConstraint2D(_arr, 2) {
  453. A = p_A;
  454. B = p_B;
  455. shape_A = p_shape_A;
  456. shape_B = p_shape_B;
  457. space = A->get_space();
  458. A->add_constraint(this, 0);
  459. B->add_constraint(this, 1);
  460. }
  461. GodotBodyPair2D::~GodotBodyPair2D() {
  462. A->remove_constraint(this, 0);
  463. B->remove_constraint(this, 1);
  464. }