godot_space_3d.cpp 42 KB

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