2
0

godot_body_pair_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*************************************************************************/
  2. /* godot_body_pair_3d.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 "godot_body_pair_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. #include "godot_space_3d.h"
  33. #include "core/os/os.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 GodotBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  46. GodotBodyPair3D *pair = (GodotBodyPair3D *)p_userdata;
  47. pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B);
  48. }
  49. void GodotBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_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.index_A = p_index_A;
  63. contact.index_B = p_index_B;
  64. contact.local_A = local_A;
  65. contact.local_B = local_B;
  66. contact.normal = (p_point_A - p_point_B).normalized();
  67. contact.mass_normal = 0; // will be computed in setup()
  68. // attempt to determine if the contact will be reused
  69. real_t contact_recycle_radius = space->get_contact_recycle_radius();
  70. for (int i = 0; i < contact_count; i++) {
  71. Contact &c = contacts[i];
  72. if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&
  73. c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {
  74. contact.acc_normal_impulse = c.acc_normal_impulse;
  75. contact.acc_bias_impulse = c.acc_bias_impulse;
  76. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  77. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  78. new_index = i;
  79. break;
  80. }
  81. }
  82. // figure out if the contact amount must be reduced to fit the new contact
  83. if (new_index == MAX_CONTACTS) {
  84. // remove the contact with the minimum depth
  85. int least_deep = -1;
  86. real_t min_depth = 1e10;
  87. for (int i = 0; i <= contact_count; i++) {
  88. Contact &c = (i == contact_count) ? contact : contacts[i];
  89. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  90. Vector3 global_B = B->get_transform().basis.xform(c.local_B) + offset_B;
  91. Vector3 axis = global_A - global_B;
  92. real_t depth = axis.dot(c.normal);
  93. if (depth < min_depth) {
  94. min_depth = depth;
  95. least_deep = i;
  96. }
  97. }
  98. ERR_FAIL_COND(least_deep == -1);
  99. if (least_deep < contact_count) { //replace the last deep contact by the new one
  100. contacts[least_deep] = contact;
  101. }
  102. return;
  103. }
  104. contacts[new_index] = contact;
  105. if (new_index == contact_count) {
  106. contact_count++;
  107. }
  108. }
  109. void GodotBodyPair3D::validate_contacts() {
  110. //make sure to erase contacts that are no longer valid
  111. real_t contact_max_separation = space->get_contact_max_separation();
  112. for (int i = 0; i < contact_count; i++) {
  113. Contact &c = contacts[i];
  114. Vector3 global_A = A->get_transform().basis.xform(c.local_A);
  115. Vector3 global_B = B->get_transform().basis.xform(c.local_B) + offset_B;
  116. Vector3 axis = global_A - global_B;
  117. real_t depth = axis.dot(c.normal);
  118. if (depth < -contact_max_separation || (global_B + c.normal * depth - global_A).length() > contact_max_separation) {
  119. // contact no longer needed, remove
  120. if ((i + 1) < contact_count) {
  121. // swap with the last one
  122. SWAP(contacts[i], contacts[contact_count - 1]);
  123. }
  124. i--;
  125. contact_count--;
  126. }
  127. }
  128. }
  129. bool GodotBodyPair3D::_test_ccd(real_t p_step, GodotBody3D *p_A, int p_shape_A, const Transform3D &p_xform_A, GodotBody3D *p_B, int p_shape_B, const Transform3D &p_xform_B) {
  130. Vector3 motion = p_A->get_linear_velocity() * p_step;
  131. real_t mlen = motion.length();
  132. if (mlen < CMP_EPSILON) {
  133. return false;
  134. }
  135. Vector3 mnormal = motion / mlen;
  136. real_t min, max;
  137. p_A->get_shape(p_shape_A)->project_range(mnormal, p_xform_A, min, max);
  138. bool fast_object = mlen > (max - min) * 0.3; //going too fast in that direction
  139. 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
  140. return false;
  141. }
  142. //cast a segment from support in motion normal, in the same direction of motion by motion length
  143. //support is the worst case collision point, so real collision happened before
  144. Vector3 s = p_A->get_shape(p_shape_A)->get_support(p_xform_A.basis.xform(mnormal).normalized());
  145. Vector3 from = p_xform_A.xform(s);
  146. Vector3 to = from + motion;
  147. Transform3D from_inv = p_xform_B.affine_inverse();
  148. Vector3 local_from = from_inv.xform(from - mnormal * mlen * 0.1); //start from a little inside the bounding box
  149. Vector3 local_to = from_inv.xform(to);
  150. Vector3 rpos, rnorm;
  151. if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
  152. return false;
  153. }
  154. //shorten the linear velocity so it does not hit, but gets close enough, next frame will hit softly or soft enough
  155. Vector3 hitpos = p_xform_B.xform(rpos);
  156. real_t newlen = hitpos.distance_to(from) - (max - min) * 0.01;
  157. p_A->set_linear_velocity((mnormal * newlen) / p_step);
  158. return true;
  159. }
  160. real_t combine_bounce(GodotBody3D *A, GodotBody3D *B) {
  161. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  162. }
  163. real_t combine_friction(GodotBody3D *A, GodotBody3D *B) {
  164. return ABS(MIN(A->get_friction(), B->get_friction()));
  165. }
  166. bool GodotBodyPair3D::setup(real_t p_step) {
  167. if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
  168. collided = false;
  169. return false;
  170. }
  171. collide_A = (A->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && A->collides_with(B);
  172. collide_B = (B->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && B->collides_with(A);
  173. report_contacts_only = false;
  174. if (!collide_A && !collide_B) {
  175. if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
  176. report_contacts_only = true;
  177. } else {
  178. collided = false;
  179. return false;
  180. }
  181. }
  182. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  183. validate_contacts();
  184. const Vector3 &offset_A = A->get_transform().get_origin();
  185. Transform3D xform_Au = Transform3D(A->get_transform().basis, Vector3());
  186. Transform3D xform_A = xform_Au * A->get_shape_transform(shape_A);
  187. Transform3D xform_Bu = B->get_transform();
  188. xform_Bu.origin -= offset_A;
  189. Transform3D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  190. GodotShape3D *shape_A_ptr = A->get_shape(shape_A);
  191. GodotShape3D *shape_B_ptr = B->get_shape(shape_B);
  192. collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  193. if (!collided) {
  194. //test ccd (currently just a raycast)
  195. if (A->is_continuous_collision_detection_enabled() && collide_A) {
  196. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  197. }
  198. if (B->is_continuous_collision_detection_enabled() && collide_B) {
  199. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  200. }
  201. return false;
  202. }
  203. return true;
  204. }
  205. bool GodotBodyPair3D::pre_solve(real_t p_step) {
  206. if (!collided) {
  207. return false;
  208. }
  209. real_t max_penetration = space->get_contact_max_allowed_penetration();
  210. real_t bias = (real_t)0.3;
  211. GodotShape3D *shape_A_ptr = A->get_shape(shape_A);
  212. GodotShape3D *shape_B_ptr = B->get_shape(shape_B);
  213. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  214. if (shape_A_ptr->get_custom_bias() == 0) {
  215. bias = shape_B_ptr->get_custom_bias();
  216. } else if (shape_B_ptr->get_custom_bias() == 0) {
  217. bias = shape_A_ptr->get_custom_bias();
  218. } else {
  219. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  220. }
  221. }
  222. real_t inv_dt = 1.0 / p_step;
  223. bool do_process = false;
  224. const Basis &basis_A = A->get_transform().basis;
  225. const Basis &basis_B = B->get_transform().basis;
  226. Basis zero_basis;
  227. zero_basis.set_zero();
  228. const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;
  229. const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;
  230. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  231. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  232. for (int i = 0; i < contact_count; i++) {
  233. Contact &c = contacts[i];
  234. c.active = false;
  235. Vector3 global_A = basis_A.xform(c.local_A);
  236. Vector3 global_B = basis_B.xform(c.local_B) + offset_B;
  237. Vector3 axis = global_A - global_B;
  238. real_t depth = axis.dot(c.normal);
  239. if (depth <= 0.0) {
  240. continue;
  241. }
  242. #ifdef DEBUG_ENABLED
  243. if (space->is_debugging_contacts()) {
  244. const Vector3 &offset_A = A->get_transform().get_origin();
  245. space->add_debug_contact(global_A + offset_A);
  246. space->add_debug_contact(global_B + offset_A);
  247. }
  248. #endif
  249. c.rA = global_A - A->get_center_of_mass();
  250. c.rB = global_B - B->get_center_of_mass() - offset_B;
  251. // contact query reporting...
  252. if (A->can_report_contacts()) {
  253. Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity();
  254. A->add_contact(global_A, -c.normal, depth, shape_A, global_B, shape_B, B->get_instance_id(), B->get_self(), crA);
  255. }
  256. if (B->can_report_contacts()) {
  257. Vector3 crB = B->get_angular_velocity().cross(c.rB) + B->get_linear_velocity();
  258. B->add_contact(global_B, c.normal, depth, shape_B, global_A, shape_A, A->get_instance_id(), A->get_self(), crB);
  259. }
  260. if (report_contacts_only) {
  261. collided = false;
  262. continue;
  263. }
  264. c.active = true;
  265. do_process = true;
  266. // Precompute normal mass, tangent mass, and bias.
  267. Vector3 inertia_A = inv_inertia_tensor_A.xform(c.rA.cross(c.normal));
  268. Vector3 inertia_B = inv_inertia_tensor_B.xform(c.rB.cross(c.normal));
  269. real_t kNormal = inv_mass_A + inv_mass_B;
  270. kNormal += c.normal.dot(inertia_A.cross(c.rA)) + c.normal.dot(inertia_B.cross(c.rB));
  271. c.mass_normal = 1.0f / kNormal;
  272. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  273. c.depth = depth;
  274. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  275. if (collide_A) {
  276. A->apply_impulse(-j_vec, c.rA + A->get_center_of_mass());
  277. }
  278. if (collide_B) {
  279. B->apply_impulse(j_vec, c.rB + B->get_center_of_mass());
  280. }
  281. c.acc_bias_impulse = 0;
  282. c.acc_bias_impulse_center_of_mass = 0;
  283. c.bounce = combine_bounce(A, B);
  284. if (c.bounce) {
  285. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  286. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  287. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  288. //normal impule
  289. c.bounce = c.bounce * dv.dot(c.normal);
  290. }
  291. }
  292. return do_process;
  293. }
  294. void GodotBodyPair3D::solve(real_t p_step) {
  295. if (!collided) {
  296. return;
  297. }
  298. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  299. Basis zero_basis;
  300. zero_basis.set_zero();
  301. const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;
  302. const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;
  303. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  304. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  305. for (int i = 0; i < contact_count; i++) {
  306. Contact &c = contacts[i];
  307. if (!c.active) {
  308. continue;
  309. }
  310. c.active = false; //try to deactivate, will activate itself if still needed
  311. //bias impulse
  312. Vector3 crbA = A->get_biased_angular_velocity().cross(c.rA);
  313. Vector3 crbB = B->get_biased_angular_velocity().cross(c.rB);
  314. Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  315. real_t vbn = dbv.dot(c.normal);
  316. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  317. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  318. real_t jbnOld = c.acc_bias_impulse;
  319. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  320. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  321. if (collide_A) {
  322. A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);
  323. }
  324. if (collide_B) {
  325. B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);
  326. }
  327. crbA = A->get_biased_angular_velocity().cross(c.rA);
  328. crbB = B->get_biased_angular_velocity().cross(c.rB);
  329. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  330. vbn = dbv.dot(c.normal);
  331. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  332. real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);
  333. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  334. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  335. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  336. if (collide_A) {
  337. A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);
  338. }
  339. if (collide_B) {
  340. B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);
  341. }
  342. }
  343. c.active = true;
  344. }
  345. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  346. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  347. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  348. //normal impulse
  349. real_t vn = dv.dot(c.normal);
  350. if (Math::abs(vn) > MIN_VELOCITY) {
  351. real_t jn = -(c.bounce + vn) * c.mass_normal;
  352. real_t jnOld = c.acc_normal_impulse;
  353. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  354. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  355. if (collide_A) {
  356. A->apply_impulse(-j, c.rA + A->get_center_of_mass());
  357. }
  358. if (collide_B) {
  359. B->apply_impulse(j, c.rB + B->get_center_of_mass());
  360. }
  361. c.active = true;
  362. }
  363. //friction impulse
  364. real_t friction = combine_friction(A, B);
  365. Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross(c.rA);
  366. Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross(c.rB);
  367. Vector3 dtv = lvB - lvA;
  368. real_t tn = c.normal.dot(dtv);
  369. // tangential velocity
  370. Vector3 tv = dtv - c.normal * tn;
  371. real_t tvl = tv.length();
  372. if (tvl > MIN_VELOCITY) {
  373. tv /= tvl;
  374. Vector3 temp1 = inv_inertia_tensor_A.xform(c.rA.cross(tv));
  375. Vector3 temp2 = inv_inertia_tensor_B.xform(c.rB.cross(tv));
  376. real_t t = -tvl / (inv_mass_A + inv_mass_B + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));
  377. Vector3 jt = t * tv;
  378. Vector3 jtOld = c.acc_tangent_impulse;
  379. c.acc_tangent_impulse += jt;
  380. real_t fi_len = c.acc_tangent_impulse.length();
  381. real_t jtMax = c.acc_normal_impulse * friction;
  382. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  383. c.acc_tangent_impulse *= jtMax / fi_len;
  384. }
  385. jt = c.acc_tangent_impulse - jtOld;
  386. if (collide_A) {
  387. A->apply_impulse(-jt, c.rA + A->get_center_of_mass());
  388. }
  389. if (collide_B) {
  390. B->apply_impulse(jt, c.rB + B->get_center_of_mass());
  391. }
  392. c.active = true;
  393. }
  394. }
  395. }
  396. GodotBodyPair3D::GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B) :
  397. GodotBodyContact3D(_arr, 2) {
  398. A = p_A;
  399. B = p_B;
  400. shape_A = p_shape_A;
  401. shape_B = p_shape_B;
  402. space = A->get_space();
  403. A->add_constraint(this, 0);
  404. B->add_constraint(this, 1);
  405. }
  406. GodotBodyPair3D::~GodotBodyPair3D() {
  407. A->remove_constraint(this);
  408. B->remove_constraint(this);
  409. }
  410. void GodotBodySoftBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  411. GodotBodySoftBodyPair3D *pair = (GodotBodySoftBodyPair3D *)p_userdata;
  412. pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B);
  413. }
  414. void GodotBodySoftBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B) {
  415. Vector3 local_A = body->get_inv_transform().xform(p_point_A);
  416. Vector3 local_B = p_point_B - soft_body->get_node_position(p_index_B);
  417. Contact contact;
  418. contact.index_A = p_index_A;
  419. contact.index_B = p_index_B;
  420. contact.acc_normal_impulse = 0;
  421. contact.acc_bias_impulse = 0;
  422. contact.acc_bias_impulse_center_of_mass = 0;
  423. contact.acc_tangent_impulse = Vector3();
  424. contact.local_A = local_A;
  425. contact.local_B = local_B;
  426. contact.normal = (p_point_A - p_point_B).normalized();
  427. contact.mass_normal = 0;
  428. // Attempt to determine if the contact will be reused.
  429. real_t contact_recycle_radius = space->get_contact_recycle_radius();
  430. uint32_t contact_count = contacts.size();
  431. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  432. Contact &c = contacts[contact_index];
  433. if (c.index_B == p_index_B) {
  434. if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&
  435. c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {
  436. contact.acc_normal_impulse = c.acc_normal_impulse;
  437. contact.acc_bias_impulse = c.acc_bias_impulse;
  438. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  439. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  440. }
  441. c = contact;
  442. return;
  443. }
  444. }
  445. contacts.push_back(contact);
  446. }
  447. void GodotBodySoftBodyPair3D::validate_contacts() {
  448. // Make sure to erase contacts that are no longer valid.
  449. const Transform3D &transform_A = body->get_transform();
  450. real_t contact_max_separation = space->get_contact_max_separation();
  451. uint32_t contact_count = contacts.size();
  452. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  453. Contact &c = contacts[contact_index];
  454. Vector3 global_A = transform_A.xform(c.local_A);
  455. Vector3 global_B = soft_body->get_node_position(c.index_B) + c.local_B;
  456. Vector3 axis = global_A - global_B;
  457. real_t depth = axis.dot(c.normal);
  458. if (depth < -contact_max_separation || (global_B + c.normal * depth - global_A).length() > contact_max_separation) {
  459. // Contact no longer needed, remove.
  460. if ((contact_index + 1) < contact_count) {
  461. // Swap with the last one.
  462. SWAP(c, contacts[contact_count - 1]);
  463. }
  464. contact_index--;
  465. contact_count--;
  466. }
  467. }
  468. contacts.resize(contact_count);
  469. }
  470. bool GodotBodySoftBodyPair3D::setup(real_t p_step) {
  471. if (!body->interacts_with(soft_body) || body->has_exception(soft_body->get_self()) || soft_body->has_exception(body->get_self())) {
  472. collided = false;
  473. return false;
  474. }
  475. body_collides = (body->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && body->collides_with(soft_body);
  476. soft_body_collides = soft_body->collides_with(body);
  477. if (!body_collides && !soft_body_collides) {
  478. if (body->get_max_contacts_reported() > 0) {
  479. report_contacts_only = true;
  480. } else {
  481. collided = false;
  482. return false;
  483. }
  484. }
  485. const Transform3D &xform_Au = body->get_transform();
  486. Transform3D xform_A = xform_Au * body->get_shape_transform(body_shape);
  487. Transform3D xform_Bu = soft_body->get_transform();
  488. Transform3D xform_B = xform_Bu * soft_body->get_shape_transform(0);
  489. validate_contacts();
  490. GodotShape3D *shape_A_ptr = body->get_shape(body_shape);
  491. GodotShape3D *shape_B_ptr = soft_body->get_shape(0);
  492. collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  493. return collided;
  494. }
  495. bool GodotBodySoftBodyPair3D::pre_solve(real_t p_step) {
  496. if (!collided) {
  497. return false;
  498. }
  499. real_t max_penetration = space->get_contact_max_allowed_penetration();
  500. real_t bias = (real_t)0.3;
  501. GodotShape3D *shape_A_ptr = body->get_shape(body_shape);
  502. if (shape_A_ptr->get_custom_bias()) {
  503. bias = shape_A_ptr->get_custom_bias();
  504. }
  505. real_t inv_dt = 1.0 / p_step;
  506. bool do_process = false;
  507. const Transform3D &transform_A = body->get_transform();
  508. Basis zero_basis;
  509. zero_basis.set_zero();
  510. const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;
  511. real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;
  512. uint32_t contact_count = contacts.size();
  513. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  514. Contact &c = contacts[contact_index];
  515. c.active = false;
  516. real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;
  517. if ((node_inv_mass == 0.0) && (body_inv_mass == 0.0)) {
  518. continue;
  519. }
  520. Vector3 global_A = transform_A.xform(c.local_A);
  521. Vector3 global_B = soft_body->get_node_position(c.index_B) + c.local_B;
  522. Vector3 axis = global_A - global_B;
  523. real_t depth = axis.dot(c.normal);
  524. if (depth <= 0.0) {
  525. continue;
  526. }
  527. #ifdef DEBUG_ENABLED
  528. if (space->is_debugging_contacts()) {
  529. space->add_debug_contact(global_A);
  530. space->add_debug_contact(global_B);
  531. }
  532. #endif
  533. c.rA = global_A - transform_A.origin - body->get_center_of_mass();
  534. c.rB = global_B;
  535. if (body->can_report_contacts()) {
  536. Vector3 crA = body->get_angular_velocity().cross(c.rA) + body->get_linear_velocity();
  537. body->add_contact(global_A, -c.normal, depth, body_shape, global_B, 0, soft_body->get_instance_id(), soft_body->get_self(), crA);
  538. }
  539. if (report_contacts_only) {
  540. collided = false;
  541. continue;
  542. }
  543. c.active = true;
  544. do_process = true;
  545. if (body_collides) {
  546. body->set_active(true);
  547. }
  548. // Precompute normal mass, tangent mass, and bias.
  549. Vector3 inertia_A = body_inv_inertia_tensor.xform(c.rA.cross(c.normal));
  550. real_t kNormal = body_inv_mass + node_inv_mass;
  551. kNormal += c.normal.dot(inertia_A.cross(c.rA));
  552. c.mass_normal = 1.0f / kNormal;
  553. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  554. c.depth = depth;
  555. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  556. if (body_collides) {
  557. body->apply_impulse(-j_vec, c.rA + body->get_center_of_mass());
  558. }
  559. if (soft_body_collides) {
  560. soft_body->apply_node_impulse(c.index_B, j_vec);
  561. }
  562. c.acc_bias_impulse = 0;
  563. c.acc_bias_impulse_center_of_mass = 0;
  564. c.bounce = body->get_bounce();
  565. if (c.bounce) {
  566. Vector3 crA = body->get_angular_velocity().cross(c.rA);
  567. Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;
  568. // Normal impulse.
  569. c.bounce = c.bounce * dv.dot(c.normal);
  570. }
  571. }
  572. return do_process;
  573. }
  574. void GodotBodySoftBodyPair3D::solve(real_t p_step) {
  575. if (!collided) {
  576. return;
  577. }
  578. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  579. Basis zero_basis;
  580. zero_basis.set_zero();
  581. const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;
  582. real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;
  583. uint32_t contact_count = contacts.size();
  584. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  585. Contact &c = contacts[contact_index];
  586. if (!c.active) {
  587. continue;
  588. }
  589. c.active = false;
  590. real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;
  591. // Bias impulse.
  592. Vector3 crbA = body->get_biased_angular_velocity().cross(c.rA);
  593. Vector3 dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;
  594. real_t vbn = dbv.dot(c.normal);
  595. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  596. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  597. real_t jbnOld = c.acc_bias_impulse;
  598. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  599. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  600. if (body_collides) {
  601. body->apply_bias_impulse(-jb, c.rA + body->get_center_of_mass(), max_bias_av);
  602. }
  603. if (soft_body_collides) {
  604. soft_body->apply_node_bias_impulse(c.index_B, jb);
  605. }
  606. crbA = body->get_biased_angular_velocity().cross(c.rA);
  607. dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;
  608. vbn = dbv.dot(c.normal);
  609. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  610. real_t jbn_com = (-vbn + c.bias) / (body_inv_mass + node_inv_mass);
  611. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  612. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  613. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  614. if (body_collides) {
  615. body->apply_bias_impulse(-jb_com, body->get_center_of_mass(), 0.0f);
  616. }
  617. if (soft_body_collides) {
  618. soft_body->apply_node_bias_impulse(c.index_B, jb_com);
  619. }
  620. }
  621. c.active = true;
  622. }
  623. Vector3 crA = body->get_angular_velocity().cross(c.rA);
  624. Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;
  625. // Normal impulse.
  626. real_t vn = dv.dot(c.normal);
  627. if (Math::abs(vn) > MIN_VELOCITY) {
  628. real_t jn = -(c.bounce + vn) * c.mass_normal;
  629. real_t jnOld = c.acc_normal_impulse;
  630. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  631. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  632. if (body_collides) {
  633. body->apply_impulse(-j, c.rA + body->get_center_of_mass());
  634. }
  635. if (soft_body_collides) {
  636. soft_body->apply_node_impulse(c.index_B, j);
  637. }
  638. c.active = true;
  639. }
  640. // Friction impulse.
  641. real_t friction = body->get_friction();
  642. Vector3 lvA = body->get_linear_velocity() + body->get_angular_velocity().cross(c.rA);
  643. Vector3 lvB = soft_body->get_node_velocity(c.index_B);
  644. Vector3 dtv = lvB - lvA;
  645. real_t tn = c.normal.dot(dtv);
  646. // Tangential velocity.
  647. Vector3 tv = dtv - c.normal * tn;
  648. real_t tvl = tv.length();
  649. if (tvl > MIN_VELOCITY) {
  650. tv /= tvl;
  651. Vector3 temp1 = body_inv_inertia_tensor.xform(c.rA.cross(tv));
  652. real_t t = -tvl / (body_inv_mass + node_inv_mass + tv.dot(temp1.cross(c.rA)));
  653. Vector3 jt = t * tv;
  654. Vector3 jtOld = c.acc_tangent_impulse;
  655. c.acc_tangent_impulse += jt;
  656. real_t fi_len = c.acc_tangent_impulse.length();
  657. real_t jtMax = c.acc_normal_impulse * friction;
  658. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  659. c.acc_tangent_impulse *= jtMax / fi_len;
  660. }
  661. jt = c.acc_tangent_impulse - jtOld;
  662. if (body_collides) {
  663. body->apply_impulse(-jt, c.rA + body->get_center_of_mass());
  664. }
  665. if (soft_body_collides) {
  666. soft_body->apply_node_impulse(c.index_B, jt);
  667. }
  668. c.active = true;
  669. }
  670. }
  671. }
  672. GodotBodySoftBodyPair3D::GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B) :
  673. GodotBodyContact3D(&body, 1) {
  674. body = p_A;
  675. soft_body = p_B;
  676. body_shape = p_shape_A;
  677. space = p_A->get_space();
  678. body->add_constraint(this, 0);
  679. soft_body->add_constraint(this);
  680. }
  681. GodotBodySoftBodyPair3D::~GodotBodySoftBodyPair3D() {
  682. body->remove_constraint(this);
  683. soft_body->remove_constraint(this);
  684. }