body_pair_2d_sw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "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. Vector2 mnormal = motion / mlen;
  132. real_t min, max;
  133. p_A->get_shape(p_shape_A)->project_rangev(mnormal, p_xform_A, min, max);
  134. bool fast_object = mlen > (max - min) * 0.3; //going too fast in that direction
  135. 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
  136. return false;
  137. }
  138. //cast a segment from support in motion normal, in the same direction of motion by motion length
  139. //support is the worst case collision point, so real collision happened before
  140. int a;
  141. Vector2 s[2];
  142. p_A->get_shape(p_shape_A)->get_supports(p_xform_A.basis_xform(mnormal).normalized(), s, a);
  143. Vector2 from = p_xform_A.xform(s[0]);
  144. Vector2 to = from + motion;
  145. Transform2D from_inv = p_xform_B.affine_inverse();
  146. Vector2 local_from = from_inv.xform(from - mnormal * mlen * 0.1); //start from a little inside the bounding box
  147. Vector2 local_to = from_inv.xform(to);
  148. Vector2 rpos, rnorm;
  149. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm))
  150. return false;
  151. //ray hit something
  152. Vector2 hitpos = p_xform_B.xform(rpos);
  153. Vector2 contact_A = to;
  154. Vector2 contact_B = hitpos;
  155. //create a contact
  156. if (p_swap_result)
  157. _contact_added_callback(contact_B, contact_A);
  158. else
  159. _contact_added_callback(contact_A, contact_B);
  160. return true;
  161. }
  162. real_t combine_bounce(Body2DSW *A, Body2DSW *B) {
  163. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  164. }
  165. real_t combine_friction(Body2DSW *A, Body2DSW *B) {
  166. return ABS(MIN(A->get_friction(), B->get_friction()));
  167. }
  168. bool BodyPair2DSW::setup(real_t p_step) {
  169. //cannot collide
  170. if (!A->test_collision_mask(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self()) || (A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC && B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC && A->get_max_contacts_reported() == 0 && B->get_max_contacts_reported() == 0)) {
  171. collided = false;
  172. return false;
  173. }
  174. if (A->is_shape_set_as_disabled(shape_A) || B->is_shape_set_as_disabled(shape_B)) {
  175. collided = false;
  176. return false;
  177. }
  178. //use local A coordinates to avoid numerical issues on collision detection
  179. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  180. _validate_contacts();
  181. Vector2 offset_A = A->get_transform().get_origin();
  182. Transform2D xform_Au = A->get_transform().untranslated();
  183. Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
  184. Transform2D xform_Bu = B->get_transform();
  185. xform_Bu.elements[2] -= A->get_transform().get_origin();
  186. Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  187. Shape2DSW *shape_A_ptr = A->get_shape(shape_A);
  188. Shape2DSW *shape_B_ptr = B->get_shape(shape_B);
  189. Vector2 motion_A, motion_B;
  190. if (A->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_SHAPE) {
  191. motion_A = A->get_motion();
  192. }
  193. if (B->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_SHAPE) {
  194. motion_B = B->get_motion();
  195. }
  196. //faster to set than to check..
  197. //bool prev_collided=collided;
  198. collided = CollisionSolver2DSW::solve(shape_A_ptr, xform_A, motion_A, shape_B_ptr, xform_B, motion_B, _add_contact, this, &sep_axis);
  199. if (!collided) {
  200. //test ccd (currently just a raycast)
  201. if (A->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_RAY && A->get_mode() > Physics2DServer::BODY_MODE_KINEMATIC) {
  202. if (_test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B))
  203. collided = true;
  204. }
  205. if (B->get_continuous_collision_detection_mode() == Physics2DServer::CCD_MODE_CAST_RAY && B->get_mode() > Physics2DServer::BODY_MODE_KINEMATIC) {
  206. if (_test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A, true))
  207. collided = true;
  208. }
  209. if (!collided) {
  210. oneway_disabled = false;
  211. return false;
  212. }
  213. }
  214. if (oneway_disabled)
  215. return false;
  216. //if (!prev_collided) {
  217. {
  218. if (A->is_shape_set_as_one_way_collision(shape_A)) {
  219. Vector2 direction = xform_A.get_axis(1).normalized();
  220. bool valid = false;
  221. if (B->get_linear_velocity().dot(direction) >= 0) {
  222. for (int i = 0; i < contact_count; i++) {
  223. Contact &c = contacts[i];
  224. if (!c.reused)
  225. continue;
  226. if (c.normal.dot(direction) < 0)
  227. continue;
  228. valid = true;
  229. break;
  230. }
  231. }
  232. if (!valid) {
  233. collided = false;
  234. oneway_disabled = true;
  235. return false;
  236. }
  237. }
  238. if (B->is_shape_set_as_one_way_collision(shape_B)) {
  239. Vector2 direction = xform_B.get_axis(1).normalized();
  240. bool valid = false;
  241. if (A->get_linear_velocity().dot(direction) >= 0) {
  242. for (int i = 0; i < contact_count; i++) {
  243. Contact &c = contacts[i];
  244. if (!c.reused)
  245. continue;
  246. if (c.normal.dot(direction) < 0)
  247. continue;
  248. valid = true;
  249. break;
  250. }
  251. }
  252. if (!valid) {
  253. collided = false;
  254. oneway_disabled = true;
  255. return false;
  256. }
  257. }
  258. }
  259. real_t max_penetration = space->get_contact_max_allowed_penetration();
  260. real_t bias = 0.3;
  261. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  262. if (shape_A_ptr->get_custom_bias() == 0)
  263. bias = shape_B_ptr->get_custom_bias();
  264. else if (shape_B_ptr->get_custom_bias() == 0)
  265. bias = shape_A_ptr->get_custom_bias();
  266. else
  267. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  268. }
  269. cc = 0;
  270. real_t inv_dt = 1.0 / p_step;
  271. bool do_process = false;
  272. for (int i = 0; i < contact_count; i++) {
  273. Contact &c = contacts[i];
  274. Vector2 global_A = xform_Au.xform(c.local_A);
  275. Vector2 global_B = xform_Bu.xform(c.local_B);
  276. real_t depth = c.normal.dot(global_A - global_B);
  277. if (depth <= 0 || !c.reused) {
  278. c.active = false;
  279. continue;
  280. }
  281. c.active = true;
  282. #ifdef DEBUG_ENABLED
  283. if (space->is_debugging_contacts()) {
  284. space->add_debug_contact(global_A + offset_A);
  285. space->add_debug_contact(global_B + offset_A);
  286. }
  287. #endif
  288. int gather_A = A->can_report_contacts();
  289. int gather_B = B->can_report_contacts();
  290. c.rA = global_A;
  291. c.rB = global_B - offset_B;
  292. if (gather_A | gather_B) {
  293. //Vector2 crB( -B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x );
  294. global_A += offset_A;
  295. global_B += offset_A;
  296. if (gather_A) {
  297. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  298. A->add_contact(global_A, -c.normal, depth, shape_A, global_B, shape_B, B->get_instance_id(), B->get_self(), crB + B->get_linear_velocity());
  299. }
  300. if (gather_B) {
  301. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  302. B->add_contact(global_B, c.normal, depth, shape_B, global_A, shape_A, A->get_instance_id(), A->get_self(), crA + A->get_linear_velocity());
  303. }
  304. }
  305. if ((A->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC && B->get_mode() <= Physics2DServer::BODY_MODE_KINEMATIC)) {
  306. c.active = false;
  307. collided = false;
  308. continue;
  309. }
  310. // Precompute normal mass, tangent mass, and bias.
  311. real_t rnA = c.rA.dot(c.normal);
  312. real_t rnB = c.rB.dot(c.normal);
  313. real_t kNormal = A->get_inv_mass() + B->get_inv_mass();
  314. kNormal += A->get_inv_inertia() * (c.rA.dot(c.rA) - rnA * rnA) + B->get_inv_inertia() * (c.rB.dot(c.rB) - rnB * rnB);
  315. c.mass_normal = 1.0f / kNormal;
  316. Vector2 tangent = c.normal.tangent();
  317. real_t rtA = c.rA.dot(tangent);
  318. real_t rtB = c.rB.dot(tangent);
  319. real_t kTangent = A->get_inv_mass() + B->get_inv_mass();
  320. kTangent += A->get_inv_inertia() * (c.rA.dot(c.rA) - rtA * rtA) + B->get_inv_inertia() * (c.rB.dot(c.rB) - rtB * rtB);
  321. c.mass_tangent = 1.0f / kTangent;
  322. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  323. c.depth = depth;
  324. //c.acc_bias_impulse=0;
  325. #ifdef ACCUMULATE_IMPULSES
  326. {
  327. // Apply normal + friction impulse
  328. Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent;
  329. A->apply_impulse(c.rA, -P);
  330. B->apply_impulse(c.rB, P);
  331. }
  332. #endif
  333. c.bounce = combine_bounce(A, B);
  334. if (c.bounce) {
  335. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  336. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  337. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  338. c.bounce = c.bounce * dv.dot(c.normal);
  339. }
  340. do_process = true;
  341. }
  342. return do_process;
  343. }
  344. void BodyPair2DSW::solve(real_t p_step) {
  345. if (!collided)
  346. return;
  347. for (int i = 0; i < contact_count; ++i) {
  348. Contact &c = contacts[i];
  349. cc++;
  350. if (!c.active)
  351. continue;
  352. // Relative velocity at contact
  353. Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
  354. Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
  355. Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  356. Vector2 crbA(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
  357. Vector2 crbB(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
  358. Vector2 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  359. real_t vn = dv.dot(c.normal);
  360. real_t vbn = dbv.dot(c.normal);
  361. Vector2 tangent = c.normal.tangent();
  362. real_t vt = dv.dot(tangent);
  363. real_t jbn = (c.bias - vbn) * c.mass_normal;
  364. real_t jbnOld = c.acc_bias_impulse;
  365. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  366. Vector2 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  367. A->apply_bias_impulse(c.rA, -jb);
  368. B->apply_bias_impulse(c.rB, jb);
  369. real_t jn = -(c.bounce + vn) * c.mass_normal;
  370. real_t jnOld = c.acc_normal_impulse;
  371. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  372. real_t friction = combine_friction(A, B);
  373. real_t jtMax = friction * c.acc_normal_impulse;
  374. real_t jt = -vt * c.mass_tangent;
  375. real_t jtOld = c.acc_tangent_impulse;
  376. c.acc_tangent_impulse = CLAMP(jtOld + jt, -jtMax, jtMax);
  377. Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld);
  378. A->apply_impulse(c.rA, -j);
  379. B->apply_impulse(c.rB, j);
  380. }
  381. }
  382. BodyPair2DSW::BodyPair2DSW(Body2DSW *p_A, int p_shape_A, Body2DSW *p_B, int p_shape_B) :
  383. Constraint2DSW(_arr, 2) {
  384. A = p_A;
  385. B = p_B;
  386. shape_A = p_shape_A;
  387. shape_B = p_shape_B;
  388. space = A->get_space();
  389. A->add_constraint(this, 0);
  390. B->add_constraint(this, 1);
  391. contact_count = 0;
  392. collided = false;
  393. oneway_disabled = false;
  394. }
  395. BodyPair2DSW::~BodyPair2DSW() {
  396. A->remove_constraint(this);
  397. B->remove_constraint(this);
  398. }