godot_body_pair_2d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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) 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_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. // _test_ccd prevents tunneling by slowing down a high velocity body that is about to collide so that next frame it will be at an appropriate location to collide (i.e. slight overlap)
  132. // Warning: the way velocity is adjusted down to cause a collision means the momentum will be weaker than it should for a bounce!
  133. // Process: only proceed if body A's motion is high relative to its size.
  134. // cast forward along motion vector to see if A is going to enter/pass B's collider next frame, only proceed if it does.
  135. // adjust the velocity of A down so that it will just slightly intersect the collider instead of blowing right past it.
  136. 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) {
  137. Vector2 motion = p_A->get_linear_velocity() * p_step;
  138. real_t mlen = motion.length();
  139. if (mlen < CMP_EPSILON) {
  140. return false;
  141. }
  142. Vector2 mnormal = motion / mlen;
  143. real_t min = 0.0, max = 0.0;
  144. p_A->get_shape(p_shape_A)->project_rangev(mnormal, p_xform_A, min, max);
  145. // Did it move enough in this direction to even attempt raycast?
  146. // Let's say it should move more than 1/3 the size of the object in that axis.
  147. bool fast_object = mlen > (max - min) * 0.3;
  148. if (!fast_object) {
  149. return false;
  150. }
  151. // A is moving fast enough that tunneling might occur. See if it's really about to collide.
  152. // Cast a segment from support in motion normal, in the same direction of motion by motion length.
  153. // Support point will the farthest forward collision point along the movement vector.
  154. // i.e. the point that should hit B first if any collision does occur.
  155. // convert mnormal into body A's local xform because get_support requires (and returns) local coordinates.
  156. int a;
  157. Vector2 s[2];
  158. p_A->get_shape(p_shape_A)->get_supports(p_xform_A.basis_xform_inv(mnormal).normalized(), s, a);
  159. Vector2 from = p_xform_A.xform(s[0]);
  160. // Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.
  161. // This should ensure the calculated new velocity will really cause a bit of overlap instead of just getting us very close.
  162. Vector2 to = from + motion;
  163. Transform2D from_inv = p_xform_B.affine_inverse();
  164. // Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.
  165. // At high speeds, this may mean we're actually casting from well behind the body instead of inside it, which is odd. But it still works out.
  166. Vector2 local_from = from_inv.xform(from - motion * 0.1);
  167. Vector2 local_to = from_inv.xform(to);
  168. Vector2 rpos, rnorm;
  169. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  170. // there was no hit. Since the segment is the length of per-frame motion, this means the bodies will not
  171. // actually collide yet on next frame. We'll probably check again next frame once they're closer.
  172. return false;
  173. }
  174. // Check one-way collision based on motion direction.
  175. if (p_A->get_shape(p_shape_A)->allows_one_way_collision() && p_B->is_shape_set_as_one_way_collision(p_shape_B)) {
  176. Vector2 direction = p_xform_B.columns[1].normalized();
  177. if (direction.dot(mnormal) < CMP_EPSILON) {
  178. collided = false;
  179. oneway_disabled = true;
  180. return false;
  181. }
  182. }
  183. // Shorten the linear velocity so it does not hit, but gets close enough,
  184. // next frame will hit softly or soft enough.
  185. Vector2 hitpos = p_xform_B.xform(rpos);
  186. real_t newlen = hitpos.distance_to(from) + (max - min) * 0.01; // adding 1% of body length to the distance between collision and support point should cause body A's support point to arrive just within B's collider next frame.
  187. p_A->set_linear_velocity(mnormal * (newlen / p_step));
  188. return true;
  189. }
  190. real_t combine_bounce(GodotBody2D *A, GodotBody2D *B) {
  191. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  192. }
  193. real_t combine_friction(GodotBody2D *A, GodotBody2D *B) {
  194. return ABS(MIN(A->get_friction(), B->get_friction()));
  195. }
  196. bool GodotBodyPair2D::setup(real_t p_step) {
  197. check_ccd = false;
  198. if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
  199. collided = false;
  200. return false;
  201. }
  202. collide_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && A->collides_with(B);
  203. collide_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && B->collides_with(A);
  204. report_contacts_only = false;
  205. if (!collide_A && !collide_B) {
  206. if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
  207. report_contacts_only = true;
  208. } else {
  209. collided = false;
  210. return false;
  211. }
  212. }
  213. //use local A coordinates to avoid numerical issues on collision detection
  214. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  215. _validate_contacts();
  216. const Vector2 &offset_A = A->get_transform().get_origin();
  217. Transform2D xform_Au = A->get_transform().untranslated();
  218. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  219. Transform2D xform_Bu = B->get_transform();
  220. xform_Bu.columns[2] -= offset_A;
  221. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  222. GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
  223. GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
  224. Vector2 motion_A, motion_B;
  225. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
  226. motion_A = A->get_motion();
  227. }
  228. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
  229. motion_B = B->get_motion();
  230. }
  231. bool prev_collided = collided;
  232. collided = GodotCollisionSolver2D::solve(shape_A_ptr, xform_A, motion_A, shape_B_ptr, xform_B, motion_B, _add_contact, this, &sep_axis);
  233. if (!collided) {
  234. oneway_disabled = false;
  235. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
  236. check_ccd = true;
  237. return true;
  238. }
  239. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
  240. check_ccd = true;
  241. return true;
  242. }
  243. return false;
  244. }
  245. if (oneway_disabled) {
  246. return false;
  247. }
  248. if (!prev_collided) {
  249. if (shape_B_ptr->allows_one_way_collision() && A->is_shape_set_as_one_way_collision(shape_A)) {
  250. Vector2 direction = xform_A.columns[1].normalized();
  251. bool valid = false;
  252. for (int i = 0; i < contact_count; i++) {
  253. Contact &c = contacts[i];
  254. if (c.normal.dot(direction) > -CMP_EPSILON) { // Greater (normal inverted).
  255. continue;
  256. }
  257. valid = true;
  258. break;
  259. }
  260. if (!valid) {
  261. collided = false;
  262. oneway_disabled = true;
  263. return false;
  264. }
  265. }
  266. if (shape_A_ptr->allows_one_way_collision() && B->is_shape_set_as_one_way_collision(shape_B)) {
  267. Vector2 direction = xform_B.columns[1].normalized();
  268. bool valid = false;
  269. for (int i = 0; i < contact_count; i++) {
  270. Contact &c = contacts[i];
  271. if (c.normal.dot(direction) < CMP_EPSILON) { // Less (normal ok).
  272. continue;
  273. }
  274. valid = true;
  275. break;
  276. }
  277. if (!valid) {
  278. collided = false;
  279. oneway_disabled = true;
  280. return false;
  281. }
  282. }
  283. }
  284. return true;
  285. }
  286. bool GodotBodyPair2D::pre_solve(real_t p_step) {
  287. if (oneway_disabled) {
  288. return false;
  289. }
  290. if (!collided) {
  291. if (check_ccd) {
  292. const Vector2 &offset_A = A->get_transform().get_origin();
  293. Transform2D xform_Au = A->get_transform().untranslated();
  294. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  295. Transform2D xform_Bu = B->get_transform();
  296. xform_Bu.columns[2] -= offset_A;
  297. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  298. if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
  299. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  300. }
  301. if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
  302. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  303. }
  304. }
  305. return false;
  306. }
  307. real_t max_penetration = space->get_contact_max_allowed_penetration();
  308. real_t bias = space->get_contact_bias();
  309. GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
  310. GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
  311. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  312. if (shape_A_ptr->get_custom_bias() == 0) {
  313. bias = shape_B_ptr->get_custom_bias();
  314. } else if (shape_B_ptr->get_custom_bias() == 0) {
  315. bias = shape_A_ptr->get_custom_bias();
  316. } else {
  317. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  318. }
  319. }
  320. real_t inv_dt = 1.0 / p_step;
  321. bool do_process = false;
  322. const Vector2 &offset_A = A->get_transform().get_origin();
  323. const Transform2D &transform_A = A->get_transform();
  324. const Transform2D &transform_B = B->get_transform();
  325. real_t inv_inertia_A = collide_A ? A->get_inv_inertia() : 0.0;
  326. real_t inv_inertia_B = collide_B ? B->get_inv_inertia() : 0.0;
  327. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  328. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  329. for (int i = 0; i < contact_count; i++) {
  330. Contact &c = contacts[i];
  331. c.active = false;
  332. Vector2 global_A = transform_A.basis_xform(c.local_A);
  333. Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
  334. Vector2 axis = global_A - global_B;
  335. real_t depth = axis.dot(c.normal);
  336. if (depth <= 0.0) {
  337. continue;
  338. }
  339. #ifdef DEBUG_ENABLED
  340. if (space->is_debugging_contacts()) {
  341. space->add_debug_contact(global_A + offset_A);
  342. space->add_debug_contact(global_B + offset_A);
  343. }
  344. #endif
  345. c.rA = global_A - A->get_center_of_mass();
  346. c.rB = global_B - B->get_center_of_mass() - offset_B;
  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. Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent;
  362. c.acc_impulse -= P;
  363. if (A->can_report_contacts()) {
  364. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  365. 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(), c.acc_impulse);
  366. }
  367. if (B->can_report_contacts()) {
  368. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  369. 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(), c.acc_impulse);
  370. }
  371. if (report_contacts_only) {
  372. collided = false;
  373. continue;
  374. }
  375. #ifdef ACCUMULATE_IMPULSES
  376. {
  377. // Apply normal + friction impulse
  378. if (collide_A) {
  379. A->apply_impulse(-P, c.rA + A->get_center_of_mass());
  380. }
  381. if (collide_B) {
  382. B->apply_impulse(P, c.rB + B->get_center_of_mass());
  383. }
  384. }
  385. #endif
  386. c.bounce = combine_bounce(A, B);
  387. if (c.bounce) {
  388. Vector2 crA(-A->get_prev_angular_velocity() * c.rA.y, A->get_prev_angular_velocity() * c.rA.x);
  389. Vector2 crB(-B->get_prev_angular_velocity() * c.rB.y, B->get_prev_angular_velocity() * c.rB.x);
  390. Vector2 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;
  391. c.bounce = c.bounce * dv.dot(c.normal);
  392. }
  393. c.active = true;
  394. do_process = true;
  395. }
  396. return do_process;
  397. }
  398. void GodotBodyPair2D::solve(real_t p_step) {
  399. if (!collided || oneway_disabled) {
  400. return;
  401. }
  402. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  403. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  404. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  405. for (int i = 0; i < contact_count; ++i) {
  406. Contact &c = contacts[i];
  407. if (!c.active) {
  408. continue;
  409. }
  410. // Relative velocity at contact
  411. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  412. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  413. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  414. Vector2 crbA(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  415. Vector2 crbB(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  416. Vector2 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  417. real_t vn = dv.dot(c.normal);
  418. real_t vbn = dbv.dot(c.normal);
  419. Vector2 tangent = c.normal.orthogonal();
  420. real_t vt = dv.dot(tangent);
  421. real_t jbn = (c.bias - vbn) * c.mass_normal;
  422. real_t jbnOld = c.acc_bias_impulse;
  423. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  424. Vector2 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  425. if (collide_A) {
  426. A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);
  427. }
  428. if (collide_B) {
  429. B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);
  430. }
  431. crbA = Vector2(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  432. crbB = Vector2(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  433. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  434. vbn = dbv.dot(c.normal);
  435. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  436. real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);
  437. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  438. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  439. Vector2 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  440. if (collide_A) {
  441. A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);
  442. }
  443. if (collide_B) {
  444. B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);
  445. }
  446. }
  447. real_t jn = -(c.bounce + vn) * c.mass_normal;
  448. real_t jnOld = c.acc_normal_impulse;
  449. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  450. real_t friction = combine_friction(A, B);
  451. real_t jtMax = friction * c.acc_normal_impulse;
  452. real_t jt = -vt * c.mass_tangent;
  453. real_t jtOld = c.acc_tangent_impulse;
  454. c.acc_tangent_impulse = CLAMP(jtOld + jt, -jtMax, jtMax);
  455. Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld);
  456. if (collide_A) {
  457. A->apply_impulse(-j, c.rA + A->get_center_of_mass());
  458. }
  459. if (collide_B) {
  460. B->apply_impulse(j, c.rB + B->get_center_of_mass());
  461. }
  462. c.acc_impulse -= j;
  463. }
  464. }
  465. GodotBodyPair2D::GodotBodyPair2D(GodotBody2D *p_A, int p_shape_A, GodotBody2D *p_B, int p_shape_B) :
  466. GodotConstraint2D(_arr, 2) {
  467. A = p_A;
  468. B = p_B;
  469. shape_A = p_shape_A;
  470. shape_B = p_shape_B;
  471. space = A->get_space();
  472. A->add_constraint(this, 0);
  473. B->add_constraint(this, 1);
  474. }
  475. GodotBodyPair2D::~GodotBodyPair2D() {
  476. A->remove_constraint(this, 0);
  477. B->remove_constraint(this, 1);
  478. }