godot_body_pair_3d.cpp 31 KB

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