godot_space_3d.cpp 42 KB

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