godot_body_pair_3d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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) 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 "godot_body_pair_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. #include "godot_space_3d.h"
  33. #define MIN_VELOCITY 0.0001
  34. #define MAX_BIAS_ROTATION (Math_PI / 8)
  35. void GodotBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
  36. GodotBodyPair3D *pair = static_cast<GodotBodyPair3D *>(p_userdata);
  37. pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B, normal);
  38. }
  39. void GodotBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal) {
  40. Vector3 local_A = A->get_inv_transform().basis.xform(p_point_A);
  41. Vector3 local_B = B->get_inv_transform().basis.xform(p_point_B - offset_B);
  42. int new_index = contact_count;
  43. ERR_FAIL_COND(new_index >= (MAX_CONTACTS + 1));
  44. Contact contact;
  45. contact.index_A = p_index_A;
  46. contact.index_B = p_index_B;
  47. contact.local_A = local_A;
  48. contact.local_B = local_B;
  49. contact.normal = (p_point_A - p_point_B).normalized();
  50. contact.used = true;
  51. // Attempt to determine if the contact will be reused.
  52. real_t contact_recycle_radius = space->get_contact_recycle_radius();
  53. for (int i = 0; i < contact_count; i++) {
  54. Contact &c = contacts[i];
  55. if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&
  56. c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {
  57. contact.acc_normal_impulse = c.acc_normal_impulse;
  58. contact.acc_bias_impulse = c.acc_bias_impulse;
  59. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  60. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  61. c = contact;
  62. return;
  63. }
  64. }
  65. // Figure out if the contact amount must be reduced to fit the new contact.
  66. if (new_index == MAX_CONTACTS) {
  67. // Remove the contact with the minimum depth.
  68. const Basis &basis_A = A->get_transform().basis;
  69. const Basis &basis_B = B->get_transform().basis;
  70. int least_deep = -1;
  71. real_t min_depth;
  72. // Start with depth for new contact.
  73. {
  74. Vector3 global_A = basis_A.xform(contact.local_A);
  75. Vector3 global_B = basis_B.xform(contact.local_B) + offset_B;
  76. Vector3 axis = global_A - global_B;
  77. min_depth = axis.dot(contact.normal);
  78. }
  79. for (int i = 0; i < contact_count; i++) {
  80. const Contact &c = contacts[i];
  81. Vector3 global_A = basis_A.xform(c.local_A);
  82. Vector3 global_B = basis_B.xform(c.local_B) + offset_B;
  83. Vector3 axis = global_A - global_B;
  84. real_t depth = axis.dot(c.normal);
  85. if (depth < min_depth) {
  86. min_depth = depth;
  87. least_deep = i;
  88. }
  89. }
  90. if (least_deep > -1) {
  91. // Replace the least deep contact by the new one.
  92. contacts[least_deep] = contact;
  93. }
  94. return;
  95. }
  96. contacts[new_index] = contact;
  97. contact_count++;
  98. }
  99. void GodotBodyPair3D::validate_contacts() {
  100. // Make sure to erase contacts that are no longer valid.
  101. real_t max_separation = space->get_contact_max_separation();
  102. real_t max_separation2 = max_separation * max_separation;
  103. const Basis &basis_A = A->get_transform().basis;
  104. const Basis &basis_B = B->get_transform().basis;
  105. for (int i = 0; i < contact_count; i++) {
  106. Contact &c = contacts[i];
  107. bool erase = false;
  108. if (!c.used) {
  109. // Was left behind in previous frame.
  110. erase = true;
  111. } else {
  112. c.used = false;
  113. Vector3 global_A = basis_A.xform(c.local_A);
  114. Vector3 global_B = basis_B.xform(c.local_B) + offset_B;
  115. Vector3 axis = global_A - global_B;
  116. real_t depth = axis.dot(c.normal);
  117. if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {
  118. erase = true;
  119. }
  120. }
  121. if (erase) {
  122. // Contact no longer needed, remove.
  123. if ((i + 1) < contact_count) {
  124. // Swap with the last one.
  125. SWAP(contacts[i], contacts[contact_count - 1]);
  126. }
  127. i--;
  128. contact_count--;
  129. }
  130. }
  131. }
  132. // `_test_ccd` prevents tunneling by slowing down a high velocity body that is about to collide so
  133. // that next frame it will be at an appropriate location to collide (i.e. slight overlap).
  134. // WARNING: The way velocity is adjusted down to cause a collision means the momentum will be
  135. // weaker than it should for a bounce!
  136. // Process: Only proceed if body A's motion is high relative to its size.
  137. // Cast forward along motion vector to see if A is going to enter/pass B's collider next frame, only proceed if it does.
  138. // Adjust the velocity of A down so that it will just slightly intersect the collider instead of blowing right past it.
  139. 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) {
  140. GodotShape3D *shape_A_ptr = p_A->get_shape(p_shape_A);
  141. Vector3 motion = p_A->get_linear_velocity() * p_step;
  142. real_t mlen = motion.length();
  143. if (mlen < CMP_EPSILON) {
  144. return false;
  145. }
  146. Vector3 mnormal = motion / mlen;
  147. real_t min = 0.0, max = 0.0;
  148. shape_A_ptr->project_range(mnormal, p_xform_A, min, max);
  149. // Did it move enough in this direction to even attempt raycast?
  150. // Let's say it should move more than 1/3 the size of the object in that axis.
  151. bool fast_object = mlen > (max - min) * 0.3;
  152. if (!fast_object) {
  153. return false; // moving slow enough that there's no chance of tunneling.
  154. }
  155. // A is moving fast enough that tunneling might occur. See if it's really about to collide.
  156. // Roughly predict body B's position in the next frame (ignoring collisions).
  157. Transform3D predicted_xform_B = p_xform_B.translated(p_B->get_linear_velocity() * p_step);
  158. // Support points are the farthest forward points on A in the direction of the motion vector.
  159. // i.e. the candidate points of which one should hit B first if any collision does occur.
  160. static const int max_supports = 16;
  161. Vector3 supports_A[max_supports];
  162. int support_count_A;
  163. GodotShape3D::FeatureType support_type_A;
  164. // Convert mnormal into body A's local xform because get_supports requires (and returns) local coordinates.
  165. shape_A_ptr->get_supports(p_xform_A.basis.xform_inv(mnormal).normalized(), max_supports, supports_A, support_count_A, support_type_A);
  166. // Cast a segment from each support point of A in the motion direction.
  167. int segment_support_idx = -1;
  168. float segment_hit_length = FLT_MAX;
  169. Vector3 segment_hit_local;
  170. for (int i = 0; i < support_count_A; i++) {
  171. supports_A[i] = p_xform_A.xform(supports_A[i]);
  172. Vector3 from = supports_A[i];
  173. Vector3 to = from + motion;
  174. Transform3D from_inv = predicted_xform_B.affine_inverse();
  175. // Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.
  176. // At high speeds, this may mean we're actually casting from well behind the body instead of inside it, which is odd.
  177. // But it still works out.
  178. Vector3 local_from = from_inv.xform(from - motion * 0.1);
  179. Vector3 local_to = from_inv.xform(to);
  180. Vector3 rpos, rnorm;
  181. int fi = -1;
  182. if (p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm, fi, true)) {
  183. float hit_length = local_from.distance_to(rpos);
  184. if (hit_length < segment_hit_length) {
  185. segment_support_idx = i;
  186. segment_hit_length = hit_length;
  187. segment_hit_local = rpos;
  188. }
  189. }
  190. }
  191. if (segment_support_idx == -1) {
  192. // There was no hit. Since the segment is the length of per-frame motion, this means the bodies will not
  193. // actually collide yet on next frame. We'll probably check again next frame once they're closer.
  194. return false;
  195. }
  196. Vector3 hitpos = predicted_xform_B.xform(segment_hit_local);
  197. real_t newlen = hitpos.distance_to(supports_A[segment_support_idx]);
  198. // Adding 1% of body length to the distance between collision and support point
  199. // should cause body A's support point to arrive just within B's collider next frame.
  200. newlen += (max - min) * 0.01;
  201. // FIXME: This doesn't always work well when colliding with a triangle face of a trimesh shape.
  202. p_A->set_linear_velocity((mnormal * newlen) / p_step);
  203. return true;
  204. }
  205. real_t combine_bounce(GodotBody3D *A, GodotBody3D *B) {
  206. return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
  207. }
  208. real_t combine_friction(GodotBody3D *A, GodotBody3D *B) {
  209. return ABS(MIN(A->get_friction(), B->get_friction()));
  210. }
  211. bool GodotBodyPair3D::setup(real_t p_step) {
  212. check_ccd = false;
  213. if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
  214. collided = false;
  215. return false;
  216. }
  217. collide_A = (A->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && A->collides_with(B);
  218. collide_B = (B->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && B->collides_with(A);
  219. report_contacts_only = false;
  220. if (!collide_A && !collide_B) {
  221. if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
  222. report_contacts_only = true;
  223. } else {
  224. collided = false;
  225. return false;
  226. }
  227. }
  228. offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
  229. validate_contacts();
  230. const Vector3 &offset_A = A->get_transform().get_origin();
  231. Transform3D xform_Au = Transform3D(A->get_transform().basis, Vector3());
  232. Transform3D xform_A = xform_Au * A->get_shape_transform(shape_A);
  233. Transform3D xform_Bu = B->get_transform();
  234. xform_Bu.origin -= offset_A;
  235. Transform3D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  236. GodotShape3D *shape_A_ptr = A->get_shape(shape_A);
  237. GodotShape3D *shape_B_ptr = B->get_shape(shape_B);
  238. collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  239. if (!collided) {
  240. if (A->is_continuous_collision_detection_enabled() && collide_A) {
  241. check_ccd = true;
  242. return true;
  243. }
  244. if (B->is_continuous_collision_detection_enabled() && collide_B) {
  245. check_ccd = true;
  246. return true;
  247. }
  248. return false;
  249. }
  250. return true;
  251. }
  252. bool GodotBodyPair3D::pre_solve(real_t p_step) {
  253. if (!collided) {
  254. if (check_ccd) {
  255. const Vector3 &offset_A = A->get_transform().get_origin();
  256. Transform3D xform_Au = Transform3D(A->get_transform().basis, Vector3());
  257. Transform3D xform_A = xform_Au * A->get_shape_transform(shape_A);
  258. Transform3D xform_Bu = B->get_transform();
  259. xform_Bu.origin -= offset_A;
  260. Transform3D xform_B = xform_Bu * B->get_shape_transform(shape_B);
  261. if (A->is_continuous_collision_detection_enabled() && collide_A) {
  262. _test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
  263. }
  264. if (B->is_continuous_collision_detection_enabled() && collide_B) {
  265. _test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
  266. }
  267. }
  268. return false;
  269. }
  270. real_t max_penetration = space->get_contact_max_allowed_penetration();
  271. real_t bias = 0.8;
  272. GodotShape3D *shape_A_ptr = A->get_shape(shape_A);
  273. GodotShape3D *shape_B_ptr = B->get_shape(shape_B);
  274. if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
  275. if (shape_A_ptr->get_custom_bias() == 0) {
  276. bias = shape_B_ptr->get_custom_bias();
  277. } else if (shape_B_ptr->get_custom_bias() == 0) {
  278. bias = shape_A_ptr->get_custom_bias();
  279. } else {
  280. bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
  281. }
  282. }
  283. real_t inv_dt = 1.0 / p_step;
  284. bool do_process = false;
  285. const Vector3 &offset_A = A->get_transform().get_origin();
  286. const Basis &basis_A = A->get_transform().basis;
  287. const Basis &basis_B = B->get_transform().basis;
  288. Basis zero_basis;
  289. zero_basis.set_zero();
  290. const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;
  291. const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;
  292. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  293. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  294. for (int i = 0; i < contact_count; i++) {
  295. Contact &c = contacts[i];
  296. c.active = false;
  297. Vector3 global_A = basis_A.xform(c.local_A);
  298. Vector3 global_B = basis_B.xform(c.local_B) + offset_B;
  299. Vector3 axis = global_A - global_B;
  300. real_t depth = axis.dot(c.normal);
  301. if (depth <= 0.0) {
  302. continue;
  303. }
  304. #ifdef DEBUG_ENABLED
  305. if (space->is_debugging_contacts()) {
  306. space->add_debug_contact(global_A + offset_A);
  307. space->add_debug_contact(global_B + offset_A);
  308. }
  309. #endif
  310. c.rA = global_A - A->get_center_of_mass();
  311. c.rB = global_B - B->get_center_of_mass() - offset_B;
  312. // Precompute normal mass, tangent mass, and bias.
  313. Vector3 inertia_A = inv_inertia_tensor_A.xform(c.rA.cross(c.normal));
  314. Vector3 inertia_B = inv_inertia_tensor_B.xform(c.rB.cross(c.normal));
  315. real_t kNormal = inv_mass_A + inv_mass_B;
  316. kNormal += c.normal.dot(inertia_A.cross(c.rA)) + c.normal.dot(inertia_B.cross(c.rB));
  317. c.mass_normal = 1.0f / kNormal;
  318. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  319. c.depth = depth;
  320. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  321. c.acc_impulse -= j_vec;
  322. // contact query reporting...
  323. if (A->can_report_contacts() || B->can_report_contacts()) {
  324. Vector3 crB = B->get_angular_velocity().cross(c.rB) + B->get_linear_velocity();
  325. Vector3 crA = A->get_angular_velocity().cross(c.rA) + A->get_linear_velocity();
  326. if (A->can_report_contacts()) {
  327. A->add_contact(global_A + offset_A, -c.normal, depth, shape_A, crA, global_B + offset_A, shape_B, B->get_instance_id(), B->get_self(), crB, c.acc_impulse);
  328. }
  329. if (B->can_report_contacts()) {
  330. B->add_contact(global_B + offset_A, c.normal, depth, shape_B, crB, global_A + offset_A, shape_A, A->get_instance_id(), A->get_self(), crA, -c.acc_impulse);
  331. }
  332. }
  333. if (report_contacts_only) {
  334. collided = false;
  335. continue;
  336. }
  337. c.active = true;
  338. do_process = true;
  339. if (collide_A) {
  340. A->apply_impulse(-j_vec, c.rA + A->get_center_of_mass());
  341. }
  342. if (collide_B) {
  343. B->apply_impulse(j_vec, c.rB + B->get_center_of_mass());
  344. }
  345. c.bounce = combine_bounce(A, B);
  346. if (c.bounce) {
  347. Vector3 crA = A->get_prev_angular_velocity().cross(c.rA);
  348. Vector3 crB = B->get_prev_angular_velocity().cross(c.rB);
  349. Vector3 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;
  350. c.bounce = c.bounce * dv.dot(c.normal);
  351. }
  352. }
  353. return do_process;
  354. }
  355. void GodotBodyPair3D::solve(real_t p_step) {
  356. if (!collided) {
  357. return;
  358. }
  359. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  360. Basis zero_basis;
  361. zero_basis.set_zero();
  362. const Basis &inv_inertia_tensor_A = collide_A ? A->get_inv_inertia_tensor() : zero_basis;
  363. const Basis &inv_inertia_tensor_B = collide_B ? B->get_inv_inertia_tensor() : zero_basis;
  364. real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
  365. real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
  366. for (int i = 0; i < contact_count; i++) {
  367. Contact &c = contacts[i];
  368. if (!c.active) {
  369. continue;
  370. }
  371. c.active = false; //try to deactivate, will activate itself if still needed
  372. //bias impulse
  373. Vector3 crbA = A->get_biased_angular_velocity().cross(c.rA);
  374. Vector3 crbB = B->get_biased_angular_velocity().cross(c.rB);
  375. Vector3 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  376. real_t vbn = dbv.dot(c.normal);
  377. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  378. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  379. real_t jbnOld = c.acc_bias_impulse;
  380. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  381. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  382. if (collide_A) {
  383. A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);
  384. }
  385. if (collide_B) {
  386. B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);
  387. }
  388. crbA = A->get_biased_angular_velocity().cross(c.rA);
  389. crbB = B->get_biased_angular_velocity().cross(c.rB);
  390. dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
  391. vbn = dbv.dot(c.normal);
  392. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  393. real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);
  394. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  395. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  396. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  397. if (collide_A) {
  398. A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);
  399. }
  400. if (collide_B) {
  401. B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);
  402. }
  403. }
  404. c.active = true;
  405. }
  406. Vector3 crA = A->get_angular_velocity().cross(c.rA);
  407. Vector3 crB = B->get_angular_velocity().cross(c.rB);
  408. Vector3 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
  409. //normal impulse
  410. real_t vn = dv.dot(c.normal);
  411. if (Math::abs(vn) > MIN_VELOCITY) {
  412. real_t jn = -(c.bounce + vn) * c.mass_normal;
  413. real_t jnOld = c.acc_normal_impulse;
  414. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  415. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  416. if (collide_A) {
  417. A->apply_impulse(-j, c.rA + A->get_center_of_mass());
  418. }
  419. if (collide_B) {
  420. B->apply_impulse(j, c.rB + B->get_center_of_mass());
  421. }
  422. c.acc_impulse -= j;
  423. c.active = true;
  424. }
  425. //friction impulse
  426. real_t friction = combine_friction(A, B);
  427. Vector3 lvA = A->get_linear_velocity() + A->get_angular_velocity().cross(c.rA);
  428. Vector3 lvB = B->get_linear_velocity() + B->get_angular_velocity().cross(c.rB);
  429. Vector3 dtv = lvB - lvA;
  430. real_t tn = c.normal.dot(dtv);
  431. // tangential velocity
  432. Vector3 tv = dtv - c.normal * tn;
  433. real_t tvl = tv.length();
  434. if (tvl > MIN_VELOCITY) {
  435. tv /= tvl;
  436. Vector3 temp1 = inv_inertia_tensor_A.xform(c.rA.cross(tv));
  437. Vector3 temp2 = inv_inertia_tensor_B.xform(c.rB.cross(tv));
  438. real_t t = -tvl / (inv_mass_A + inv_mass_B + tv.dot(temp1.cross(c.rA) + temp2.cross(c.rB)));
  439. Vector3 jt = t * tv;
  440. Vector3 jtOld = c.acc_tangent_impulse;
  441. c.acc_tangent_impulse += jt;
  442. real_t fi_len = c.acc_tangent_impulse.length();
  443. real_t jtMax = c.acc_normal_impulse * friction;
  444. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  445. c.acc_tangent_impulse *= jtMax / fi_len;
  446. }
  447. jt = c.acc_tangent_impulse - jtOld;
  448. if (collide_A) {
  449. A->apply_impulse(-jt, c.rA + A->get_center_of_mass());
  450. }
  451. if (collide_B) {
  452. B->apply_impulse(jt, c.rB + B->get_center_of_mass());
  453. }
  454. c.acc_impulse -= jt;
  455. c.active = true;
  456. }
  457. }
  458. }
  459. GodotBodyPair3D::GodotBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotBody3D *p_B, int p_shape_B) :
  460. GodotBodyContact3D(_arr, 2) {
  461. A = p_A;
  462. B = p_B;
  463. shape_A = p_shape_A;
  464. shape_B = p_shape_B;
  465. space = A->get_space();
  466. A->add_constraint(this, 0);
  467. B->add_constraint(this, 1);
  468. }
  469. GodotBodyPair3D::~GodotBodyPair3D() {
  470. A->remove_constraint(this);
  471. B->remove_constraint(this);
  472. }
  473. void GodotBodySoftBodyPair3D::_contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
  474. GodotBodySoftBodyPair3D *pair = static_cast<GodotBodySoftBodyPair3D *>(p_userdata);
  475. pair->contact_added_callback(p_point_A, p_index_A, p_point_B, p_index_B, normal);
  476. }
  477. void GodotBodySoftBodyPair3D::contact_added_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal) {
  478. Vector3 local_A = body->get_inv_transform().xform(p_point_A);
  479. Vector3 local_B = p_point_B - soft_body->get_node_position(p_index_B);
  480. Contact contact;
  481. contact.index_A = p_index_A;
  482. contact.index_B = p_index_B;
  483. contact.local_A = local_A;
  484. contact.local_B = local_B;
  485. contact.normal = (normal.dot((p_point_A - p_point_B)) < 0 ? -normal : normal);
  486. contact.used = true;
  487. // Attempt to determine if the contact will be reused.
  488. real_t contact_recycle_radius = space->get_contact_recycle_radius();
  489. uint32_t contact_count = contacts.size();
  490. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  491. Contact &c = contacts[contact_index];
  492. if (c.index_B == p_index_B) {
  493. if (c.local_A.distance_squared_to(local_A) < (contact_recycle_radius * contact_recycle_radius) &&
  494. c.local_B.distance_squared_to(local_B) < (contact_recycle_radius * contact_recycle_radius)) {
  495. contact.acc_normal_impulse = c.acc_normal_impulse;
  496. contact.acc_bias_impulse = c.acc_bias_impulse;
  497. contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
  498. contact.acc_tangent_impulse = c.acc_tangent_impulse;
  499. }
  500. c = contact;
  501. return;
  502. }
  503. }
  504. contacts.push_back(contact);
  505. }
  506. void GodotBodySoftBodyPair3D::validate_contacts() {
  507. // Make sure to erase contacts that are no longer valid.
  508. real_t max_separation = space->get_contact_max_separation();
  509. real_t max_separation2 = max_separation * max_separation;
  510. const Transform3D &transform_A = body->get_transform();
  511. uint32_t contact_count = contacts.size();
  512. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  513. Contact &c = contacts[contact_index];
  514. bool erase = false;
  515. if (!c.used) {
  516. // Was left behind in previous frame.
  517. erase = true;
  518. } else {
  519. c.used = false;
  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 < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {
  525. erase = true;
  526. }
  527. }
  528. if (erase) {
  529. // Contact no longer needed, remove.
  530. if ((contact_index + 1) < contact_count) {
  531. // Swap with the last one.
  532. SWAP(c, contacts[contact_count - 1]);
  533. }
  534. contact_index--;
  535. contact_count--;
  536. }
  537. }
  538. contacts.resize(contact_count);
  539. }
  540. bool GodotBodySoftBodyPair3D::setup(real_t p_step) {
  541. if (!body->interacts_with(soft_body) || body->has_exception(soft_body->get_self()) || soft_body->has_exception(body->get_self())) {
  542. collided = false;
  543. return false;
  544. }
  545. body_collides = (body->get_mode() > PhysicsServer3D::BODY_MODE_KINEMATIC) && body->collides_with(soft_body);
  546. soft_body_collides = soft_body->collides_with(body);
  547. if (!body_collides && !soft_body_collides) {
  548. if (body->get_max_contacts_reported() > 0) {
  549. report_contacts_only = true;
  550. } else {
  551. collided = false;
  552. return false;
  553. }
  554. }
  555. const Transform3D &xform_Au = body->get_transform();
  556. Transform3D xform_A = xform_Au * body->get_shape_transform(body_shape);
  557. Transform3D xform_Bu = soft_body->get_transform();
  558. Transform3D xform_B = xform_Bu * soft_body->get_shape_transform(0);
  559. validate_contacts();
  560. GodotShape3D *shape_A_ptr = body->get_shape(body_shape);
  561. GodotShape3D *shape_B_ptr = soft_body->get_shape(0);
  562. collided = GodotCollisionSolver3D::solve_static(shape_A_ptr, xform_A, shape_B_ptr, xform_B, _contact_added_callback, this, &sep_axis);
  563. return collided;
  564. }
  565. bool GodotBodySoftBodyPair3D::pre_solve(real_t p_step) {
  566. if (!collided) {
  567. return false;
  568. }
  569. real_t max_penetration = space->get_contact_max_allowed_penetration();
  570. real_t bias = space->get_contact_bias();
  571. GodotShape3D *shape_A_ptr = body->get_shape(body_shape);
  572. if (shape_A_ptr->get_custom_bias()) {
  573. bias = shape_A_ptr->get_custom_bias();
  574. }
  575. real_t inv_dt = 1.0 / p_step;
  576. bool do_process = false;
  577. const Transform3D &transform_A = body->get_transform();
  578. Basis zero_basis;
  579. zero_basis.set_zero();
  580. const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;
  581. real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;
  582. uint32_t contact_count = contacts.size();
  583. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  584. Contact &c = contacts[contact_index];
  585. c.active = false;
  586. real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;
  587. if ((node_inv_mass == 0.0) && (body_inv_mass == 0.0)) {
  588. continue;
  589. }
  590. Vector3 global_A = transform_A.xform(c.local_A);
  591. Vector3 global_B = soft_body->get_node_position(c.index_B) + c.local_B;
  592. Vector3 axis = global_A - global_B;
  593. real_t depth = axis.dot(c.normal);
  594. if (depth <= 0.0) {
  595. continue;
  596. }
  597. #ifdef DEBUG_ENABLED
  598. if (space->is_debugging_contacts()) {
  599. space->add_debug_contact(global_A);
  600. space->add_debug_contact(global_B);
  601. }
  602. #endif
  603. c.rA = global_A - transform_A.origin - body->get_center_of_mass();
  604. c.rB = global_B;
  605. // Precompute normal mass, tangent mass, and bias.
  606. Vector3 inertia_A = body_inv_inertia_tensor.xform(c.rA.cross(c.normal));
  607. real_t kNormal = body_inv_mass + node_inv_mass;
  608. kNormal += c.normal.dot(inertia_A.cross(c.rA));
  609. c.mass_normal = 1.0f / kNormal;
  610. c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
  611. c.depth = depth;
  612. Vector3 j_vec = c.normal * c.acc_normal_impulse + c.acc_tangent_impulse;
  613. if (body_collides) {
  614. body->apply_impulse(-j_vec, c.rA + body->get_center_of_mass());
  615. }
  616. if (soft_body_collides) {
  617. soft_body->apply_node_impulse(c.index_B, j_vec);
  618. }
  619. c.acc_impulse -= j_vec;
  620. if (body->can_report_contacts()) {
  621. Vector3 crA = body->get_angular_velocity().cross(c.rA) + body->get_linear_velocity();
  622. Vector3 crB = soft_body->get_node_velocity(c.index_B);
  623. body->add_contact(global_A, -c.normal, depth, body_shape, crA, global_B, 0, soft_body->get_instance_id(), soft_body->get_self(), crB, c.acc_impulse);
  624. }
  625. if (report_contacts_only) {
  626. collided = false;
  627. continue;
  628. }
  629. c.active = true;
  630. do_process = true;
  631. if (body_collides) {
  632. body->set_active(true);
  633. }
  634. c.bounce = body->get_bounce();
  635. if (c.bounce) {
  636. Vector3 crA = body->get_angular_velocity().cross(c.rA);
  637. Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;
  638. // Normal impulse.
  639. c.bounce = c.bounce * dv.dot(c.normal);
  640. }
  641. }
  642. return do_process;
  643. }
  644. void GodotBodySoftBodyPair3D::solve(real_t p_step) {
  645. if (!collided) {
  646. return;
  647. }
  648. const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
  649. Basis zero_basis;
  650. zero_basis.set_zero();
  651. const Basis &body_inv_inertia_tensor = body_collides ? body->get_inv_inertia_tensor() : zero_basis;
  652. real_t body_inv_mass = body_collides ? body->get_inv_mass() : 0.0;
  653. uint32_t contact_count = contacts.size();
  654. for (uint32_t contact_index = 0; contact_index < contact_count; ++contact_index) {
  655. Contact &c = contacts[contact_index];
  656. if (!c.active) {
  657. continue;
  658. }
  659. c.active = false;
  660. real_t node_inv_mass = soft_body_collides ? soft_body->get_node_inv_mass(c.index_B) : 0.0;
  661. // Bias impulse.
  662. Vector3 crbA = body->get_biased_angular_velocity().cross(c.rA);
  663. Vector3 dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;
  664. real_t vbn = dbv.dot(c.normal);
  665. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  666. real_t jbn = (-vbn + c.bias) * c.mass_normal;
  667. real_t jbnOld = c.acc_bias_impulse;
  668. c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
  669. Vector3 jb = c.normal * (c.acc_bias_impulse - jbnOld);
  670. if (body_collides) {
  671. body->apply_bias_impulse(-jb, c.rA + body->get_center_of_mass(), max_bias_av);
  672. }
  673. if (soft_body_collides) {
  674. soft_body->apply_node_bias_impulse(c.index_B, jb);
  675. }
  676. crbA = body->get_biased_angular_velocity().cross(c.rA);
  677. dbv = soft_body->get_node_biased_velocity(c.index_B) - body->get_biased_linear_velocity() - crbA;
  678. vbn = dbv.dot(c.normal);
  679. if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
  680. real_t jbn_com = (-vbn + c.bias) / (body_inv_mass + node_inv_mass);
  681. real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
  682. c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
  683. Vector3 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
  684. if (body_collides) {
  685. body->apply_bias_impulse(-jb_com, body->get_center_of_mass(), 0.0f);
  686. }
  687. if (soft_body_collides) {
  688. soft_body->apply_node_bias_impulse(c.index_B, jb_com);
  689. }
  690. }
  691. c.active = true;
  692. }
  693. Vector3 crA = body->get_angular_velocity().cross(c.rA);
  694. Vector3 dv = soft_body->get_node_velocity(c.index_B) - body->get_linear_velocity() - crA;
  695. // Normal impulse.
  696. real_t vn = dv.dot(c.normal);
  697. if (Math::abs(vn) > MIN_VELOCITY) {
  698. real_t jn = -(c.bounce + vn) * c.mass_normal;
  699. real_t jnOld = c.acc_normal_impulse;
  700. c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
  701. Vector3 j = c.normal * (c.acc_normal_impulse - jnOld);
  702. if (body_collides) {
  703. body->apply_impulse(-j, c.rA + body->get_center_of_mass());
  704. }
  705. if (soft_body_collides) {
  706. soft_body->apply_node_impulse(c.index_B, j);
  707. }
  708. c.acc_impulse -= j;
  709. c.active = true;
  710. }
  711. // Friction impulse.
  712. real_t friction = body->get_friction();
  713. Vector3 lvA = body->get_linear_velocity() + body->get_angular_velocity().cross(c.rA);
  714. Vector3 lvB = soft_body->get_node_velocity(c.index_B);
  715. Vector3 dtv = lvB - lvA;
  716. real_t tn = c.normal.dot(dtv);
  717. // Tangential velocity.
  718. Vector3 tv = dtv - c.normal * tn;
  719. real_t tvl = tv.length();
  720. if (tvl > MIN_VELOCITY) {
  721. tv /= tvl;
  722. Vector3 temp1 = body_inv_inertia_tensor.xform(c.rA.cross(tv));
  723. real_t t = -tvl / (body_inv_mass + node_inv_mass + tv.dot(temp1.cross(c.rA)));
  724. Vector3 jt = t * tv;
  725. Vector3 jtOld = c.acc_tangent_impulse;
  726. c.acc_tangent_impulse += jt;
  727. real_t fi_len = c.acc_tangent_impulse.length();
  728. real_t jtMax = c.acc_normal_impulse * friction;
  729. if (fi_len > CMP_EPSILON && fi_len > jtMax) {
  730. c.acc_tangent_impulse *= jtMax / fi_len;
  731. }
  732. jt = c.acc_tangent_impulse - jtOld;
  733. if (body_collides) {
  734. body->apply_impulse(-jt, c.rA + body->get_center_of_mass());
  735. }
  736. if (soft_body_collides) {
  737. soft_body->apply_node_impulse(c.index_B, jt);
  738. }
  739. c.acc_impulse -= jt;
  740. c.active = true;
  741. }
  742. }
  743. }
  744. GodotBodySoftBodyPair3D::GodotBodySoftBodyPair3D(GodotBody3D *p_A, int p_shape_A, GodotSoftBody3D *p_B) :
  745. GodotBodyContact3D(&body, 1) {
  746. body = p_A;
  747. soft_body = p_B;
  748. body_shape = p_shape_A;
  749. space = p_A->get_space();
  750. body->add_constraint(this, 0);
  751. soft_body->add_constraint(this);
  752. }
  753. GodotBodySoftBodyPair3D::~GodotBodySoftBodyPair3D() {
  754. body->remove_constraint(this);
  755. soft_body->remove_constraint(this);
  756. }