space_3d_sw.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. /*************************************************************************/
  2. /* space_3d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "space_3d_sw.h"
  31. #include "collision_solver_3d_sw.h"
  32. #include "core/project_settings.h"
  33. #include "physics_server_3d_sw.h"
  34. _FORCE_INLINE_ static bool _can_collide_with(CollisionObject3DSW *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  35. if (!(p_object->get_collision_layer() & p_collision_mask)) {
  36. return false;
  37. }
  38. if (p_object->get_type() == CollisionObject3DSW::TYPE_AREA && !p_collide_with_areas)
  39. return false;
  40. if (p_object->get_type() == CollisionObject3DSW::TYPE_BODY && !p_collide_with_bodies)
  41. return false;
  42. return true;
  43. }
  44. int PhysicsDirectSpaceState3DSW::intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  45. ERR_FAIL_COND_V(space->locked, false);
  46. int amount = space->broadphase->cull_point(p_point, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  47. int cc = 0;
  48. //Transform ai = p_xform.affine_inverse();
  49. for (int i = 0; i < amount; i++) {
  50. if (cc >= p_result_max)
  51. break;
  52. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  53. continue;
  54. //area can't be picked by ray (default)
  55. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  56. continue;
  57. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  58. int shape_idx = space->intersection_query_subindex_results[i];
  59. Transform inv_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  60. inv_xform.affine_invert();
  61. if (!col_obj->get_shape(shape_idx)->intersect_point(inv_xform.xform(p_point)))
  62. continue;
  63. r_results[cc].collider_id = col_obj->get_instance_id();
  64. if (r_results[cc].collider_id.is_valid())
  65. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  66. else
  67. r_results[cc].collider = nullptr;
  68. r_results[cc].rid = col_obj->get_self();
  69. r_results[cc].shape = shape_idx;
  70. cc++;
  71. }
  72. return cc;
  73. }
  74. bool PhysicsDirectSpaceState3DSW::intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_pick_ray) {
  75. ERR_FAIL_COND_V(space->locked, false);
  76. Vector3 begin, end;
  77. Vector3 normal;
  78. begin = p_from;
  79. end = p_to;
  80. normal = (end - begin).normalized();
  81. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  82. //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
  83. bool collided = false;
  84. Vector3 res_point, res_normal;
  85. int res_shape;
  86. const CollisionObject3DSW *res_obj;
  87. real_t min_d = 1e10;
  88. for (int i = 0; i < amount; i++) {
  89. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  90. continue;
  91. if (p_pick_ray && !(space->intersection_query_results[i]->is_ray_pickable()))
  92. continue;
  93. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  94. continue;
  95. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  96. int shape_idx = space->intersection_query_subindex_results[i];
  97. Transform inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  98. Vector3 local_from = inv_xform.xform(begin);
  99. Vector3 local_to = inv_xform.xform(end);
  100. const Shape3DSW *shape = col_obj->get_shape(shape_idx);
  101. Vector3 shape_point, shape_normal;
  102. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
  103. Transform xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  104. shape_point = xform.xform(shape_point);
  105. real_t ld = normal.dot(shape_point);
  106. if (ld < min_d) {
  107. min_d = ld;
  108. res_point = shape_point;
  109. res_normal = inv_xform.basis.xform_inv(shape_normal).normalized();
  110. res_shape = shape_idx;
  111. res_obj = col_obj;
  112. collided = true;
  113. }
  114. }
  115. }
  116. if (!collided)
  117. return false;
  118. r_result.collider_id = res_obj->get_instance_id();
  119. if (r_result.collider_id.is_valid())
  120. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  121. else
  122. r_result.collider = nullptr;
  123. r_result.normal = res_normal;
  124. r_result.position = res_point;
  125. r_result.rid = res_obj->get_self();
  126. r_result.shape = res_shape;
  127. return true;
  128. }
  129. int PhysicsDirectSpaceState3DSW::intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  130. if (p_result_max <= 0)
  131. return 0;
  132. Shape3DSW *shape = static_cast<PhysicsServer3DSW *>(PhysicsServer3D::get_singleton())->shape_owner.getornull(p_shape);
  133. ERR_FAIL_COND_V(!shape, 0);
  134. AABB aabb = p_xform.xform(shape->get_aabb());
  135. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  136. int cc = 0;
  137. //Transform ai = p_xform.affine_inverse();
  138. for (int i = 0; i < amount; i++) {
  139. if (cc >= p_result_max)
  140. break;
  141. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  142. continue;
  143. //area can't be picked by ray (default)
  144. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  145. continue;
  146. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  147. int shape_idx = space->intersection_query_subindex_results[i];
  148. if (!CollisionSolver3DSW::solve_static(shape, p_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), nullptr, nullptr, nullptr, p_margin, 0))
  149. continue;
  150. if (r_results) {
  151. r_results[cc].collider_id = col_obj->get_instance_id();
  152. if (r_results[cc].collider_id.is_valid())
  153. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  154. else
  155. r_results[cc].collider = nullptr;
  156. r_results[cc].rid = col_obj->get_self();
  157. r_results[cc].shape = shape_idx;
  158. }
  159. cc++;
  160. }
  161. return cc;
  162. }
  163. bool PhysicsDirectSpaceState3DSW::cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, ShapeRestInfo *r_info) {
  164. Shape3DSW *shape = static_cast<PhysicsServer3DSW *>(PhysicsServer3D::get_singleton())->shape_owner.getornull(p_shape);
  165. ERR_FAIL_COND_V(!shape, false);
  166. AABB aabb = p_xform.xform(shape->get_aabb());
  167. aabb = aabb.merge(AABB(aabb.position + p_motion, aabb.size)); //motion
  168. aabb = aabb.grow(p_margin);
  169. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  170. real_t best_safe = 1;
  171. real_t best_unsafe = 1;
  172. Transform xform_inv = p_xform.affine_inverse();
  173. MotionShape3DSW mshape;
  174. mshape.shape = shape;
  175. mshape.motion = xform_inv.basis.xform(p_motion);
  176. bool best_first = true;
  177. Vector3 closest_A, closest_B;
  178. for (int i = 0; i < amount; i++) {
  179. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  180. continue;
  181. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  182. continue; //ignore excluded
  183. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  184. int shape_idx = space->intersection_query_subindex_results[i];
  185. Vector3 point_A, point_B;
  186. Vector3 sep_axis = p_motion.normalized();
  187. Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  188. //test initial overlap, does it collide if going all the way?
  189. if (CollisionSolver3DSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  190. continue;
  191. }
  192. //test initial overlap
  193. sep_axis = p_motion.normalized();
  194. if (!CollisionSolver3DSW::solve_distance(shape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  195. return false;
  196. }
  197. //just do kinematic solving
  198. real_t low = 0;
  199. real_t hi = 1;
  200. Vector3 mnormal = p_motion.normalized();
  201. for (int j = 0; j < 8; j++) { //steps should be customizable..
  202. real_t ofs = (low + hi) * 0.5;
  203. Vector3 sep = mnormal; //important optimization for this to work fast enough
  204. mshape.motion = xform_inv.basis.xform(p_motion * ofs);
  205. Vector3 lA, lB;
  206. bool collided = !CollisionSolver3DSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep);
  207. if (collided) {
  208. hi = ofs;
  209. } else {
  210. point_A = lA;
  211. point_B = lB;
  212. low = ofs;
  213. }
  214. }
  215. if (low < best_safe) {
  216. best_first = true; //force reset
  217. best_safe = low;
  218. best_unsafe = hi;
  219. }
  220. if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) {
  221. closest_A = point_A;
  222. closest_B = point_B;
  223. r_info->collider_id = col_obj->get_instance_id();
  224. r_info->rid = col_obj->get_self();
  225. r_info->shape = shape_idx;
  226. r_info->point = closest_B;
  227. r_info->normal = (closest_A - closest_B).normalized();
  228. best_first = false;
  229. if (col_obj->get_type() == CollisionObject3DSW::TYPE_BODY) {
  230. const Body3DSW *body = static_cast<const Body3DSW *>(col_obj);
  231. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - closest_B);
  232. }
  233. }
  234. }
  235. p_closest_safe = best_safe;
  236. p_closest_unsafe = best_unsafe;
  237. return true;
  238. }
  239. bool PhysicsDirectSpaceState3DSW::collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  240. if (p_result_max <= 0)
  241. return false;
  242. Shape3DSW *shape = static_cast<PhysicsServer3DSW *>(PhysicsServer3D::get_singleton())->shape_owner.getornull(p_shape);
  243. ERR_FAIL_COND_V(!shape, 0);
  244. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  245. aabb = aabb.grow(p_margin);
  246. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  247. bool collided = false;
  248. r_result_count = 0;
  249. PhysicsServer3DSW::CollCbkData cbk;
  250. cbk.max = p_result_max;
  251. cbk.amount = 0;
  252. cbk.ptr = r_results;
  253. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;
  254. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  255. for (int i = 0; i < amount; i++) {
  256. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  257. continue;
  258. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  259. int shape_idx = space->intersection_query_subindex_results[i];
  260. if (p_exclude.has(col_obj->get_self())) {
  261. continue;
  262. }
  263. if (CollisionSolver3DSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_margin)) {
  264. collided = true;
  265. }
  266. }
  267. r_result_count = cbk.amount;
  268. return collided;
  269. }
  270. struct _RestCallbackData {
  271. const CollisionObject3DSW *object;
  272. const CollisionObject3DSW *best_object;
  273. int shape;
  274. int best_shape;
  275. Vector3 best_contact;
  276. Vector3 best_normal;
  277. real_t best_len;
  278. real_t min_allowed_depth;
  279. };
  280. static void _rest_cbk_result(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  281. _RestCallbackData *rd = (_RestCallbackData *)p_userdata;
  282. Vector3 contact_rel = p_point_B - p_point_A;
  283. real_t len = contact_rel.length();
  284. if (len < rd->min_allowed_depth)
  285. return;
  286. if (len <= rd->best_len)
  287. return;
  288. rd->best_len = len;
  289. rd->best_contact = p_point_B;
  290. rd->best_normal = contact_rel / len;
  291. rd->best_object = rd->object;
  292. rd->best_shape = rd->shape;
  293. }
  294. bool PhysicsDirectSpaceState3DSW::rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  295. Shape3DSW *shape = static_cast<PhysicsServer3DSW *>(PhysicsServer3D::get_singleton())->shape_owner.getornull(p_shape);
  296. ERR_FAIL_COND_V(!shape, 0);
  297. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  298. aabb = aabb.grow(p_margin);
  299. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  300. _RestCallbackData rcd;
  301. rcd.best_len = 0;
  302. rcd.best_object = nullptr;
  303. rcd.best_shape = 0;
  304. rcd.min_allowed_depth = space->test_motion_min_contact_depth;
  305. for (int i = 0; i < amount; i++) {
  306. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  307. continue;
  308. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  309. int shape_idx = space->intersection_query_subindex_results[i];
  310. if (p_exclude.has(col_obj->get_self()))
  311. continue;
  312. rcd.object = col_obj;
  313. rcd.shape = shape_idx;
  314. bool sc = CollisionSolver3DSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, p_margin);
  315. if (!sc)
  316. continue;
  317. }
  318. if (rcd.best_len == 0 || !rcd.best_object)
  319. return false;
  320. r_info->collider_id = rcd.best_object->get_instance_id();
  321. r_info->shape = rcd.best_shape;
  322. r_info->normal = rcd.best_normal;
  323. r_info->point = rcd.best_contact;
  324. r_info->rid = rcd.best_object->get_self();
  325. if (rcd.best_object->get_type() == CollisionObject3DSW::TYPE_BODY) {
  326. const Body3DSW *body = static_cast<const Body3DSW *>(rcd.best_object);
  327. r_info->linear_velocity = body->get_linear_velocity() +
  328. (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  329. } else {
  330. r_info->linear_velocity = Vector3();
  331. }
  332. return true;
  333. }
  334. Vector3 PhysicsDirectSpaceState3DSW::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  335. CollisionObject3DSW *obj = PhysicsServer3DSW::singleton->area_owner.getornull(p_object);
  336. if (!obj) {
  337. obj = PhysicsServer3DSW::singleton->body_owner.getornull(p_object);
  338. }
  339. ERR_FAIL_COND_V(!obj, Vector3());
  340. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  341. float min_distance = 1e20;
  342. Vector3 min_point;
  343. bool shapes_found = false;
  344. for (int i = 0; i < obj->get_shape_count(); i++) {
  345. if (obj->is_shape_set_as_disabled(i))
  346. continue;
  347. Transform shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  348. Shape3DSW *shape = obj->get_shape(i);
  349. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  350. point = shape_xform.xform(point);
  351. float dist = point.distance_to(p_point);
  352. if (dist < min_distance) {
  353. min_distance = dist;
  354. min_point = point;
  355. }
  356. shapes_found = true;
  357. }
  358. if (!shapes_found) {
  359. return obj->get_transform().origin; //no shapes found, use distance to origin.
  360. } else {
  361. return min_point;
  362. }
  363. }
  364. PhysicsDirectSpaceState3DSW::PhysicsDirectSpaceState3DSW() {
  365. space = nullptr;
  366. }
  367. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  368. int Space3DSW::_cull_aabb_for_body(Body3DSW *p_body, const AABB &p_aabb) {
  369. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  370. for (int i = 0; i < amount; i++) {
  371. bool keep = true;
  372. if (intersection_query_results[i] == p_body)
  373. keep = false;
  374. else if (intersection_query_results[i]->get_type() == CollisionObject3DSW::TYPE_AREA)
  375. keep = false;
  376. else if ((static_cast<Body3DSW *>(intersection_query_results[i])->test_collision_mask(p_body)) == 0)
  377. keep = false;
  378. else if (static_cast<Body3DSW *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self()))
  379. keep = false;
  380. else if (static_cast<Body3DSW *>(intersection_query_results[i])->is_shape_set_as_disabled(intersection_query_subindex_results[i]))
  381. keep = false;
  382. if (!keep) {
  383. if (i < amount - 1) {
  384. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  385. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  386. }
  387. amount--;
  388. i--;
  389. }
  390. }
  391. return amount;
  392. }
  393. int Space3DSW::test_body_ray_separation(Body3DSW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer3D::SeparationResult *r_results, int p_result_max, real_t p_margin) {
  394. AABB body_aabb;
  395. bool shapes_found = false;
  396. for (int i = 0; i < p_body->get_shape_count(); i++) {
  397. if (p_body->is_shape_set_as_disabled(i))
  398. continue;
  399. if (!shapes_found) {
  400. body_aabb = p_body->get_shape_aabb(i);
  401. shapes_found = true;
  402. } else {
  403. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  404. }
  405. }
  406. if (!shapes_found) {
  407. return 0;
  408. }
  409. // Undo the currently transform the physics server is aware of and apply the provided one
  410. body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
  411. body_aabb = body_aabb.grow(p_margin);
  412. Transform body_transform = p_transform;
  413. for (int i = 0; i < p_result_max; i++) {
  414. //reset results
  415. r_results[i].collision_depth = 0;
  416. }
  417. int rays_found = 0;
  418. {
  419. // raycast AND separate
  420. const int max_results = 32;
  421. int recover_attempts = 4;
  422. Vector3 sr[max_results * 2];
  423. PhysicsServer3DSW::CollCbkData cbk;
  424. cbk.max = max_results;
  425. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  426. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;
  427. do {
  428. Vector3 recover_motion;
  429. bool collided = false;
  430. int amount = _cull_aabb_for_body(p_body, body_aabb);
  431. for (int j = 0; j < p_body->get_shape_count(); j++) {
  432. if (p_body->is_shape_set_as_disabled(j))
  433. continue;
  434. Shape3DSW *body_shape = p_body->get_shape(j);
  435. if (body_shape->get_type() != PhysicsServer3D::SHAPE_RAY)
  436. continue;
  437. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  438. for (int i = 0; i < amount; i++) {
  439. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  440. int shape_idx = intersection_query_subindex_results[i];
  441. cbk.amount = 0;
  442. cbk.ptr = sr;
  443. if (CollisionObject3DSW::TYPE_BODY == col_obj->get_type()) {
  444. const Body3DSW *b = static_cast<const Body3DSW *>(col_obj);
  445. if (p_infinite_inertia && PhysicsServer3D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer3D::BODY_MODE_KINEMATIC != b->get_mode()) {
  446. continue;
  447. }
  448. }
  449. Shape3DSW *against_shape = col_obj->get_shape(shape_idx);
  450. if (CollisionSolver3DSW::solve_static(body_shape, body_shape_xform, against_shape, col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_margin)) {
  451. if (cbk.amount > 0) {
  452. collided = true;
  453. }
  454. int ray_index = -1; //reuse shape
  455. for (int k = 0; k < rays_found; k++) {
  456. if (r_results[k].collision_local_shape == j) {
  457. ray_index = k;
  458. }
  459. }
  460. if (ray_index == -1 && rays_found < p_result_max) {
  461. ray_index = rays_found;
  462. rays_found++;
  463. }
  464. if (ray_index != -1) {
  465. PhysicsServer3D::SeparationResult &result = r_results[ray_index];
  466. for (int k = 0; k < cbk.amount; k++) {
  467. Vector3 a = sr[k * 2 + 0];
  468. Vector3 b = sr[k * 2 + 1];
  469. recover_motion += (b - a) * 0.4;
  470. float depth = a.distance_to(b);
  471. if (depth > result.collision_depth) {
  472. result.collision_depth = depth;
  473. result.collision_point = b;
  474. result.collision_normal = (b - a).normalized();
  475. result.collision_local_shape = j;
  476. result.collider = col_obj->get_self();
  477. result.collider_id = col_obj->get_instance_id();
  478. result.collider_shape = shape_idx;
  479. //result.collider_metadata = col_obj->get_shape_metadata(shape_idx);
  480. if (col_obj->get_type() == CollisionObject3DSW::TYPE_BODY) {
  481. Body3DSW *body = (Body3DSW *)col_obj;
  482. Vector3 rel_vec = b - body->get_transform().get_origin();
  483. //result.collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  484. result.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rel_vec); // * mPos);
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. }
  492. if (!collided || recover_motion == Vector3()) {
  493. break;
  494. }
  495. body_transform.origin += recover_motion;
  496. body_aabb.position += recover_motion;
  497. recover_attempts--;
  498. } while (recover_attempts);
  499. }
  500. //optimize results (remove non colliding)
  501. for (int i = 0; i < rays_found; i++) {
  502. if (r_results[i].collision_depth == 0) {
  503. rays_found--;
  504. SWAP(r_results[i], r_results[rays_found]);
  505. }
  506. }
  507. r_recover_motion = body_transform.origin - p_transform.origin;
  508. return rays_found;
  509. }
  510. bool Space3DSW::test_body_motion(Body3DSW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer3D::MotionResult *r_result, bool p_exclude_raycast_shapes) {
  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. if (r_result) {
  518. r_result->collider_id = ObjectID();
  519. r_result->collider_shape = 0;
  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_set_as_disabled(i))
  525. continue;
  526. if (!shapes_found) {
  527. body_aabb = p_body->get_shape_aabb(i);
  528. shapes_found = true;
  529. } else {
  530. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  531. }
  532. }
  533. if (!shapes_found) {
  534. if (r_result) {
  535. *r_result = PhysicsServer3D::MotionResult();
  536. r_result->motion = p_motion;
  537. }
  538. return false;
  539. }
  540. // Undo the currently transform the physics server is aware of and apply the provided one
  541. body_aabb = p_from.xform(p_body->get_inv_transform().xform(body_aabb));
  542. body_aabb = body_aabb.grow(p_margin);
  543. Transform body_transform = p_from;
  544. {
  545. //STEP 1, FREE BODY IF STUCK
  546. const int max_results = 32;
  547. int recover_attempts = 4;
  548. Vector3 sr[max_results * 2];
  549. do {
  550. PhysicsServer3DSW::CollCbkData cbk;
  551. cbk.max = max_results;
  552. cbk.amount = 0;
  553. cbk.ptr = sr;
  554. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  555. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;
  556. bool collided = false;
  557. int amount = _cull_aabb_for_body(p_body, body_aabb);
  558. for (int j = 0; j < p_body->get_shape_count(); j++) {
  559. if (p_body->is_shape_set_as_disabled(j))
  560. continue;
  561. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  562. Shape3DSW *body_shape = p_body->get_shape(j);
  563. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer3D::SHAPE_RAY) {
  564. continue;
  565. }
  566. for (int i = 0; i < amount; i++) {
  567. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  568. int shape_idx = intersection_query_subindex_results[i];
  569. if (CollisionSolver3DSW::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, p_margin)) {
  570. collided = cbk.amount > 0;
  571. }
  572. }
  573. }
  574. if (!collided) {
  575. break;
  576. }
  577. Vector3 recover_motion;
  578. for (int i = 0; i < cbk.amount; i++) {
  579. Vector3 a = sr[i * 2 + 0];
  580. Vector3 b = sr[i * 2 + 1];
  581. recover_motion += (b - a) * 0.4;
  582. }
  583. if (recover_motion == Vector3()) {
  584. collided = false;
  585. break;
  586. }
  587. body_transform.origin += recover_motion;
  588. body_aabb.position += recover_motion;
  589. recover_attempts--;
  590. } while (recover_attempts);
  591. }
  592. real_t safe = 1.0;
  593. real_t unsafe = 1.0;
  594. int best_shape = -1;
  595. {
  596. // STEP 2 ATTEMPT MOTION
  597. AABB motion_aabb = body_aabb;
  598. motion_aabb.position += p_motion;
  599. motion_aabb = motion_aabb.merge(body_aabb);
  600. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  601. for (int j = 0; j < p_body->get_shape_count(); j++) {
  602. if (p_body->is_shape_set_as_disabled(j))
  603. continue;
  604. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  605. Shape3DSW *body_shape = p_body->get_shape(j);
  606. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer3D::SHAPE_RAY) {
  607. continue;
  608. }
  609. Transform body_shape_xform_inv = body_shape_xform.affine_inverse();
  610. MotionShape3DSW mshape;
  611. mshape.shape = body_shape;
  612. mshape.motion = body_shape_xform_inv.basis.xform(p_motion);
  613. bool stuck = false;
  614. real_t best_safe = 1;
  615. real_t best_unsafe = 1;
  616. for (int i = 0; i < amount; i++) {
  617. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  618. int shape_idx = intersection_query_subindex_results[i];
  619. //test initial overlap, does it collide if going all the way?
  620. Vector3 point_A, point_B;
  621. Vector3 sep_axis = p_motion.normalized();
  622. Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  623. //test initial overlap, does it collide if going all the way?
  624. if (CollisionSolver3DSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  625. continue;
  626. }
  627. sep_axis = p_motion.normalized();
  628. if (!CollisionSolver3DSW::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  629. stuck = true;
  630. break;
  631. }
  632. //just do kinematic solving
  633. real_t low = 0;
  634. real_t hi = 1;
  635. Vector3 mnormal = p_motion.normalized();
  636. for (int k = 0; k < 8; k++) { //steps should be customizable..
  637. real_t ofs = (low + hi) * 0.5;
  638. Vector3 sep = mnormal; //important optimization for this to work fast enough
  639. mshape.motion = body_shape_xform_inv.basis.xform(p_motion * ofs);
  640. Vector3 lA, lB;
  641. bool collided = !CollisionSolver3DSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep);
  642. if (collided) {
  643. hi = ofs;
  644. } else {
  645. point_A = lA;
  646. point_B = lB;
  647. low = ofs;
  648. }
  649. }
  650. if (low < best_safe) {
  651. best_safe = low;
  652. best_unsafe = hi;
  653. }
  654. }
  655. if (stuck) {
  656. safe = 0;
  657. unsafe = 0;
  658. best_shape = j; //sadly it's the best
  659. break;
  660. }
  661. if (best_safe == 1.0) {
  662. continue;
  663. }
  664. if (best_safe < safe) {
  665. safe = best_safe;
  666. unsafe = best_unsafe;
  667. best_shape = j;
  668. }
  669. }
  670. }
  671. bool collided = false;
  672. if (safe >= 1) {
  673. //not collided
  674. collided = false;
  675. if (r_result) {
  676. r_result->motion = p_motion;
  677. r_result->remainder = Vector3();
  678. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  679. }
  680. } else {
  681. //it collided, let's get the rest info in unsafe advance
  682. Transform ugt = body_transform;
  683. ugt.origin += p_motion * unsafe;
  684. _RestCallbackData rcd;
  685. rcd.best_len = 0;
  686. rcd.best_object = nullptr;
  687. rcd.best_shape = 0;
  688. rcd.min_allowed_depth = test_motion_min_contact_depth;
  689. Transform body_shape_xform = ugt * p_body->get_shape_transform(best_shape);
  690. Shape3DSW *body_shape = p_body->get_shape(best_shape);
  691. body_aabb.position += p_motion * unsafe;
  692. int amount = _cull_aabb_for_body(p_body, body_aabb);
  693. for (int i = 0; i < amount; i++) {
  694. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  695. int shape_idx = intersection_query_subindex_results[i];
  696. rcd.object = col_obj;
  697. rcd.shape = shape_idx;
  698. bool sc = CollisionSolver3DSW::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, p_margin);
  699. if (!sc)
  700. continue;
  701. }
  702. if (rcd.best_len != 0) {
  703. if (r_result) {
  704. r_result->collider = rcd.best_object->get_self();
  705. r_result->collider_id = rcd.best_object->get_instance_id();
  706. r_result->collider_shape = rcd.best_shape;
  707. r_result->collision_local_shape = best_shape;
  708. r_result->collision_normal = rcd.best_normal;
  709. r_result->collision_point = rcd.best_contact;
  710. //r_result->collider_metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  711. const Body3DSW *body = static_cast<const Body3DSW *>(rcd.best_object);
  712. //Vector3 rel_vec = r_result->collision_point - body->get_transform().get_origin();
  713. // r_result->collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  714. r_result->collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  715. r_result->motion = safe * p_motion;
  716. r_result->remainder = p_motion - safe * p_motion;
  717. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  718. }
  719. collided = true;
  720. } else {
  721. if (r_result) {
  722. r_result->motion = p_motion;
  723. r_result->remainder = Vector3();
  724. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  725. }
  726. collided = false;
  727. }
  728. }
  729. return collided;
  730. }
  731. void *Space3DSW::_broadphase_pair(CollisionObject3DSW *A, int p_subindex_A, CollisionObject3DSW *B, int p_subindex_B, void *p_self) {
  732. CollisionObject3DSW::Type type_A = A->get_type();
  733. CollisionObject3DSW::Type type_B = B->get_type();
  734. if (type_A > type_B) {
  735. SWAP(A, B);
  736. SWAP(p_subindex_A, p_subindex_B);
  737. SWAP(type_A, type_B);
  738. }
  739. Space3DSW *self = (Space3DSW *)p_self;
  740. self->collision_pairs++;
  741. if (type_A == CollisionObject3DSW::TYPE_AREA) {
  742. Area3DSW *area = static_cast<Area3DSW *>(A);
  743. if (type_B == CollisionObject3DSW::TYPE_AREA) {
  744. Area3DSW *area_b = static_cast<Area3DSW *>(B);
  745. Area2Pair3DSW *area2_pair = memnew(Area2Pair3DSW(area_b, p_subindex_B, area, p_subindex_A));
  746. return area2_pair;
  747. } else {
  748. Body3DSW *body = static_cast<Body3DSW *>(B);
  749. AreaPair3DSW *area_pair = memnew(AreaPair3DSW(body, p_subindex_B, area, p_subindex_A));
  750. return area_pair;
  751. }
  752. } else {
  753. BodyPair3DSW *b = memnew(BodyPair3DSW((Body3DSW *)A, p_subindex_A, (Body3DSW *)B, p_subindex_B));
  754. return b;
  755. }
  756. return nullptr;
  757. }
  758. void Space3DSW::_broadphase_unpair(CollisionObject3DSW *A, int p_subindex_A, CollisionObject3DSW *B, int p_subindex_B, void *p_data, void *p_self) {
  759. Space3DSW *self = (Space3DSW *)p_self;
  760. self->collision_pairs--;
  761. Constraint3DSW *c = (Constraint3DSW *)p_data;
  762. memdelete(c);
  763. }
  764. const SelfList<Body3DSW>::List &Space3DSW::get_active_body_list() const {
  765. return active_list;
  766. }
  767. void Space3DSW::body_add_to_active_list(SelfList<Body3DSW> *p_body) {
  768. active_list.add(p_body);
  769. }
  770. void Space3DSW::body_remove_from_active_list(SelfList<Body3DSW> *p_body) {
  771. active_list.remove(p_body);
  772. }
  773. void Space3DSW::body_add_to_inertia_update_list(SelfList<Body3DSW> *p_body) {
  774. inertia_update_list.add(p_body);
  775. }
  776. void Space3DSW::body_remove_from_inertia_update_list(SelfList<Body3DSW> *p_body) {
  777. inertia_update_list.remove(p_body);
  778. }
  779. BroadPhase3DSW *Space3DSW::get_broadphase() {
  780. return broadphase;
  781. }
  782. void Space3DSW::add_object(CollisionObject3DSW *p_object) {
  783. ERR_FAIL_COND(objects.has(p_object));
  784. objects.insert(p_object);
  785. }
  786. void Space3DSW::remove_object(CollisionObject3DSW *p_object) {
  787. ERR_FAIL_COND(!objects.has(p_object));
  788. objects.erase(p_object);
  789. }
  790. const Set<CollisionObject3DSW *> &Space3DSW::get_objects() const {
  791. return objects;
  792. }
  793. void Space3DSW::body_add_to_state_query_list(SelfList<Body3DSW> *p_body) {
  794. state_query_list.add(p_body);
  795. }
  796. void Space3DSW::body_remove_from_state_query_list(SelfList<Body3DSW> *p_body) {
  797. state_query_list.remove(p_body);
  798. }
  799. void Space3DSW::area_add_to_monitor_query_list(SelfList<Area3DSW> *p_area) {
  800. monitor_query_list.add(p_area);
  801. }
  802. void Space3DSW::area_remove_from_monitor_query_list(SelfList<Area3DSW> *p_area) {
  803. monitor_query_list.remove(p_area);
  804. }
  805. void Space3DSW::area_add_to_moved_list(SelfList<Area3DSW> *p_area) {
  806. area_moved_list.add(p_area);
  807. }
  808. void Space3DSW::area_remove_from_moved_list(SelfList<Area3DSW> *p_area) {
  809. area_moved_list.remove(p_area);
  810. }
  811. const SelfList<Area3DSW>::List &Space3DSW::get_moved_area_list() const {
  812. return area_moved_list;
  813. }
  814. void Space3DSW::call_queries() {
  815. while (state_query_list.first()) {
  816. Body3DSW *b = state_query_list.first()->self();
  817. state_query_list.remove(state_query_list.first());
  818. b->call_queries();
  819. }
  820. while (monitor_query_list.first()) {
  821. Area3DSW *a = monitor_query_list.first()->self();
  822. monitor_query_list.remove(monitor_query_list.first());
  823. a->call_queries();
  824. }
  825. }
  826. void Space3DSW::setup() {
  827. contact_debug_count = 0;
  828. while (inertia_update_list.first()) {
  829. inertia_update_list.first()->self()->update_inertias();
  830. inertia_update_list.remove(inertia_update_list.first());
  831. }
  832. }
  833. void Space3DSW::update() {
  834. broadphase->update();
  835. }
  836. void Space3DSW::set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value) {
  837. switch (p_param) {
  838. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  839. contact_recycle_radius = p_value;
  840. break;
  841. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  842. contact_max_separation = p_value;
  843. break;
  844. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  845. contact_max_allowed_penetration = p_value;
  846. break;
  847. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  848. body_linear_velocity_sleep_threshold = p_value;
  849. break;
  850. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  851. body_angular_velocity_sleep_threshold = p_value;
  852. break;
  853. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  854. body_time_to_sleep = p_value;
  855. break;
  856. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  857. body_angular_velocity_damp_ratio = p_value;
  858. break;
  859. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  860. constraint_bias = p_value;
  861. break;
  862. case PhysicsServer3D::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH:
  863. test_motion_min_contact_depth = p_value;
  864. break;
  865. }
  866. }
  867. real_t Space3DSW::get_param(PhysicsServer3D::SpaceParameter p_param) const {
  868. switch (p_param) {
  869. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  870. return contact_recycle_radius;
  871. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  872. return contact_max_separation;
  873. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  874. return contact_max_allowed_penetration;
  875. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  876. return body_linear_velocity_sleep_threshold;
  877. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  878. return body_angular_velocity_sleep_threshold;
  879. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  880. return body_time_to_sleep;
  881. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  882. return body_angular_velocity_damp_ratio;
  883. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  884. return constraint_bias;
  885. case PhysicsServer3D::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH:
  886. return test_motion_min_contact_depth;
  887. }
  888. return 0;
  889. }
  890. void Space3DSW::lock() {
  891. locked = true;
  892. }
  893. void Space3DSW::unlock() {
  894. locked = false;
  895. }
  896. bool Space3DSW::is_locked() const {
  897. return locked;
  898. }
  899. PhysicsDirectSpaceState3DSW *Space3DSW::get_direct_state() {
  900. return direct_access;
  901. }
  902. Space3DSW::Space3DSW() {
  903. collision_pairs = 0;
  904. active_objects = 0;
  905. island_count = 0;
  906. contact_debug_count = 0;
  907. locked = false;
  908. contact_recycle_radius = 0.01;
  909. contact_max_separation = 0.05;
  910. contact_max_allowed_penetration = 0.01;
  911. test_motion_min_contact_depth = 0.00001;
  912. constraint_bias = 0.01;
  913. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
  914. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", (8.0 / 180.0 * Math_PI));
  915. body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
  916. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/time_before_sleep", PropertyInfo(Variant::FLOAT, "physics/3d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  917. body_angular_velocity_damp_ratio = 10;
  918. broadphase = BroadPhase3DSW::create_func();
  919. broadphase->set_pair_callback(_broadphase_pair, this);
  920. broadphase->set_unpair_callback(_broadphase_unpair, this);
  921. area = nullptr;
  922. direct_access = memnew(PhysicsDirectSpaceState3DSW);
  923. direct_access->space = this;
  924. for (int i = 0; i < ELAPSED_TIME_MAX; i++)
  925. elapsed_time[i] = 0;
  926. }
  927. Space3DSW::~Space3DSW() {
  928. memdelete(broadphase);
  929. memdelete(direct_access);
  930. }