body_pair_sw.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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-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_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. Vector3 mnormal = motion / mlen;
  133. real_t min, max;
  134. p_A->get_shape(p_shape_A)->project_range(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. Vector3 s = p_A->get_shape(p_shape_A)->get_support(p_xform_A.basis.xform(mnormal).normalized());
  142. Vector3 from = p_xform_A.xform(s);
  143. Vector3 to = from + motion;
  144. Transform from_inv = p_xform_B.affine_inverse();
  145. Vector3 local_from = from_inv.xform(from - mnormal * mlen * 0.1); //start from a little inside the bounding box
  146. Vector3 local_to = from_inv.xform(to);
  147. Vector3 rpos, rnorm;
  148. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  149. return false;
  150. }
  151. //shorten the linear velocity so it does not hit, but gets close enough, next frame will hit softly or soft enough
  152. Vector3 hitpos = p_xform_B.xform(rpos);
  153. real_t newlen = hitpos.distance_to(from) - (max - min) * 0.01;
  154. p_A->set_linear_velocity((mnormal * newlen) / p_step);
  155. return true;
  156. }
  157. real_t combine_bounce(BodySW *A, BodySW *B) {
  158. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  159. }
  160. real_t combine_friction(BodySW *A, BodySW *B) {
  161. return ABS(MIN(A->get_friction(), B->get_friction()));
  162. }
  163. bool BodyPairSW::setup(real_t p_step) {
  164. //cannot collide
  165. 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)) {
  166. collided = false;
  167. return false;
  168. }
  169. if (A->is_shape_set_as_disabled(shape_A) || B->is_shape_set_as_disabled(shape_B)) {
  170. collided = false;
  171. return false;
  172. }
  173. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  174. validate_contacts();
  175. Vector3 offset_A = A->get_transform().get_origin();
  176. Transform xform_Au = Transform(A->get_transform().basis, Vector3());
  177. Transform xform_A = xform_Au * A->get_shape_transform(shape_A);
  178. Transform xform_Bu = B->get_transform();
  179. xform_Bu.origin -= offset_A;
  180. Transform xform_B = xform_Bu * B->get_shape_transform(shape_B);
  181. ShapeSW *shape_A_ptr = A->get_shape(shape_A);
  182. ShapeSW *shape_B_ptr = B->get_shape(shape_B);
  183. bool collided = CollisionSolverSW::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  184. this->collided = collided;
  185. if (!collided) {
  186. //test ccd (currently just a raycast)
  187. if (A->is_continuous_collision_detection_enabled() && A->get_mode() > PhysicsServer::BODY_MODE_KINEMATIC && B->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC) {
  188. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  189. }
  190. if (B->is_continuous_collision_detection_enabled() && B->get_mode() > PhysicsServer::BODY_MODE_KINEMATIC && A->get_mode() <= PhysicsServer::BODY_MODE_KINEMATIC) {
  191. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  192. }
  193. return false;
  194. }
  195. real_t max_penetration = space->get_contact_max_allowed_penetration();
  196. real_t bias = (real_t)0.3;
  197. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  198. if (shape_A_ptr->get_custom_bias() == 0)
  199. bias = shape_B_ptr->get_custom_bias();
  200. else if (shape_B_ptr->get_custom_bias() == 0)
  201. bias = shape_A_ptr->get_custom_bias();
  202. else
  203. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  204. }
  205. real_t inv_dt = 1.0 / p_step;
  206. for (int i = 0; i < contact_count; i++) {
  207. Contact &c = contacts[i];
  208. c.active = false;
  209. Vector3 global_A = xform_Au.xform(c.local_A);
  210. Vector3 global_B = xform_Bu.xform(c.local_B);
  211. real_t depth = c.normal.dot(global_A - global_B);
  212. if (depth <= 0) {
  213. c.active = false;
  214. continue;
  215. }
  216. c.active = true;
  217. #ifdef DEBUG_ENABLED
  218. if (space->is_debugging_contacts()) {
  219. space->add_debug_contact(global_A + offset_A);
  220. space->add_debug_contact(global_B + offset_A);
  221. }
  222. #endif
  223. c.rA = global_A - A->get_center_of_mass();
  224. c.rB = global_B - B->get_center_of_mass() - offset_B;
  225. // contact query reporting...
  226. if (A->can_report_contacts()) {
  227. Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity();
  228. A->add_contact(global_A, -c.normal, depth, shape_A, global_B, shape_B, B->get_instance_id(), B->get_self(), crA);
  229. }
  230. if (B->can_report_contacts()) {
  231. Vector3 crB = B->get_angular_velocity().cross(c.rB) + B->get_linear_velocity();
  232. B->add_contact(global_B, c.normal, depth, shape_B, global_A, shape_A, A->get_instance_id(), A->get_self(), crB);
  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_angular_velocity().cross(c.rA);
  251. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  252. Vector3 dv = B->get_linear_velocity() + crB - A->get_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. for (int i = 0; i < contact_count; i++) {
  263. Contact &c = contacts[i];
  264. if (!c.active)
  265. continue;
  266. c.active = false; //try to deactivate, will activate itself if still needed
  267. //bias impulse
  268. Vector3 crbA = A->get_biased_angular_velocity().cross(c.rA);
  269. Vector3 crbB = B->get_biased_angular_velocity().cross(c.rB);
  270. Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  271. real_t vbn = dbv.dot(c.normal);
  272. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  273. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  274. real_t jbnOld = c.acc_bias_impulse;
  275. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  276. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  277. A->apply_bias_impulse(c.rA + A->get_center_of_mass(), -jb, MAX_BIAS_ROTATION / p_step);
  278. B->apply_bias_impulse(c.rB + B->get_center_of_mass(), jb, MAX_BIAS_ROTATION / p_step);
  279. crbA = A->get_biased_angular_velocity().cross(c.rA);
  280. crbB = B->get_biased_angular_velocity().cross(c.rB);
  281. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  282. vbn = dbv.dot(c.normal);
  283. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  284. real_t jbn_com = (-vbn + c.bias) / (A->get_inv_mass() + B->get_inv_mass());
  285. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  286. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  287. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  288. A->apply_bias_impulse(A->get_center_of_mass(), -jb_com, 0.0f);
  289. B->apply_bias_impulse(B->get_center_of_mass(), jb_com, 0.0f);
  290. }
  291. c.active = true;
  292. }
  293. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  294. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  295. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  296. //normal impulse
  297. real_t vn = dv.dot(c.normal);
  298. if (Math::abs(vn) > MIN_VELOCITY) {
  299. real_t jn = -(c.bounce + vn) * c.mass_normal;
  300. real_t jnOld = c.acc_normal_impulse;
  301. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  302. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  303. A->apply_impulse(c.rA + A->get_center_of_mass(), -j);
  304. B->apply_impulse(c.rB + B->get_center_of_mass(), j);
  305. c.active = true;
  306. }
  307. //friction impulse
  308. real_t friction = combine_friction(A, B);
  309. Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross(c.rA);
  310. Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross(c.rB);
  311. Vector3 dtv = lvB - lvA;
  312. real_t tn = c.normal.dot(dtv);
  313. // tangential velocity
  314. Vector3 tv = dtv - c.normal * tn;
  315. real_t tvl = tv.length();
  316. if (tvl > MIN_VELOCITY) {
  317. tv /= tvl;
  318. Vector3 temp1 = A->get_inv_inertia_tensor().xform(c.rA.cross(tv));
  319. Vector3 temp2 = B->get_inv_inertia_tensor().xform(c.rB.cross(tv));
  320. real_t t = -tvl /
  321. (A->get_inv_mass() + B->get_inv_mass() + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));
  322. Vector3 jt = t * tv;
  323. Vector3 jtOld = c.acc_tangent_impulse;
  324. c.acc_tangent_impulse += jt;
  325. real_t fi_len = c.acc_tangent_impulse.length();
  326. real_t jtMax = c.acc_normal_impulse * friction;
  327. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  328. c.acc_tangent_impulse *= jtMax / fi_len;
  329. }
  330. jt = c.acc_tangent_impulse - jtOld;
  331. A->apply_impulse(c.rA + A->get_center_of_mass(), -jt);
  332. B->apply_impulse(c.rB + B->get_center_of_mass(), jt);
  333. c.active = true;
  334. }
  335. }
  336. }
  337. BodyPairSW::BodyPairSW(BodySW *p_A, int p_shape_A, BodySW *p_B, int p_shape_B) :
  338. ConstraintSW(_arr, 2) {
  339. A = p_A;
  340. B = p_B;
  341. shape_A = p_shape_A;
  342. shape_B = p_shape_B;
  343. space = A->get_space();
  344. A->add_constraint(this, 0);
  345. B->add_constraint(this, 1);
  346. contact_count = 0;
  347. collided = false;
  348. }
  349. BodyPairSW::~BodyPairSW() {
  350. A->remove_constraint(this);
  351. B->remove_constraint(this);
  352. }