godot_space_3d.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*************************************************************************/
  2. /* godot_space_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_space_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. #include "godot_physics_server_3d.h"
  33. #include "core/config/project_settings.h"
  34. #define TEST_MOTION_MARGIN_MIN_VALUE 0.0001
  35. #define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
  36. _FORCE_INLINE_ static bool _can_collide_with(GodotCollisionObject3D *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  37. if (!(p_object->get_collision_layer() & p_collision_mask)) {
  38. return false;
  39. }
  40. if (p_object->get_type() == GodotCollisionObject3D::TYPE_AREA && !p_collide_with_areas) {
  41. return false;
  42. }
  43. if (p_object->get_type() == GodotCollisionObject3D::TYPE_BODY && !p_collide_with_bodies) {
  44. return false;
  45. }
  46. if (p_object->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY && !p_collide_with_bodies) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. int GodotPhysicsDirectSpaceState3D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  52. ERR_FAIL_COND_V(space->locked, false);
  53. int amount = space->broadphase->cull_point(p_parameters.position, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  54. int cc = 0;
  55. //Transform3D ai = p_xform.affine_inverse();
  56. for (int i = 0; i < amount; i++) {
  57. if (cc >= p_result_max) {
  58. break;
  59. }
  60. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  61. continue;
  62. }
  63. //area can't be picked by ray (default)
  64. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  65. continue;
  66. }
  67. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  68. int shape_idx = space->intersection_query_subindex_results[i];
  69. Transform3D inv_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  70. inv_xform.affine_invert();
  71. if (!col_obj->get_shape(shape_idx)->intersect_point(inv_xform.xform(p_parameters.position))) {
  72. continue;
  73. }
  74. r_results[cc].collider_id = col_obj->get_instance_id();
  75. if (r_results[cc].collider_id.is_valid()) {
  76. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  77. } else {
  78. r_results[cc].collider = nullptr;
  79. }
  80. r_results[cc].rid = col_obj->get_self();
  81. r_results[cc].shape = shape_idx;
  82. cc++;
  83. }
  84. return cc;
  85. }
  86. bool GodotPhysicsDirectSpaceState3D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) {
  87. ERR_FAIL_COND_V(space->locked, false);
  88. Vector3 begin, end;
  89. Vector3 normal;
  90. begin = p_parameters.from;
  91. end = p_parameters.to;
  92. normal = (end - begin).normalized();
  93. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  94. //todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
  95. bool collided = false;
  96. Vector3 res_point, res_normal;
  97. int res_shape;
  98. const GodotCollisionObject3D *res_obj;
  99. real_t min_d = 1e10;
  100. for (int i = 0; i < amount; i++) {
  101. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  102. continue;
  103. }
  104. if (p_parameters.pick_ray && !(space->intersection_query_results[i]->is_ray_pickable())) {
  105. continue;
  106. }
  107. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  108. continue;
  109. }
  110. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  111. int shape_idx = space->intersection_query_subindex_results[i];
  112. Transform3D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  113. Vector3 local_from = inv_xform.xform(begin);
  114. Vector3 local_to = inv_xform.xform(end);
  115. const GodotShape3D *shape = col_obj->get_shape(shape_idx);
  116. Vector3 shape_point, shape_normal;
  117. if (shape->intersect_point(local_from)) {
  118. if (p_parameters.hit_from_inside) {
  119. // Hit shape at starting point.
  120. min_d = 0;
  121. res_point = begin;
  122. res_normal = Vector3();
  123. res_shape = shape_idx;
  124. res_obj = col_obj;
  125. collided = true;
  126. break;
  127. } else {
  128. // Ignore shape when starting inside.
  129. continue;
  130. }
  131. }
  132. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal, p_parameters.hit_back_faces)) {
  133. Transform3D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  134. shape_point = xform.xform(shape_point);
  135. real_t ld = normal.dot(shape_point);
  136. if (ld < min_d) {
  137. min_d = ld;
  138. res_point = shape_point;
  139. res_normal = inv_xform.basis.xform_inv(shape_normal).normalized();
  140. res_shape = shape_idx;
  141. res_obj = col_obj;
  142. collided = true;
  143. }
  144. }
  145. }
  146. if (!collided) {
  147. return false;
  148. }
  149. r_result.collider_id = res_obj->get_instance_id();
  150. if (r_result.collider_id.is_valid()) {
  151. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  152. } else {
  153. r_result.collider = nullptr;
  154. }
  155. r_result.normal = res_normal;
  156. r_result.position = res_point;
  157. r_result.rid = res_obj->get_self();
  158. r_result.shape = res_shape;
  159. return true;
  160. }
  161. int GodotPhysicsDirectSpaceState3D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  162. if (p_result_max <= 0) {
  163. return 0;
  164. }
  165. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  166. ERR_FAIL_COND_V(!shape, 0);
  167. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  168. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  169. int cc = 0;
  170. //Transform3D ai = p_xform.affine_inverse();
  171. for (int i = 0; i < amount; i++) {
  172. if (cc >= p_result_max) {
  173. break;
  174. }
  175. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  176. continue;
  177. }
  178. //area can't be picked by ray (default)
  179. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  180. continue;
  181. }
  182. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  183. int shape_idx = space->intersection_query_subindex_results[i];
  184. if (!GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), nullptr, nullptr, nullptr, p_parameters.margin, 0)) {
  185. continue;
  186. }
  187. if (r_results) {
  188. r_results[cc].collider_id = col_obj->get_instance_id();
  189. if (r_results[cc].collider_id.is_valid()) {
  190. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  191. } else {
  192. r_results[cc].collider = nullptr;
  193. }
  194. r_results[cc].rid = col_obj->get_self();
  195. r_results[cc].shape = shape_idx;
  196. }
  197. cc++;
  198. }
  199. return cc;
  200. }
  201. bool GodotPhysicsDirectSpaceState3D::cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info) {
  202. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  203. ERR_FAIL_COND_V(!shape, false);
  204. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  205. aabb = aabb.merge(AABB(aabb.position + p_parameters.motion, aabb.size)); //motion
  206. aabb = aabb.grow(p_parameters.margin);
  207. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  208. real_t best_safe = 1;
  209. real_t best_unsafe = 1;
  210. Transform3D xform_inv = p_parameters.transform.affine_inverse();
  211. GodotMotionShape3D mshape;
  212. mshape.shape = shape;
  213. mshape.motion = xform_inv.basis.xform(p_parameters.motion);
  214. bool best_first = true;
  215. Vector3 motion_normal = p_parameters.motion.normalized();
  216. Vector3 closest_A, closest_B;
  217. for (int i = 0; i < amount; i++) {
  218. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  219. continue;
  220. }
  221. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  222. continue; //ignore excluded
  223. }
  224. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  225. int shape_idx = space->intersection_query_subindex_results[i];
  226. Vector3 point_A, point_B;
  227. Vector3 sep_axis = motion_normal;
  228. Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  229. //test initial overlap, does it collide if going all the way?
  230. if (GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  231. continue;
  232. }
  233. //test initial overlap, ignore objects it's inside of.
  234. sep_axis = motion_normal;
  235. if (!GodotCollisionSolver3D::solve_distance(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  236. continue;
  237. }
  238. //just do kinematic solving
  239. real_t low = 0.0;
  240. real_t hi = 1.0;
  241. real_t fraction_coeff = 0.5;
  242. for (int j = 0; j < 8; j++) { //steps should be customizable..
  243. real_t fraction = low + (hi - low) * fraction_coeff;
  244. mshape.motion = xform_inv.basis.xform(p_parameters.motion * fraction);
  245. Vector3 lA, lB;
  246. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  247. bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep);
  248. if (collided) {
  249. hi = fraction;
  250. if ((j == 0) || (low > 0.0)) { // Did it not collide before?
  251. // When alternating or first iteration, use dichotomy.
  252. fraction_coeff = 0.5;
  253. } else {
  254. // When colliding again, converge faster towards low fraction
  255. // for more accurate results with long motions that collide near the start.
  256. fraction_coeff = 0.25;
  257. }
  258. } else {
  259. point_A = lA;
  260. point_B = lB;
  261. low = fraction;
  262. if ((j == 0) || (hi < 1.0)) { // Did it collide before?
  263. // When alternating or first iteration, use dichotomy.
  264. fraction_coeff = 0.5;
  265. } else {
  266. // When not colliding again, converge faster towards high fraction
  267. // for more accurate results with long motions that collide near the end.
  268. fraction_coeff = 0.75;
  269. }
  270. }
  271. }
  272. if (low < best_safe) {
  273. best_first = true; //force reset
  274. best_safe = low;
  275. best_unsafe = hi;
  276. }
  277. if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) {
  278. closest_A = point_A;
  279. closest_B = point_B;
  280. r_info->collider_id = col_obj->get_instance_id();
  281. r_info->rid = col_obj->get_self();
  282. r_info->shape = shape_idx;
  283. r_info->point = closest_B;
  284. r_info->normal = (closest_A - closest_B).normalized();
  285. best_first = false;
  286. if (col_obj->get_type() == GodotCollisionObject3D::TYPE_BODY) {
  287. const GodotBody3D *body = static_cast<const GodotBody3D *>(col_obj);
  288. Vector3 rel_vec = closest_B - (body->get_transform().origin + body->get_center_of_mass());
  289. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  290. }
  291. }
  292. }
  293. p_closest_safe = best_safe;
  294. p_closest_unsafe = best_unsafe;
  295. return true;
  296. }
  297. bool GodotPhysicsDirectSpaceState3D::collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) {
  298. if (p_result_max <= 0) {
  299. return false;
  300. }
  301. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  302. ERR_FAIL_COND_V(!shape, 0);
  303. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  304. aabb = aabb.grow(p_parameters.margin);
  305. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  306. bool collided = false;
  307. r_result_count = 0;
  308. GodotPhysicsServer3D::CollCbkData cbk;
  309. cbk.max = p_result_max;
  310. cbk.amount = 0;
  311. cbk.ptr = r_results;
  312. GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk;
  313. GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk;
  314. for (int i = 0; i < amount; i++) {
  315. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  316. continue;
  317. }
  318. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  319. if (p_parameters.exclude.has(col_obj->get_self())) {
  320. continue;
  321. }
  322. int shape_idx = space->intersection_query_subindex_results[i];
  323. if (GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_parameters.margin)) {
  324. collided = true;
  325. }
  326. }
  327. r_result_count = cbk.amount;
  328. return collided;
  329. }
  330. struct _RestResultData {
  331. const GodotCollisionObject3D *object = nullptr;
  332. int local_shape = 0;
  333. int shape = 0;
  334. Vector3 contact;
  335. Vector3 normal;
  336. real_t len = 0.0;
  337. };
  338. struct _RestCallbackData {
  339. const GodotCollisionObject3D *object = nullptr;
  340. int local_shape = 0;
  341. int shape = 0;
  342. real_t min_allowed_depth = 0.0;
  343. _RestResultData best_result;
  344. int max_results = 0;
  345. int result_count = 0;
  346. _RestResultData *other_results = nullptr;
  347. };
  348. static void _rest_cbk_result(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  349. _RestCallbackData *rd = static_cast<_RestCallbackData *>(p_userdata);
  350. Vector3 contact_rel = p_point_B - p_point_A;
  351. real_t len = contact_rel.length();
  352. if (len < rd->min_allowed_depth) {
  353. return;
  354. }
  355. bool is_best_result = (len > rd->best_result.len);
  356. if (rd->other_results && rd->result_count > 0) {
  357. // Consider as new result by default.
  358. int prev_result_count = rd->result_count++;
  359. int result_index = 0;
  360. real_t tested_len = is_best_result ? rd->best_result.len : len;
  361. for (; result_index < prev_result_count - 1; ++result_index) {
  362. if (tested_len > rd->other_results[result_index].len) {
  363. // Re-using a previous result.
  364. rd->result_count--;
  365. break;
  366. }
  367. }
  368. if (result_index < rd->max_results - 1) {
  369. _RestResultData &result = rd->other_results[result_index];
  370. if (is_best_result) {
  371. // Keep the previous best result as separate result.
  372. result = rd->best_result;
  373. } else {
  374. // Keep this result as separate result.
  375. result.len = len;
  376. result.contact = p_point_B;
  377. result.normal = contact_rel / len;
  378. result.object = rd->object;
  379. result.shape = rd->shape;
  380. result.local_shape = rd->local_shape;
  381. }
  382. } else {
  383. // Discarding this result.
  384. rd->result_count--;
  385. }
  386. } else if (is_best_result) {
  387. rd->result_count = 1;
  388. }
  389. if (!is_best_result) {
  390. return;
  391. }
  392. rd->best_result.len = len;
  393. rd->best_result.contact = p_point_B;
  394. rd->best_result.normal = contact_rel / len;
  395. rd->best_result.object = rd->object;
  396. rd->best_result.shape = rd->shape;
  397. rd->best_result.local_shape = rd->local_shape;
  398. }
  399. bool GodotPhysicsDirectSpaceState3D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) {
  400. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  401. ERR_FAIL_COND_V(!shape, 0);
  402. real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
  403. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  404. aabb = aabb.grow(margin);
  405. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  406. _RestCallbackData rcd;
  407. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  408. real_t motion_length = p_parameters.motion.length();
  409. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  410. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  411. for (int i = 0; i < amount; i++) {
  412. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  413. continue;
  414. }
  415. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  416. if (p_parameters.exclude.has(col_obj->get_self())) {
  417. continue;
  418. }
  419. int shape_idx = space->intersection_query_subindex_results[i];
  420. rcd.object = col_obj;
  421. rcd.shape = shape_idx;
  422. bool sc = GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin);
  423. if (!sc) {
  424. continue;
  425. }
  426. }
  427. if (rcd.best_result.len == 0 || !rcd.best_result.object) {
  428. return false;
  429. }
  430. r_info->collider_id = rcd.best_result.object->get_instance_id();
  431. r_info->shape = rcd.best_result.shape;
  432. r_info->normal = rcd.best_result.normal;
  433. r_info->point = rcd.best_result.contact;
  434. r_info->rid = rcd.best_result.object->get_self();
  435. if (rcd.best_result.object->get_type() == GodotCollisionObject3D::TYPE_BODY) {
  436. const GodotBody3D *body = static_cast<const GodotBody3D *>(rcd.best_result.object);
  437. Vector3 rel_vec = rcd.best_result.contact - (body->get_transform().origin + body->get_center_of_mass());
  438. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  439. } else {
  440. r_info->linear_velocity = Vector3();
  441. }
  442. return true;
  443. }
  444. Vector3 GodotPhysicsDirectSpaceState3D::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  445. GodotCollisionObject3D *obj = GodotPhysicsServer3D::godot_singleton->area_owner.get_or_null(p_object);
  446. if (!obj) {
  447. obj = GodotPhysicsServer3D::godot_singleton->body_owner.get_or_null(p_object);
  448. }
  449. ERR_FAIL_COND_V(!obj, Vector3());
  450. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  451. real_t min_distance = 1e20;
  452. Vector3 min_point;
  453. bool shapes_found = false;
  454. for (int i = 0; i < obj->get_shape_count(); i++) {
  455. if (obj->is_shape_disabled(i)) {
  456. continue;
  457. }
  458. Transform3D shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  459. GodotShape3D *shape = obj->get_shape(i);
  460. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  461. point = shape_xform.xform(point);
  462. real_t dist = point.distance_to(p_point);
  463. if (dist < min_distance) {
  464. min_distance = dist;
  465. min_point = point;
  466. }
  467. shapes_found = true;
  468. }
  469. if (!shapes_found) {
  470. return obj->get_transform().origin; //no shapes found, use distance to origin.
  471. } else {
  472. return min_point;
  473. }
  474. }
  475. GodotPhysicsDirectSpaceState3D::GodotPhysicsDirectSpaceState3D() {
  476. space = nullptr;
  477. }
  478. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  479. int GodotSpace3D::_cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb) {
  480. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  481. for (int i = 0; i < amount; i++) {
  482. bool keep = true;
  483. if (intersection_query_results[i] == p_body) {
  484. keep = false;
  485. } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_AREA) {
  486. keep = false;
  487. } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  488. keep = false;
  489. } else if (!p_body->collides_with(static_cast<GodotBody3D *>(intersection_query_results[i]))) {
  490. keep = false;
  491. } else if (static_cast<GodotBody3D *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
  492. keep = false;
  493. }
  494. if (!keep) {
  495. if (i < amount - 1) {
  496. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  497. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  498. }
  499. amount--;
  500. i--;
  501. }
  502. }
  503. return amount;
  504. }
  505. bool GodotSpace3D::test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result) {
  506. //give me back regular physics engine logic
  507. //this is madness
  508. //and most people using this function will think
  509. //what it does is simpler than using physics
  510. //this took about a week to get right..
  511. //but is it right? who knows at this point..
  512. ERR_FAIL_INDEX_V(p_parameters.max_collisions, PhysicsServer3D::MotionResult::MAX_COLLISIONS, false);
  513. if (r_result) {
  514. *r_result = PhysicsServer3D::MotionResult();
  515. }
  516. AABB body_aabb;
  517. bool shapes_found = false;
  518. for (int i = 0; i < p_body->get_shape_count(); i++) {
  519. if (p_body->is_shape_disabled(i)) {
  520. continue;
  521. }
  522. if (!shapes_found) {
  523. body_aabb = p_body->get_shape_aabb(i);
  524. shapes_found = true;
  525. } else {
  526. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  527. }
  528. }
  529. if (!shapes_found) {
  530. if (r_result) {
  531. r_result->travel = p_parameters.motion;
  532. }
  533. return false;
  534. }
  535. real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
  536. // Undo the currently transform the physics server is aware of and apply the provided one
  537. body_aabb = p_parameters.from.xform(p_body->get_inv_transform().xform(body_aabb));
  538. body_aabb = body_aabb.grow(margin);
  539. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  540. real_t motion_length = p_parameters.motion.length();
  541. Vector3 motion_normal = p_parameters.motion / motion_length;
  542. Transform3D body_transform = p_parameters.from;
  543. bool recovered = false;
  544. {
  545. //STEP 1, FREE BODY IF STUCK
  546. const int max_results = 32;
  547. int recover_attempts = 4;
  548. Vector3 sr[max_results * 2];
  549. real_t priorities[max_results];
  550. do {
  551. GodotPhysicsServer3D::CollCbkData cbk;
  552. cbk.max = max_results;
  553. cbk.amount = 0;
  554. cbk.ptr = sr;
  555. GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk;
  556. GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk;
  557. int priority_amount = 0;
  558. bool collided = false;
  559. int amount = _cull_aabb_for_body(p_body, body_aabb);
  560. for (int j = 0; j < p_body->get_shape_count(); j++) {
  561. if (p_body->is_shape_disabled(j)) {
  562. continue;
  563. }
  564. Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  565. GodotShape3D *body_shape = p_body->get_shape(j);
  566. for (int i = 0; i < amount; i++) {
  567. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  568. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  569. continue;
  570. }
  571. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  572. continue;
  573. }
  574. int shape_idx = intersection_query_subindex_results[i];
  575. if (GodotCollisionSolver3D::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, margin)) {
  576. collided = cbk.amount > 0;
  577. }
  578. while (cbk.amount > priority_amount) {
  579. priorities[priority_amount] = col_obj->get_collision_priority();
  580. priority_amount++;
  581. }
  582. }
  583. }
  584. if (!collided) {
  585. break;
  586. }
  587. real_t inv_total_weight = 0.0;
  588. for (int i = 0; i < cbk.amount; i++) {
  589. inv_total_weight += priorities[i];
  590. }
  591. inv_total_weight = Math::is_zero_approx(inv_total_weight) ? 1.0 : (real_t)cbk.amount / inv_total_weight;
  592. recovered = true;
  593. Vector3 recover_motion;
  594. for (int i = 0; i < cbk.amount; i++) {
  595. Vector3 a = sr[i * 2 + 0];
  596. Vector3 b = sr[i * 2 + 1];
  597. // Compute plane on b towards a.
  598. Vector3 n = (a - b).normalized();
  599. real_t d = n.dot(b);
  600. // Compute depth on recovered motion.
  601. real_t depth = n.dot(a + recover_motion) - d;
  602. if (depth > min_contact_depth + CMP_EPSILON) {
  603. // Only recover if there is penetration.
  604. recover_motion -= n * (depth - min_contact_depth) * 0.4 * priorities[i] * inv_total_weight;
  605. }
  606. }
  607. if (recover_motion == Vector3()) {
  608. collided = false;
  609. break;
  610. }
  611. body_transform.origin += recover_motion;
  612. body_aabb.position += recover_motion;
  613. recover_attempts--;
  614. } while (recover_attempts);
  615. }
  616. real_t safe = 1.0;
  617. real_t unsafe = 1.0;
  618. int best_shape = -1;
  619. {
  620. // STEP 2 ATTEMPT MOTION
  621. AABB motion_aabb = body_aabb;
  622. motion_aabb.position += p_parameters.motion;
  623. motion_aabb = motion_aabb.merge(body_aabb);
  624. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  625. for (int j = 0; j < p_body->get_shape_count(); j++) {
  626. if (p_body->is_shape_disabled(j)) {
  627. continue;
  628. }
  629. GodotShape3D *body_shape = p_body->get_shape(j);
  630. // Colliding separation rays allows to properly snap to the ground,
  631. // otherwise it's not needed in regular motion.
  632. if (!p_parameters.collide_separation_ray && (body_shape->get_type() == PhysicsServer3D::SHAPE_SEPARATION_RAY)) {
  633. // When slide on slope is on, separation ray shape acts like a regular shape.
  634. if (!static_cast<GodotSeparationRayShape3D *>(body_shape)->get_slide_on_slope()) {
  635. continue;
  636. }
  637. }
  638. Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  639. Transform3D body_shape_xform_inv = body_shape_xform.affine_inverse();
  640. GodotMotionShape3D mshape;
  641. mshape.shape = body_shape;
  642. mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion);
  643. bool stuck = false;
  644. real_t best_safe = 1;
  645. real_t best_unsafe = 1;
  646. for (int i = 0; i < amount; i++) {
  647. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  648. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  649. continue;
  650. }
  651. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  652. continue;
  653. }
  654. int shape_idx = intersection_query_subindex_results[i];
  655. //test initial overlap, does it collide if going all the way?
  656. Vector3 point_A, point_B;
  657. Vector3 sep_axis = motion_normal;
  658. Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  659. //test initial overlap, does it collide if going all the way?
  660. if (GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  661. continue;
  662. }
  663. sep_axis = motion_normal;
  664. if (!GodotCollisionSolver3D::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  665. stuck = true;
  666. break;
  667. }
  668. //just do kinematic solving
  669. real_t low = 0.0;
  670. real_t hi = 1.0;
  671. real_t fraction_coeff = 0.5;
  672. for (int k = 0; k < 8; k++) { //steps should be customizable..
  673. real_t fraction = low + (hi - low) * fraction_coeff;
  674. mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion * fraction);
  675. Vector3 lA, lB;
  676. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  677. bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep);
  678. if (collided) {
  679. hi = fraction;
  680. if ((k == 0) || (low > 0.0)) { // Did it not collide before?
  681. // When alternating or first iteration, use dichotomy.
  682. fraction_coeff = 0.5;
  683. } else {
  684. // When colliding again, converge faster towards low fraction
  685. // for more accurate results with long motions that collide near the start.
  686. fraction_coeff = 0.25;
  687. }
  688. } else {
  689. point_A = lA;
  690. point_B = lB;
  691. low = fraction;
  692. if ((k == 0) || (hi < 1.0)) { // Did it collide before?
  693. // When alternating or first iteration, use dichotomy.
  694. fraction_coeff = 0.5;
  695. } else {
  696. // When not colliding again, converge faster towards high fraction
  697. // for more accurate results with long motions that collide near the end.
  698. fraction_coeff = 0.75;
  699. }
  700. }
  701. }
  702. if (low < best_safe) {
  703. best_safe = low;
  704. best_unsafe = hi;
  705. }
  706. }
  707. if (stuck) {
  708. safe = 0;
  709. unsafe = 0;
  710. best_shape = j; //sadly it's the best
  711. break;
  712. }
  713. if (best_safe == 1.0) {
  714. continue;
  715. }
  716. if (best_safe < safe) {
  717. safe = best_safe;
  718. unsafe = best_unsafe;
  719. best_shape = j;
  720. }
  721. }
  722. }
  723. bool collided = false;
  724. if ((p_parameters.recovery_as_collision && recovered) || (safe < 1)) {
  725. if (safe >= 1) {
  726. best_shape = -1; //no best shape with cast, reset to -1
  727. }
  728. //it collided, let's get the rest info in unsafe advance
  729. Transform3D ugt = body_transform;
  730. ugt.origin += p_parameters.motion * unsafe;
  731. _RestResultData results[PhysicsServer3D::MotionResult::MAX_COLLISIONS];
  732. _RestCallbackData rcd;
  733. if (p_parameters.max_collisions > 1) {
  734. rcd.max_results = p_parameters.max_collisions;
  735. rcd.other_results = results;
  736. }
  737. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  738. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  739. body_aabb.position += p_parameters.motion * unsafe;
  740. int amount = _cull_aabb_for_body(p_body, body_aabb);
  741. int from_shape = best_shape != -1 ? best_shape : 0;
  742. int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
  743. for (int j = from_shape; j < to_shape; j++) {
  744. if (p_body->is_shape_disabled(j)) {
  745. continue;
  746. }
  747. Transform3D body_shape_xform = ugt * p_body->get_shape_transform(j);
  748. GodotShape3D *body_shape = p_body->get_shape(j);
  749. for (int i = 0; i < amount; i++) {
  750. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  751. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  752. continue;
  753. }
  754. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  755. continue;
  756. }
  757. int shape_idx = intersection_query_subindex_results[i];
  758. rcd.object = col_obj;
  759. rcd.shape = shape_idx;
  760. bool sc = GodotCollisionSolver3D::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin);
  761. if (!sc) {
  762. continue;
  763. }
  764. }
  765. }
  766. if (rcd.result_count > 0) {
  767. if (r_result) {
  768. for (int collision_index = 0; collision_index < rcd.result_count; ++collision_index) {
  769. const _RestResultData &result = (collision_index > 0) ? rcd.other_results[collision_index - 1] : rcd.best_result;
  770. PhysicsServer3D::MotionCollision &collision = r_result->collisions[collision_index];
  771. collision.collider = result.object->get_self();
  772. collision.collider_id = result.object->get_instance_id();
  773. collision.collider_shape = result.shape;
  774. collision.local_shape = result.local_shape;
  775. collision.normal = result.normal;
  776. collision.position = result.contact;
  777. collision.depth = result.len;
  778. const GodotBody3D *body = static_cast<const GodotBody3D *>(result.object);
  779. Vector3 rel_vec = result.contact - (body->get_transform().origin + body->get_center_of_mass());
  780. collision.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  781. }
  782. r_result->travel = safe * p_parameters.motion;
  783. r_result->remainder = p_parameters.motion - safe * p_parameters.motion;
  784. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  785. r_result->collision_safe_fraction = safe;
  786. r_result->collision_unsafe_fraction = unsafe;
  787. r_result->collision_count = rcd.result_count;
  788. r_result->collision_depth = rcd.best_result.len;
  789. }
  790. collided = true;
  791. }
  792. }
  793. if (!collided && r_result) {
  794. r_result->travel = p_parameters.motion;
  795. r_result->remainder = Vector3();
  796. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  797. r_result->collision_safe_fraction = 1.0;
  798. r_result->collision_unsafe_fraction = 1.0;
  799. r_result->collision_depth = 0.0;
  800. }
  801. return collided;
  802. }
  803. // Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree.
  804. void *GodotSpace3D::_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self) {
  805. GodotCollisionObject3D::Type type_A = A->get_type();
  806. GodotCollisionObject3D::Type type_B = B->get_type();
  807. if (type_A > type_B) {
  808. SWAP(A, B);
  809. SWAP(p_subindex_A, p_subindex_B);
  810. SWAP(type_A, type_B);
  811. }
  812. GodotSpace3D *self = static_cast<GodotSpace3D *>(p_self);
  813. self->collision_pairs++;
  814. if (type_A == GodotCollisionObject3D::TYPE_AREA) {
  815. GodotArea3D *area = static_cast<GodotArea3D *>(A);
  816. if (type_B == GodotCollisionObject3D::TYPE_AREA) {
  817. GodotArea3D *area_b = static_cast<GodotArea3D *>(B);
  818. GodotArea2Pair3D *area2_pair = memnew(GodotArea2Pair3D(area_b, p_subindex_B, area, p_subindex_A));
  819. return area2_pair;
  820. } else if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  821. GodotSoftBody3D *softbody = static_cast<GodotSoftBody3D *>(B);
  822. GodotAreaSoftBodyPair3D *soft_area_pair = memnew(GodotAreaSoftBodyPair3D(softbody, p_subindex_B, area, p_subindex_A));
  823. return soft_area_pair;
  824. } else {
  825. GodotBody3D *body = static_cast<GodotBody3D *>(B);
  826. GodotAreaPair3D *area_pair = memnew(GodotAreaPair3D(body, p_subindex_B, area, p_subindex_A));
  827. return area_pair;
  828. }
  829. } else if (type_A == GodotCollisionObject3D::TYPE_BODY) {
  830. if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  831. GodotBodySoftBodyPair3D *soft_pair = memnew(GodotBodySoftBodyPair3D(static_cast<GodotBody3D *>(A), p_subindex_A, static_cast<GodotSoftBody3D *>(B)));
  832. return soft_pair;
  833. } else {
  834. GodotBodyPair3D *b = memnew(GodotBodyPair3D(static_cast<GodotBody3D *>(A), p_subindex_A, static_cast<GodotBody3D *>(B), p_subindex_B));
  835. return b;
  836. }
  837. } else {
  838. // Soft Body/Soft Body, not supported.
  839. }
  840. return nullptr;
  841. }
  842. void GodotSpace3D::_broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self) {
  843. if (!p_data) {
  844. return;
  845. }
  846. GodotSpace3D *self = static_cast<GodotSpace3D *>(p_self);
  847. self->collision_pairs--;
  848. GodotConstraint3D *c = static_cast<GodotConstraint3D *>(p_data);
  849. memdelete(c);
  850. }
  851. const SelfList<GodotBody3D>::List &GodotSpace3D::get_active_body_list() const {
  852. return active_list;
  853. }
  854. void GodotSpace3D::body_add_to_active_list(SelfList<GodotBody3D> *p_body) {
  855. active_list.add(p_body);
  856. }
  857. void GodotSpace3D::body_remove_from_active_list(SelfList<GodotBody3D> *p_body) {
  858. active_list.remove(p_body);
  859. }
  860. void GodotSpace3D::body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body) {
  861. mass_properties_update_list.add(p_body);
  862. }
  863. void GodotSpace3D::body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body) {
  864. mass_properties_update_list.remove(p_body);
  865. }
  866. GodotBroadPhase3D *GodotSpace3D::get_broadphase() {
  867. return broadphase;
  868. }
  869. void GodotSpace3D::add_object(GodotCollisionObject3D *p_object) {
  870. ERR_FAIL_COND(objects.has(p_object));
  871. objects.insert(p_object);
  872. }
  873. void GodotSpace3D::remove_object(GodotCollisionObject3D *p_object) {
  874. ERR_FAIL_COND(!objects.has(p_object));
  875. objects.erase(p_object);
  876. }
  877. const HashSet<GodotCollisionObject3D *> &GodotSpace3D::get_objects() const {
  878. return objects;
  879. }
  880. void GodotSpace3D::body_add_to_state_query_list(SelfList<GodotBody3D> *p_body) {
  881. state_query_list.add(p_body);
  882. }
  883. void GodotSpace3D::body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body) {
  884. state_query_list.remove(p_body);
  885. }
  886. void GodotSpace3D::area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area) {
  887. monitor_query_list.add(p_area);
  888. }
  889. void GodotSpace3D::area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area) {
  890. monitor_query_list.remove(p_area);
  891. }
  892. void GodotSpace3D::area_add_to_moved_list(SelfList<GodotArea3D> *p_area) {
  893. area_moved_list.add(p_area);
  894. }
  895. void GodotSpace3D::area_remove_from_moved_list(SelfList<GodotArea3D> *p_area) {
  896. area_moved_list.remove(p_area);
  897. }
  898. const SelfList<GodotArea3D>::List &GodotSpace3D::get_moved_area_list() const {
  899. return area_moved_list;
  900. }
  901. const SelfList<GodotSoftBody3D>::List &GodotSpace3D::get_active_soft_body_list() const {
  902. return active_soft_body_list;
  903. }
  904. void GodotSpace3D::soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body) {
  905. active_soft_body_list.add(p_soft_body);
  906. }
  907. void GodotSpace3D::soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body) {
  908. active_soft_body_list.remove(p_soft_body);
  909. }
  910. void GodotSpace3D::call_queries() {
  911. while (state_query_list.first()) {
  912. GodotBody3D *b = state_query_list.first()->self();
  913. state_query_list.remove(state_query_list.first());
  914. b->call_queries();
  915. }
  916. while (monitor_query_list.first()) {
  917. GodotArea3D *a = monitor_query_list.first()->self();
  918. monitor_query_list.remove(monitor_query_list.first());
  919. a->call_queries();
  920. }
  921. }
  922. void GodotSpace3D::setup() {
  923. contact_debug_count = 0;
  924. while (mass_properties_update_list.first()) {
  925. mass_properties_update_list.first()->self()->update_mass_properties();
  926. mass_properties_update_list.remove(mass_properties_update_list.first());
  927. }
  928. }
  929. void GodotSpace3D::update() {
  930. broadphase->update();
  931. }
  932. void GodotSpace3D::set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value) {
  933. switch (p_param) {
  934. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  935. contact_recycle_radius = p_value;
  936. break;
  937. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  938. contact_max_separation = p_value;
  939. break;
  940. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
  941. contact_max_allowed_penetration = p_value;
  942. break;
  943. case PhysicsServer3D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
  944. contact_bias = p_value;
  945. break;
  946. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  947. body_linear_velocity_sleep_threshold = p_value;
  948. break;
  949. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  950. body_angular_velocity_sleep_threshold = p_value;
  951. break;
  952. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  953. body_time_to_sleep = p_value;
  954. break;
  955. case PhysicsServer3D::SPACE_PARAM_SOLVER_ITERATIONS:
  956. solver_iterations = p_value;
  957. break;
  958. }
  959. }
  960. real_t GodotSpace3D::get_param(PhysicsServer3D::SpaceParameter p_param) const {
  961. switch (p_param) {
  962. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  963. return contact_recycle_radius;
  964. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  965. return contact_max_separation;
  966. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
  967. return contact_max_allowed_penetration;
  968. case PhysicsServer3D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
  969. return contact_bias;
  970. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  971. return body_linear_velocity_sleep_threshold;
  972. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  973. return body_angular_velocity_sleep_threshold;
  974. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  975. return body_time_to_sleep;
  976. case PhysicsServer3D::SPACE_PARAM_SOLVER_ITERATIONS:
  977. return solver_iterations;
  978. }
  979. return 0;
  980. }
  981. void GodotSpace3D::lock() {
  982. locked = true;
  983. }
  984. void GodotSpace3D::unlock() {
  985. locked = false;
  986. }
  987. bool GodotSpace3D::is_locked() const {
  988. return locked;
  989. }
  990. GodotPhysicsDirectSpaceState3D *GodotSpace3D::get_direct_state() {
  991. return direct_access;
  992. }
  993. GodotSpace3D::GodotSpace3D() {
  994. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
  995. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", Math::deg_to_rad(8.0));
  996. body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
  997. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/time_before_sleep", PropertyInfo(Variant::FLOAT, "physics/3d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  998. solver_iterations = GLOBAL_DEF("physics/3d/solver/solver_iterations", 16);
  999. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/solver/solver_iterations", PropertyInfo(Variant::INT, "physics/3d/solver/solver_iterations", PROPERTY_HINT_RANGE, "1,32,1,or_greater"));
  1000. contact_recycle_radius = GLOBAL_DEF("physics/3d/solver/contact_recycle_radius", 0.01);
  1001. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/solver/contact_recycle_radius", PropertyInfo(Variant::FLOAT, "physics/3d/solver/contact_max_separation", PROPERTY_HINT_RANGE, "0,0.1,0.01,or_greater"));
  1002. contact_max_separation = GLOBAL_DEF("physics/3d/solver/contact_max_separation", 0.05);
  1003. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/solver/contact_max_separation", PropertyInfo(Variant::FLOAT, "physics/3d/solver/contact_max_separation", PROPERTY_HINT_RANGE, "0,0.1,0.01,or_greater"));
  1004. contact_max_allowed_penetration = GLOBAL_DEF("physics/3d/solver/contact_max_allowed_penetration", 0.01);
  1005. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/solver/contact_max_allowed_penetration", PropertyInfo(Variant::FLOAT, "physics/3d/solver/contact_max_allowed_penetration", PROPERTY_HINT_RANGE, "0,0.1,0.01,or_greater"));
  1006. contact_bias = GLOBAL_DEF("physics/3d/solver/default_contact_bias", 0.8);
  1007. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/solver/default_contact_bias", PropertyInfo(Variant::FLOAT, "physics/3d/solver/default_contact_bias", PROPERTY_HINT_RANGE, "0,1,0.01"));
  1008. broadphase = GodotBroadPhase3D::create_func();
  1009. broadphase->set_pair_callback(_broadphase_pair, this);
  1010. broadphase->set_unpair_callback(_broadphase_unpair, this);
  1011. direct_access = memnew(GodotPhysicsDirectSpaceState3D);
  1012. direct_access->space = this;
  1013. }
  1014. GodotSpace3D::~GodotSpace3D() {
  1015. memdelete(broadphase);
  1016. memdelete(direct_access);
  1017. }