2
0

body_pair_sw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*************************************************************************/
  2. /* body_pair_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_sw.h"
  31. #include "collision_solver_sw.h"
  32. #include "core/os/os.h"
  33. #include "space_sw.h"
  34. /*
  35. #define NO_ACCUMULATE_IMPULSES
  36. #define NO_SPLIT_IMPULSES
  37. #define NO_FRICTION
  38. */
  39. #define NO_TANGENTIALS
  40. /* BODY PAIR */
  41. //#define ALLOWED_PENETRATION 0.01
  42. #define RELAXATION_TIMESTEPS 3
  43. #define MIN_VELOCITY 0.0001
  44. #define MAX_BIAS_ROTATION (Math_PI / 8)
  45. void BodyPairSW::_contact_added_callback(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  46. BodyPairSW *pair = (BodyPairSW *)p_userdata;
  47. pair->contact_added_callback(p_point_A, p_point_B);
  48. }
  49. void BodyPairSW::contact_added_callback(const Vector3 &p_point_A, const Vector3 &p_point_B) {
  50. // check if we already have the contact
  51. //Vector3 local_A = A->get_inv_transform().xform(p_point_A);
  52. //Vector3 local_B = B->get_inv_transform().xform(p_point_B);
  53. Vector3 local_A = A->get_inv_transform().basis.xform(p_point_A);
  54. Vector3 local_B = B->get_inv_transform().basis.xform(p_point_B - offset_B);
  55. int new_index = contact_count;
  56. ERR_FAIL_COND(new_index >= (MAX_CONTACTS + 1));
  57. Contact contact;
  58. contact.acc_normal_impulse = 0;
  59. contact.acc_bias_impulse = 0;
  60. contact.acc_bias_impulse_center_of_mass = 0;
  61. contact.acc_tangent_impulse = Vector3();
  62. contact.local_A = local_A;
  63. contact.local_B = local_B;
  64. contact.normal = (p_point_A - p_point_B).normalized();
  65. contact.mass_normal = 0; // will be computed in setup()
  66. // attempt to determine if the contact will be reused
  67. real_t contact_recycle_radius = space->get_contact_recycle_radius();
  68. for (int i = 0; i < contact_count; i++) {
  69. Contact &c = contacts[i];
  70. if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&
  71. c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {
  72. contact.acc_normal_impulse = c.acc_normal_impulse;
  73. contact.acc_bias_impulse = c.acc_bias_impulse;
  74. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  75. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  76. new_index = i;
  77. break;
  78. }
  79. }
  80. // figure out if the contact amount must be reduced to fit the new contact
  81. if (new_index == MAX_CONTACTS) {
  82. // remove the contact with the minimum depth
  83. int least_deep = -1;
  84. real_t min_depth = 1e10;
  85. for (int i = 0; i <= contact_count; i++) {
  86. Contact &c = (i == contact_count) ? contact : contacts[i];
  87. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  88. Vector3 global_B = B->get_transform().basis.xform(c.local_B) + offset_B;
  89. Vector3 axis = global_A - global_B;
  90. real_t depth = axis.dot(c.normal);
  91. if (depth < min_depth) {
  92. min_depth = depth;
  93. least_deep = i;
  94. }
  95. }
  96. ERR_FAIL_COND(least_deep == -1);
  97. if (least_deep < contact_count) { //replace the last deep contact by the new one
  98. contacts[least_deep] = contact;
  99. }
  100. return;
  101. }
  102. contacts[new_index] = contact;
  103. if (new_index == contact_count) {
  104. contact_count++;
  105. }
  106. }
  107. void BodyPairSW::validate_contacts() {
  108. //make sure to erase contacts that are no longer valid
  109. real_t contact_max_separation = space->get_contact_max_separation();
  110. for (int i = 0; i < contact_count; i++) {
  111. Contact &c = contacts[i];
  112. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  113. Vector3 global_B = B->get_transform().basis.xform(c.local_B) + offset_B;
  114. Vector3 axis = global_A - global_B;
  115. real_t depth = axis.dot(c.normal);
  116. if (depth < -contact_max_separation || (global_B + c.normal * depth - global_A).length() > contact_max_separation) {
  117. // contact no longer needed, remove
  118. if ((i + 1) < contact_count) {
  119. // swap with the last one
  120. SWAP(contacts[i], contacts[contact_count - 1]);
  121. }
  122. i--;
  123. contact_count--;
  124. }
  125. }
  126. }
  127. bool BodyPairSW::_test_ccd(real_t p_step, BodySW *p_A, int p_shape_A, const Transform &p_xform_A, BodySW *p_B, int p_shape_B, const Transform &p_xform_B) {
  128. Vector3 motion = p_A->get_linear_velocity() * p_step;
  129. real_t mlen = motion.length();
  130. if (mlen < CMP_EPSILON) {
  131. return false;
  132. }
  133. Vector3 mnormal = motion / mlen;
  134. real_t min, max;
  135. p_A->get_shape(p_shape_A)->project_range(mnormal, p_xform_A, min, max);
  136. bool fast_object = mlen > (max - min) * 0.3; //going too fast in that direction
  137. 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
  138. return false;
  139. }
  140. //cast a segment from support in motion normal, in the same direction of motion by motion length
  141. //support is the worst case collision point, so real collision happened before
  142. Vector3 s = p_A->get_shape(p_shape_A)->get_support(p_xform_A.basis.xform(mnormal).normalized());
  143. Vector3 from = p_xform_A.xform(s);
  144. Vector3 to = from + motion;
  145. Transform from_inv = p_xform_B.affine_inverse();
  146. Vector3 local_from = from_inv.xform(from - mnormal * mlen * 0.1); //start from a little inside the bounding box
  147. Vector3 local_to = from_inv.xform(to);
  148. Vector3 rpos, rnorm;
  149. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  150. return false;
  151. }
  152. //shorten the linear velocity so it does not hit, but gets close enough, next frame will hit softly or soft enough
  153. Vector3 hitpos = p_xform_B.xform(rpos);
  154. real_t newlen = hitpos.distance_to(from) - (max - min) * 0.01;
  155. p_A->set_linear_velocity((mnormal * newlen) / p_step);
  156. return true;
  157. }
  158. real_t combine_bounce(BodySW *A, BodySW *B) {
  159. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  160. }
  161. real_t combine_friction(BodySW *A, BodySW *B) {
  162. return ABS(MIN(A->get_friction(), B->get_friction()));
  163. }
  164. bool BodyPairSW::setup(real_t p_step) {
  165. //cannot collide
  166. if (!A->test_collision_mask(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self()) || (A->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC && B->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC && A->get_max_contacts_reported() == 0 && B->get_max_contacts_reported() == 0)) {
  167. collided = false;
  168. return false;
  169. }
  170. if (A->is_shape_set_as_disabled(shape_A) || B->is_shape_set_as_disabled(shape_B)) {
  171. collided = false;
  172. return false;
  173. }
  174. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  175. validate_contacts();
  176. Vector3 offset_A = A->get_transform().get_origin();
  177. Transform xform_Au = Transform(A->get_transform().basis, Vector3());
  178. Transform xform_A = xform_Au * A->get_shape_transform(shape_A);
  179. Transform xform_Bu = B->get_transform();
  180. xform_Bu.origin -= offset_A;
  181. Transform xform_B = xform_Bu * B->get_shape_transform(shape_B);
  182. ShapeSW *shape_A_ptr = A->get_shape(shape_A);
  183. ShapeSW *shape_B_ptr = B->get_shape(shape_B);
  184. bool collided = CollisionSolverSW::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  185. this->collided = collided;
  186. if (!collided) {
  187. //test ccd (currently just a raycast)
  188. if (A->is_continuous_collision_detection_enabled() && A->get_mode() > PhysicsServer::BODY_MODE_KINEMATIC && B->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC) {
  189. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  190. }
  191. if (B->is_continuous_collision_detection_enabled() && B->get_mode() > PhysicsServer::BODY_MODE_KINEMATIC && A->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC) {
  192. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  193. }
  194. return false;
  195. }
  196. real_t max_penetration = space->get_contact_max_allowed_penetration();
  197. real_t bias = (real_t)0.3;
  198. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  199. if (shape_A_ptr->get_custom_bias() == 0) {
  200. bias = shape_B_ptr->get_custom_bias();
  201. } else if (shape_B_ptr->get_custom_bias() == 0) {
  202. bias = shape_A_ptr->get_custom_bias();
  203. } else {
  204. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  205. }
  206. }
  207. real_t inv_dt = 1.0 / p_step;
  208. for (int i = 0; i < contact_count; i++) {
  209. Contact &c = contacts[i];
  210. c.active = false;
  211. Vector3 global_A = xform_Au.xform(c.local_A);
  212. Vector3 global_B = xform_Bu.xform(c.local_B);
  213. real_t depth = c.normal.dot(global_A - global_B);
  214. if (depth <= 0) {
  215. c.active = false;
  216. continue;
  217. }
  218. c.active = true;
  219. #ifdef DEBUG_ENABLED
  220. if (space->is_debugging_contacts()) {
  221. space->add_debug_contact(global_A + offset_A);
  222. space->add_debug_contact(global_B + offset_A);
  223. }
  224. #endif
  225. c.rA = global_A - A->get_center_of_mass();
  226. c.rB = global_B - B->get_center_of_mass() - offset_B;
  227. // contact query reporting...
  228. if (A->can_report_contacts()) {
  229. Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity();
  230. A->add_contact(global_A, -c.normal, depth, shape_A, global_B, shape_B, B->get_instance_id(), B->get_self(), crA);
  231. }
  232. if (B->can_report_contacts()) {
  233. Vector3 crB = B->get_angular_velocity().cross(c.rB) + B->get_linear_velocity();
  234. B->add_contact(global_B, c.normal, depth, shape_B, global_A, shape_A, A->get_instance_id(), A->get_self(), crB);
  235. }
  236. c.active = true;
  237. // Precompute normal mass, tangent mass, and bias.
  238. Vector3 inertia_A = A->get_inv_inertia_tensor().xform(c.rA.cross(c.normal));
  239. Vector3 inertia_B = B->get_inv_inertia_tensor().xform(c.rB.cross(c.normal));
  240. real_t kNormal = A->get_inv_mass() + B->get_inv_mass();
  241. kNormal += c.normal.dot(inertia_A.cross(c.rA)) + c.normal.dot(inertia_B.cross(c.rB));
  242. c.mass_normal = 1.0f / kNormal;
  243. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  244. c.depth = depth;
  245. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  246. A->apply_impulse(c.rA + A->get_center_of_mass(), -j_vec);
  247. B->apply_impulse(c.rB + B->get_center_of_mass(), j_vec);
  248. c.acc_bias_impulse = 0;
  249. c.acc_bias_impulse_center_of_mass = 0;
  250. c.bounce = combine_bounce(A, B);
  251. if (c.bounce) {
  252. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  253. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  254. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  255. //normal impule
  256. c.bounce = c.bounce * dv.dot(c.normal);
  257. }
  258. }
  259. return true;
  260. }
  261. void BodyPairSW::solve(real_t p_step) {
  262. if (!collided) {
  263. return;
  264. }
  265. for (int i = 0; i < contact_count; i++) {
  266. Contact &c = contacts[i];
  267. if (!c.active) {
  268. continue;
  269. }
  270. c.active = false; //try to deactivate, will activate itself if still needed
  271. //bias impulse
  272. Vector3 crbA = A->get_biased_angular_velocity().cross(c.rA);
  273. Vector3 crbB = B->get_biased_angular_velocity().cross(c.rB);
  274. Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  275. real_t vbn = dbv.dot(c.normal);
  276. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  277. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  278. real_t jbnOld = c.acc_bias_impulse;
  279. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  280. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  281. A->apply_bias_impulse(c.rA + A->get_center_of_mass(), -jb, MAX_BIAS_ROTATION / p_step);
  282. B->apply_bias_impulse(c.rB + B->get_center_of_mass(), jb, MAX_BIAS_ROTATION / p_step);
  283. crbA = A->get_biased_angular_velocity().cross(c.rA);
  284. crbB = B->get_biased_angular_velocity().cross(c.rB);
  285. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  286. vbn = dbv.dot(c.normal);
  287. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  288. real_t jbn_com = (-vbn + c.bias) / (A->get_inv_mass() + B->get_inv_mass());
  289. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  290. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  291. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  292. A->apply_bias_impulse(A->get_center_of_mass(), -jb_com, 0.0f);
  293. B->apply_bias_impulse(B->get_center_of_mass(), jb_com, 0.0f);
  294. }
  295. c.active = true;
  296. }
  297. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  298. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  299. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  300. //normal impulse
  301. real_t vn = dv.dot(c.normal);
  302. if (Math::abs(vn) > MIN_VELOCITY) {
  303. real_t jn = -(c.bounce + vn) * c.mass_normal;
  304. real_t jnOld = c.acc_normal_impulse;
  305. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  306. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  307. A->apply_impulse(c.rA + A->get_center_of_mass(), -j);
  308. B->apply_impulse(c.rB + B->get_center_of_mass(), j);
  309. c.active = true;
  310. }
  311. //friction impulse
  312. real_t friction = combine_friction(A, B);
  313. Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross(c.rA);
  314. Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross(c.rB);
  315. Vector3 dtv = lvB - lvA;
  316. real_t tn = c.normal.dot(dtv);
  317. // tangential velocity
  318. Vector3 tv = dtv - c.normal * tn;
  319. real_t tvl = tv.length();
  320. if (tvl > MIN_VELOCITY) {
  321. tv /= tvl;
  322. Vector3 temp1 = A->get_inv_inertia_tensor().xform(c.rA.cross(tv));
  323. Vector3 temp2 = B->get_inv_inertia_tensor().xform(c.rB.cross(tv));
  324. real_t t = -tvl /
  325. (A->get_inv_mass() + B->get_inv_mass() + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));
  326. Vector3 jt = t * tv;
  327. Vector3 jtOld = c.acc_tangent_impulse;
  328. c.acc_tangent_impulse += jt;
  329. real_t fi_len = c.acc_tangent_impulse.length();
  330. real_t jtMax = c.acc_normal_impulse * friction;
  331. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  332. c.acc_tangent_impulse *= jtMax / fi_len;
  333. }
  334. jt = c.acc_tangent_impulse - jtOld;
  335. A->apply_impulse(c.rA + A->get_center_of_mass(), -jt);
  336. B->apply_impulse(c.rB + B->get_center_of_mass(), jt);
  337. c.active = true;
  338. }
  339. }
  340. }
  341. BodyPairSW::BodyPairSW(BodySW *p_A, int p_shape_A, BodySW *p_B, int p_shape_B) :
  342. ConstraintSW(_arr, 2) {
  343. A = p_A;
  344. B = p_B;
  345. shape_A = p_shape_A;
  346. shape_B = p_shape_B;
  347. space = A->get_space();
  348. A->add_constraint(this, 0);
  349. B->add_constraint(this, 1);
  350. contact_count = 0;
  351. collided = false;
  352. }
  353. BodyPairSW::~BodyPairSW() {
  354. A->remove_constraint(this);
  355. B->remove_constraint(this);
  356. }