godot_body_pair_2d.cpp 21 KB

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