body_pair_sw.cpp 16 KB

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