body_pair_2d_sw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**************************************************************************/
  2. /* body_pair_2d_sw.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 "body_pair_2d_sw.h"
  31. #include "collision_solver_2d_sw.h"
  32. #include "space_2d_sw.h"
  33. #define POSITION_CORRECTION
  34. #define ACCUMULATE_IMPULSES
  35. void BodyPair2DSW::_add_contact(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_self) {
  36. BodyPair2DSW *self = (BodyPair2DSW *)p_self;
  37. self->_contact_added_callback(p_point_A, p_point_B);
  38. }
  39. void BodyPair2DSW::_contact_added_callback(const Vector2 &p_point_A, const Vector2 &p_point_B) {
  40. // check if we already have the contact
  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.acc_normal_impulse = 0;
  47. contact.acc_bias_impulse = 0;
  48. contact.acc_tangent_impulse = 0;
  49. contact.local_A = local_A;
  50. contact.local_B = local_B;
  51. contact.reused = true;
  52. contact.normal = (p_point_A - p_point_B).normalized();
  53. contact.mass_normal = 0; // will be computed in setup()
  54. // attempt to determine if the contact will be reused
  55. real_t recycle_radius_2 = space->get_contact_recycle_radius() * space->get_contact_recycle_radius();
  56. for (int i = 0; i < contact_count; i++) {
  57. Contact &c = contacts[i];
  58. if (
  59. c.local_A.distance_squared_to(local_A) < (recycle_radius_2) &&
  60. c.local_B.distance_squared_to(local_B) < (recycle_radius_2)) {
  61. contact.acc_normal_impulse = c.acc_normal_impulse;
  62. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  63. contact.acc_bias_impulse = c.acc_bias_impulse;
  64. new_index = i;
  65. break;
  66. }
  67. }
  68. // figure out if the contact amount must be reduced to fit the new contact
  69. if (new_index == MAX_CONTACTS) {
  70. // remove the contact with the minimum depth
  71. int least_deep = -1;
  72. real_t min_depth = 1e10;
  73. for (int i = 0; i <= contact_count; i++) {
  74. Contact &c = (i == contact_count) ? contact : contacts[i];
  75. Vector2 global_A = A->get_transform().basis_xform(c.local_A);
  76. Vector2 global_B = B->get_transform().basis_xform(c.local_B) + offset_B;
  77. Vector2 axis = global_A - global_B;
  78. real_t depth = axis.dot(c.normal);
  79. if (depth < min_depth) {
  80. min_depth = depth;
  81. least_deep = i;
  82. }
  83. }
  84. ERR_FAIL_COND(least_deep == -1);
  85. if (least_deep < contact_count) { //replace the last deep contact by the new one
  86. contacts[least_deep] = contact;
  87. }
  88. return;
  89. }
  90. contacts[new_index] = contact;
  91. if (new_index == contact_count) {
  92. contact_count++;
  93. }
  94. }
  95. void BodyPair2DSW::_validate_contacts() {
  96. //make sure to erase contacts that are no longer valid
  97. real_t max_separation = space->get_contact_max_separation();
  98. real_t max_separation2 = max_separation * max_separation;
  99. for (int i = 0; i < contact_count; i++) {
  100. Contact &c = contacts[i];
  101. bool erase = false;
  102. if (!c.reused) {
  103. //was left behind in previous frame
  104. erase = true;
  105. } else {
  106. c.reused = false;
  107. Vector2 global_A = A->get_transform().basis_xform(c.local_A);
  108. Vector2 global_B = B->get_transform().basis_xform(c.local_B) + offset_B;
  109. Vector2 axis = global_A - global_B;
  110. real_t depth = axis.dot(c.normal);
  111. if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {
  112. erase = true;
  113. }
  114. }
  115. if (erase) {
  116. // contact no longer needed, remove
  117. if ((i + 1) < contact_count) {
  118. // swap with the last one
  119. SWAP(contacts[i], contacts[contact_count - 1]);
  120. }
  121. i--;
  122. contact_count--;
  123. }
  124. }
  125. }
  126. bool BodyPair2DSW::_test_ccd(real_t p_step, Body2DSW *p_A, int p_shape_A, const Transform2D &p_xform_A, Body2DSW *p_B, int p_shape_B, const Transform2D &p_xform_B, bool p_swap_result) {
  127. Vector2 motion = p_A->get_linear_velocity() * p_step;
  128. real_t mlen = motion.length();
  129. if (mlen < CMP_EPSILON) {
  130. return false;
  131. }
  132. Vector2 mnormal = motion / mlen;
  133. real_t min, max;
  134. p_A->get_shape(p_shape_A)->project_rangev(mnormal, p_xform_A, min, max);
  135. bool fast_object = mlen > (max - min) * 0.3; //going too fast in that direction
  136. if (!fast_object) { //did it move enough in this direction to even attempt raycast? let's say it should move more than 1/3 the size of the object in that axis
  137. return false;
  138. }
  139. //cast a segment from support in motion normal, in the same direction of motion by motion length
  140. //support is the worst case collision point, so real collision happened before
  141. int a;
  142. Vector2 s[2];
  143. p_A->get_shape(p_shape_A)->get_supports(p_xform_A.basis_xform(mnormal).normalized(), s, a);
  144. Vector2 from = p_xform_A.xform(s[0]);
  145. Vector2 to = from + motion;
  146. Transform2D from_inv = p_xform_B.affine_inverse();
  147. Vector2 local_from = from_inv.xform(from - mnormal * mlen * 0.1); //start from a little inside the bounding box
  148. Vector2 local_to = from_inv.xform(to);
  149. Vector2 rpos, rnorm;
  150. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  151. return false;
  152. }
  153. //ray hit something
  154. Vector2 hitpos = p_xform_B.xform(rpos);
  155. Vector2 contact_A = to;
  156. Vector2 contact_B = hitpos;
  157. //create a contact
  158. if (p_swap_result) {
  159. _contact_added_callback(contact_B, contact_A);
  160. } else {
  161. _contact_added_callback(contact_A, contact_B);
  162. }
  163. return true;
  164. }
  165. real_t combine_bounce(Body2DSW *A, Body2DSW *B) {
  166. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  167. }
  168. real_t combine_friction(Body2DSW *A, Body2DSW *B) {
  169. return ABS(MIN(A->get_friction(), B->get_friction()));
  170. }
  171. bool BodyPair2DSW::setup(real_t p_step) {
  172. //cannot collide
  173. if (!A->test_collision_mask(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
  174. collided = false;
  175. return false;
  176. }
  177. bool report_contacts_only = false;
  178. if ((A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC) && (B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC)) {
  179. if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
  180. report_contacts_only = true;
  181. } else {
  182. collided = false;
  183. return false;
  184. }
  185. }
  186. //use local A coordinates to avoid numerical issues on collision detection
  187. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  188. _validate_contacts();
  189. Vector2 offset_A = A->get_transform().get_origin();
  190. Transform2D xform_Au = A->get_transform().untranslated();
  191. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  192. Transform2D xform_Bu = B->get_transform();
  193. xform_Bu.elements[2] -= A->get_transform().get_origin();
  194. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  195. Shape2DSW *shape_A_ptr = A->get_shape(shape_A);
  196. Shape2DSW *shape_B_ptr = B->get_shape(shape_B);
  197. Vector2 motion_A, motion_B;
  198. if (A->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_SHAPE) {
  199. motion_A = A->get_motion();
  200. }
  201. if (B->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_SHAPE) {
  202. motion_B = B->get_motion();
  203. }
  204. bool prev_collided = collided;
  205. collided = CollisionSolver2DSW::solve(shape_A_ptr, xform_A, motion_A, shape_B_ptr, xform_B, motion_B, _add_contact, this, &sep_axis);
  206. if (!collided) {
  207. //test ccd (currently just a raycast)
  208. if (A->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_RAY && A->get_mode() > Physics2DServer::BODY_MODE_KINEMATIC) {
  209. if (_test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B)) {
  210. collided = true;
  211. }
  212. }
  213. if (B->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_RAY && B->get_mode() > Physics2DServer::BODY_MODE_KINEMATIC) {
  214. if (_test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A, true)) {
  215. collided = true;
  216. }
  217. }
  218. if (!collided) {
  219. oneway_disabled = false;
  220. return false;
  221. }
  222. }
  223. if (oneway_disabled) {
  224. return false;
  225. }
  226. if (!prev_collided) {
  227. if (A->is_shape_set_as_one_way_collision(shape_A)) {
  228. Vector2 direction = xform_A.get_axis(1).normalized();
  229. bool valid = false;
  230. for (int i = 0; i < contact_count; i++) {
  231. Contact &c = contacts[i];
  232. if (!c.reused) {
  233. continue;
  234. }
  235. if (c.normal.dot(direction) > -CMP_EPSILON) { //greater (normal inverted)
  236. continue;
  237. }
  238. valid = true;
  239. break;
  240. }
  241. if (!valid) {
  242. collided = false;
  243. oneway_disabled = true;
  244. return false;
  245. }
  246. }
  247. if (B->is_shape_set_as_one_way_collision(shape_B)) {
  248. Vector2 direction = xform_B.get_axis(1).normalized();
  249. bool valid = false;
  250. for (int i = 0; i < contact_count; i++) {
  251. Contact &c = contacts[i];
  252. if (!c.reused) {
  253. continue;
  254. }
  255. if (c.normal.dot(direction) < CMP_EPSILON) { //less (normal ok)
  256. continue;
  257. }
  258. valid = true;
  259. break;
  260. }
  261. if (!valid) {
  262. collided = false;
  263. oneway_disabled = true;
  264. return false;
  265. }
  266. }
  267. }
  268. real_t max_penetration = space->get_contact_max_allowed_penetration();
  269. real_t bias = 0.3;
  270. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  271. if (shape_A_ptr->get_custom_bias() == 0) {
  272. bias = shape_B_ptr->get_custom_bias();
  273. } else if (shape_B_ptr->get_custom_bias() == 0) {
  274. bias = shape_A_ptr->get_custom_bias();
  275. } else {
  276. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  277. }
  278. }
  279. cc = 0;
  280. real_t inv_dt = 1.0 / p_step;
  281. bool do_process = false;
  282. for (int i = 0; i < contact_count; i++) {
  283. Contact &c = contacts[i];
  284. c.active = false;
  285. Vector2 global_A = xform_Au.xform(c.local_A);
  286. Vector2 global_B = xform_Bu.xform(c.local_B);
  287. real_t depth = c.normal.dot(global_A - global_B);
  288. if (depth <= 0 || !c.reused) {
  289. continue;
  290. }
  291. #ifdef DEBUG_ENABLED
  292. if (space->is_debugging_contacts()) {
  293. space->add_debug_contact(global_A + offset_A);
  294. space->add_debug_contact(global_B + offset_A);
  295. }
  296. #endif
  297. c.rA = global_A;
  298. c.rB = global_B - offset_B;
  299. if (A->can_report_contacts()) {
  300. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  301. 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());
  302. }
  303. if (B->can_report_contacts()) {
  304. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  305. 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());
  306. }
  307. if (report_contacts_only) {
  308. collided = false;
  309. continue;
  310. }
  311. c.active = true;
  312. // Precompute normal mass, tangent mass, and bias.
  313. real_t rnA = c.rA.dot(c.normal);
  314. real_t rnB = c.rB.dot(c.normal);
  315. real_t kNormal = A->get_inv_mass() + B->get_inv_mass();
  316. kNormal += A->get_inv_inertia() * (c.rA.dot(c.rA) - rnA * rnA) + B->get_inv_inertia() * (c.rB.dot(c.rB) - rnB * rnB);
  317. c.mass_normal = 1.0f / kNormal;
  318. Vector2 tangent = c.normal.tangent();
  319. real_t rtA = c.rA.dot(tangent);
  320. real_t rtB = c.rB.dot(tangent);
  321. real_t kTangent = A->get_inv_mass() + B->get_inv_mass();
  322. kTangent += A->get_inv_inertia() * (c.rA.dot(c.rA) - rtA * rtA) + B->get_inv_inertia() * (c.rB.dot(c.rB) - rtB * rtB);
  323. c.mass_tangent = 1.0f / kTangent;
  324. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  325. c.depth = depth;
  326. //c.acc_bias_impulse=0;
  327. #ifdef ACCUMULATE_IMPULSES
  328. {
  329. // Apply normal + friction impulse
  330. Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent;
  331. A->apply_impulse(c.rA, -P);
  332. B->apply_impulse(c.rB, P);
  333. }
  334. #endif
  335. c.bounce = combine_bounce(A, B);
  336. if (c.bounce) {
  337. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  338. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  339. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  340. c.bounce = c.bounce * dv.dot(c.normal);
  341. }
  342. do_process = true;
  343. }
  344. return do_process;
  345. }
  346. void BodyPair2DSW::solve(real_t p_step) {
  347. if (!collided) {
  348. return;
  349. }
  350. for (int i = 0; i < contact_count; ++i) {
  351. Contact &c = contacts[i];
  352. cc++;
  353. if (!c.active) {
  354. continue;
  355. }
  356. // Relative velocity at contact
  357. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  358. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  359. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  360. Vector2 crbA(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  361. Vector2 crbB(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  362. Vector2 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  363. real_t vn = dv.dot(c.normal);
  364. real_t vbn = dbv.dot(c.normal);
  365. Vector2 tangent = c.normal.tangent();
  366. real_t vt = dv.dot(tangent);
  367. real_t jbn = (c.bias - vbn) * c.mass_normal;
  368. real_t jbnOld = c.acc_bias_impulse;
  369. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  370. Vector2 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  371. A->apply_bias_impulse(c.rA, -jb);
  372. B->apply_bias_impulse(c.rB, jb);
  373. real_t jn = -(c.bounce + vn) * c.mass_normal;
  374. real_t jnOld = c.acc_normal_impulse;
  375. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  376. real_t friction = combine_friction(A, B);
  377. real_t jtMax = friction * c.acc_normal_impulse;
  378. real_t jt = -vt * c.mass_tangent;
  379. real_t jtOld = c.acc_tangent_impulse;
  380. c.acc_tangent_impulse = CLAMP(jtOld + jt, -jtMax, jtMax);
  381. Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld);
  382. A->apply_impulse(c.rA, -j);
  383. B->apply_impulse(c.rB, j);
  384. }
  385. }
  386. BodyPair2DSW::BodyPair2DSW(Body2DSW *p_A, int p_shape_A, Body2DSW *p_B, int p_shape_B) :
  387. Constraint2DSW(_arr, 2) {
  388. A = p_A;
  389. B = p_B;
  390. shape_A = p_shape_A;
  391. shape_B = p_shape_B;
  392. space = A->get_space();
  393. A->add_constraint(this, 0);
  394. B->add_constraint(this, 1);
  395. contact_count = 0;
  396. collided = false;
  397. oneway_disabled = false;
  398. }
  399. BodyPair2DSW::~BodyPair2DSW() {
  400. A->remove_constraint(this);
  401. B->remove_constraint(this);
  402. }