2
0

space_sw.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*************************************************************************/
  2. /* space_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_sw.h"
  31. #include "collision_solver_sw.h"
  32. #include "core/project_settings.h"
  33. #include "physics_server_sw.h"
  34. _FORCE_INLINE_ static bool _can_collide_with(CollisionObjectSW *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() == CollisionObjectSW::TYPE_AREA && !p_collide_with_areas)
  39. return false;
  40. if (p_object->get_type() == CollisionObjectSW::TYPE_BODY && !p_collide_with_bodies)
  41. return false;
  42. return true;
  43. }
  44. int PhysicsDirectSpaceStateSW::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, SpaceSW::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 CollisionObjectSW *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 != 0)
  65. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  66. else
  67. r_results[cc].collider = NULL;
  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 PhysicsDirectSpaceStateSW::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, SpaceSW::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 CollisionObjectSW *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 && !(static_cast<CollisionObjectSW *>(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 CollisionObjectSW *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 ShapeSW *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 != 0)
  120. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  121. else
  122. r_result.collider = NULL;
  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 PhysicsDirectSpaceStateSW::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. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(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, SpaceSW::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 CollisionObjectSW *col_obj = space->intersection_query_results[i];
  147. int shape_idx = space->intersection_query_subindex_results[i];
  148. if (!CollisionSolverSW::solve_static(shape, p_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), NULL, NULL, NULL, 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 != 0)
  153. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  154. else
  155. r_results[cc].collider = NULL;
  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 PhysicsDirectSpaceStateSW::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. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(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, SpaceSW::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. MotionShapeSW 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 CollisionObjectSW *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 (CollisionSolverSW::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 (!CollisionSolverSW::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 = !CollisionSolverSW::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() == CollisionObjectSW::TYPE_BODY) {
  230. const BodySW *body = static_cast<const BodySW *>(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 PhysicsDirectSpaceStateSW::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 0;
  242. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(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, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  247. bool collided = false;
  248. r_result_count = 0;
  249. PhysicsServerSW::CollCbkData cbk;
  250. cbk.max = p_result_max;
  251. cbk.amount = 0;
  252. cbk.ptr = r_results;
  253. CollisionSolverSW::CallbackResult cbkres = NULL;
  254. PhysicsServerSW::CollCbkData *cbkptr = NULL;
  255. if (p_result_max > 0) {
  256. cbkptr = &cbk;
  257. cbkres = PhysicsServerSW::_shape_col_cbk;
  258. }
  259. for (int i = 0; i < amount; i++) {
  260. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  261. continue;
  262. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  263. int shape_idx = space->intersection_query_subindex_results[i];
  264. if (p_exclude.has(col_obj->get_self())) {
  265. continue;
  266. }
  267. if (CollisionSolverSW::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, NULL, p_margin)) {
  268. collided = true;
  269. }
  270. }
  271. r_result_count = cbk.amount;
  272. return collided;
  273. }
  274. struct _RestCallbackData {
  275. const CollisionObjectSW *object;
  276. const CollisionObjectSW *best_object;
  277. int shape;
  278. int best_shape;
  279. Vector3 best_contact;
  280. Vector3 best_normal;
  281. real_t best_len;
  282. real_t min_allowed_depth;
  283. };
  284. static void _rest_cbk_result(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  285. _RestCallbackData *rd = (_RestCallbackData *)p_userdata;
  286. Vector3 contact_rel = p_point_B - p_point_A;
  287. real_t len = contact_rel.length();
  288. if (len < rd->min_allowed_depth)
  289. return;
  290. if (len <= rd->best_len)
  291. return;
  292. rd->best_len = len;
  293. rd->best_contact = p_point_B;
  294. rd->best_normal = contact_rel / len;
  295. rd->best_object = rd->object;
  296. rd->best_shape = rd->shape;
  297. }
  298. bool PhysicsDirectSpaceStateSW::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) {
  299. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  300. ERR_FAIL_COND_V(!shape, 0);
  301. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  302. aabb = aabb.grow(p_margin);
  303. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  304. _RestCallbackData rcd;
  305. rcd.best_len = 0;
  306. rcd.best_object = NULL;
  307. rcd.best_shape = 0;
  308. rcd.min_allowed_depth = space->test_motion_min_contact_depth;
  309. for (int i = 0; i < amount; i++) {
  310. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  311. continue;
  312. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  313. int shape_idx = space->intersection_query_subindex_results[i];
  314. if (p_exclude.has(col_obj->get_self()))
  315. continue;
  316. rcd.object = col_obj;
  317. rcd.shape = shape_idx;
  318. bool sc = CollisionSolverSW::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, NULL, p_margin);
  319. if (!sc)
  320. continue;
  321. }
  322. if (rcd.best_len == 0)
  323. return false;
  324. r_info->collider_id = rcd.best_object->get_instance_id();
  325. r_info->shape = rcd.best_shape;
  326. r_info->normal = rcd.best_normal;
  327. r_info->point = rcd.best_contact;
  328. r_info->rid = rcd.best_object->get_self();
  329. if (rcd.best_object->get_type() == CollisionObjectSW::TYPE_BODY) {
  330. const BodySW *body = static_cast<const BodySW *>(rcd.best_object);
  331. r_info->linear_velocity = body->get_linear_velocity() +
  332. (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  333. } else {
  334. r_info->linear_velocity = Vector3();
  335. }
  336. return true;
  337. }
  338. Vector3 PhysicsDirectSpaceStateSW::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  339. CollisionObjectSW *obj = PhysicsServerSW::singleton->area_owner.getornull(p_object);
  340. if (!obj) {
  341. obj = PhysicsServerSW::singleton->body_owner.getornull(p_object);
  342. }
  343. ERR_FAIL_COND_V(!obj, Vector3());
  344. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  345. float min_distance = 1e20;
  346. Vector3 min_point;
  347. bool shapes_found = false;
  348. for (int i = 0; i < obj->get_shape_count(); i++) {
  349. if (obj->is_shape_set_as_disabled(i))
  350. continue;
  351. Transform shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  352. ShapeSW *shape = obj->get_shape(i);
  353. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  354. point = shape_xform.xform(point);
  355. float dist = point.distance_to(p_point);
  356. if (dist < min_distance) {
  357. min_distance = dist;
  358. min_point = point;
  359. }
  360. shapes_found = true;
  361. }
  362. if (!shapes_found) {
  363. return obj->get_transform().origin; //no shapes found, use distance to origin.
  364. } else {
  365. return min_point;
  366. }
  367. }
  368. PhysicsDirectSpaceStateSW::PhysicsDirectSpaceStateSW() {
  369. space = NULL;
  370. }
  371. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  372. int SpaceSW::_cull_aabb_for_body(BodySW *p_body, const AABB &p_aabb) {
  373. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  374. for (int i = 0; i < amount; i++) {
  375. bool keep = true;
  376. if (intersection_query_results[i] == p_body)
  377. keep = false;
  378. else if (intersection_query_results[i]->get_type() == CollisionObjectSW::TYPE_AREA)
  379. keep = false;
  380. else if ((static_cast<BodySW *>(intersection_query_results[i])->test_collision_mask(p_body)) == 0)
  381. keep = false;
  382. else if (static_cast<BodySW *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self()))
  383. keep = false;
  384. else if (static_cast<BodySW *>(intersection_query_results[i])->is_shape_set_as_disabled(intersection_query_subindex_results[i]))
  385. keep = false;
  386. if (!keep) {
  387. if (i < amount - 1) {
  388. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  389. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  390. }
  391. amount--;
  392. i--;
  393. }
  394. }
  395. return amount;
  396. }
  397. int SpaceSW::test_body_ray_separation(BodySW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer::SeparationResult *r_results, int p_result_max, real_t p_margin) {
  398. AABB body_aabb;
  399. bool shapes_found = false;
  400. for (int i = 0; i < p_body->get_shape_count(); i++) {
  401. if (p_body->is_shape_set_as_disabled(i))
  402. continue;
  403. if (!shapes_found) {
  404. body_aabb = p_body->get_shape_aabb(i);
  405. shapes_found = true;
  406. } else {
  407. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  408. }
  409. }
  410. if (!shapes_found) {
  411. return 0;
  412. }
  413. // Undo the currently transform the physics server is aware of and apply the provided one
  414. body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
  415. body_aabb = body_aabb.grow(p_margin);
  416. Transform body_transform = p_transform;
  417. for (int i = 0; i < p_result_max; i++) {
  418. //reset results
  419. r_results[i].collision_depth = 0;
  420. }
  421. int rays_found = 0;
  422. {
  423. // raycast AND separate
  424. const int max_results = 32;
  425. int recover_attempts = 4;
  426. Vector3 sr[max_results * 2];
  427. PhysicsServerSW::CollCbkData cbk;
  428. cbk.max = max_results;
  429. PhysicsServerSW::CollCbkData *cbkptr = &cbk;
  430. CollisionSolverSW::CallbackResult cbkres = PhysicsServerSW::_shape_col_cbk;
  431. do {
  432. Vector3 recover_motion;
  433. bool collided = false;
  434. int amount = _cull_aabb_for_body(p_body, body_aabb);
  435. for (int j = 0; j < p_body->get_shape_count(); j++) {
  436. if (p_body->is_shape_set_as_disabled(j))
  437. continue;
  438. ShapeSW *body_shape = p_body->get_shape(j);
  439. if (body_shape->get_type() != PhysicsServer::SHAPE_RAY)
  440. continue;
  441. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  442. for (int i = 0; i < amount; i++) {
  443. const CollisionObjectSW *col_obj = intersection_query_results[i];
  444. int shape_idx = intersection_query_subindex_results[i];
  445. cbk.amount = 0;
  446. cbk.ptr = sr;
  447. if (CollisionObjectSW::TYPE_BODY == col_obj->get_type()) {
  448. const BodySW *b = static_cast<const BodySW *>(col_obj);
  449. if (p_infinite_inertia && PhysicsServer::BODY_MODE_STATIC != b->get_mode() && PhysicsServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  450. continue;
  451. }
  452. }
  453. ShapeSW *against_shape = col_obj->get_shape(shape_idx);
  454. if (CollisionSolverSW::solve_static(body_shape, body_shape_xform, against_shape, col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, NULL, p_margin)) {
  455. if (cbk.amount > 0) {
  456. collided = true;
  457. }
  458. int ray_index = -1; //reuse shape
  459. for (int k = 0; k < rays_found; k++) {
  460. if (r_results[ray_index].collision_local_shape == j) {
  461. ray_index = k;
  462. }
  463. }
  464. if (ray_index == -1 && rays_found < p_result_max) {
  465. ray_index = rays_found;
  466. rays_found++;
  467. }
  468. if (ray_index != -1) {
  469. PhysicsServer::SeparationResult &result = r_results[ray_index];
  470. for (int k = 0; k < cbk.amount; k++) {
  471. Vector3 a = sr[k * 2 + 0];
  472. Vector3 b = sr[k * 2 + 1];
  473. recover_motion += (b - a) * 0.4;
  474. float depth = a.distance_to(b);
  475. if (depth > result.collision_depth) {
  476. result.collision_depth = depth;
  477. result.collision_point = b;
  478. result.collision_normal = (b - a).normalized();
  479. result.collision_local_shape = j;
  480. result.collider = col_obj->get_self();
  481. result.collider_id = col_obj->get_instance_id();
  482. result.collider_shape = shape_idx;
  483. //result.collider_metadata = col_obj->get_shape_metadata(shape_idx);
  484. if (col_obj->get_type() == CollisionObjectSW::TYPE_BODY) {
  485. BodySW *body = (BodySW *)col_obj;
  486. Vector3 rel_vec = b - body->get_transform().get_origin();
  487. //result.collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  488. result.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rel_vec); // * mPos);
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. }
  496. if (!collided || recover_motion == Vector3()) {
  497. break;
  498. }
  499. body_transform.origin += recover_motion;
  500. body_aabb.position += recover_motion;
  501. recover_attempts--;
  502. } while (recover_attempts);
  503. }
  504. //optimize results (remove non colliding)
  505. for (int i = 0; i < rays_found; i++) {
  506. if (r_results[i].collision_depth == 0) {
  507. rays_found--;
  508. SWAP(r_results[i], r_results[rays_found]);
  509. }
  510. }
  511. r_recover_motion = body_transform.origin - p_transform.origin;
  512. return rays_found;
  513. }
  514. bool SpaceSW::test_body_motion(BodySW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer::MotionResult *r_result, bool p_exclude_raycast_shapes) {
  515. //give me back regular physics engine logic
  516. //this is madness
  517. //and most people using this function will think
  518. //what it does is simpler than using physics
  519. //this took about a week to get right..
  520. //but is it right? who knows at this point..
  521. if (r_result) {
  522. r_result->collider_id = 0;
  523. r_result->collider_shape = 0;
  524. }
  525. AABB body_aabb;
  526. bool shapes_found = false;
  527. for (int i = 0; i < p_body->get_shape_count(); i++) {
  528. if (p_body->is_shape_set_as_disabled(i))
  529. continue;
  530. if (!shapes_found) {
  531. body_aabb = p_body->get_shape_aabb(i);
  532. shapes_found = true;
  533. } else {
  534. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  535. }
  536. }
  537. if (!shapes_found) {
  538. if (r_result) {
  539. *r_result = PhysicsServer::MotionResult();
  540. r_result->motion = p_motion;
  541. }
  542. return false;
  543. }
  544. // Undo the currently transform the physics server is aware of and apply the provided one
  545. body_aabb = p_from.xform(p_body->get_inv_transform().xform(body_aabb));
  546. body_aabb = body_aabb.grow(p_margin);
  547. Transform body_transform = p_from;
  548. {
  549. //STEP 1, FREE BODY IF STUCK
  550. const int max_results = 32;
  551. int recover_attempts = 4;
  552. Vector3 sr[max_results * 2];
  553. do {
  554. PhysicsServerSW::CollCbkData cbk;
  555. cbk.max = max_results;
  556. cbk.amount = 0;
  557. cbk.ptr = sr;
  558. PhysicsServerSW::CollCbkData *cbkptr = &cbk;
  559. CollisionSolverSW::CallbackResult cbkres = PhysicsServerSW::_shape_col_cbk;
  560. bool collided = false;
  561. int amount = _cull_aabb_for_body(p_body, body_aabb);
  562. for (int j = 0; j < p_body->get_shape_count(); j++) {
  563. if (p_body->is_shape_set_as_disabled(j))
  564. continue;
  565. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  566. ShapeSW *body_shape = p_body->get_shape(j);
  567. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer::SHAPE_RAY) {
  568. continue;
  569. }
  570. for (int i = 0; i < amount; i++) {
  571. const CollisionObjectSW *col_obj = intersection_query_results[i];
  572. int shape_idx = intersection_query_subindex_results[i];
  573. if (CollisionSolverSW::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, NULL, p_margin)) {
  574. collided = cbk.amount > 0;
  575. }
  576. }
  577. }
  578. if (!collided) {
  579. break;
  580. }
  581. Vector3 recover_motion;
  582. for (int i = 0; i < cbk.amount; i++) {
  583. Vector3 a = sr[i * 2 + 0];
  584. Vector3 b = sr[i * 2 + 1];
  585. recover_motion += (b - a) * 0.4;
  586. }
  587. if (recover_motion == Vector3()) {
  588. collided = false;
  589. break;
  590. }
  591. body_transform.origin += recover_motion;
  592. body_aabb.position += recover_motion;
  593. recover_attempts--;
  594. } while (recover_attempts);
  595. }
  596. real_t safe = 1.0;
  597. real_t unsafe = 1.0;
  598. int best_shape = -1;
  599. {
  600. // STEP 2 ATTEMPT MOTION
  601. AABB motion_aabb = body_aabb;
  602. motion_aabb.position += p_motion;
  603. motion_aabb = motion_aabb.merge(body_aabb);
  604. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  605. for (int j = 0; j < p_body->get_shape_count(); j++) {
  606. if (p_body->is_shape_set_as_disabled(j))
  607. continue;
  608. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  609. ShapeSW *body_shape = p_body->get_shape(j);
  610. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer::SHAPE_RAY) {
  611. continue;
  612. }
  613. Transform body_shape_xform_inv = body_shape_xform.affine_inverse();
  614. MotionShapeSW mshape;
  615. mshape.shape = body_shape;
  616. mshape.motion = body_shape_xform_inv.basis.xform(p_motion);
  617. bool stuck = false;
  618. real_t best_safe = 1;
  619. real_t best_unsafe = 1;
  620. for (int i = 0; i < amount; i++) {
  621. const CollisionObjectSW *col_obj = intersection_query_results[i];
  622. int shape_idx = intersection_query_subindex_results[i];
  623. //test initial overlap, does it collide if going all the way?
  624. Vector3 point_A, point_B;
  625. Vector3 sep_axis = p_motion.normalized();
  626. Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  627. //test initial overlap, does it collide if going all the way?
  628. if (CollisionSolverSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  629. continue;
  630. }
  631. sep_axis = p_motion.normalized();
  632. if (!CollisionSolverSW::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  633. stuck = true;
  634. break;
  635. }
  636. //just do kinematic solving
  637. real_t low = 0;
  638. real_t hi = 1;
  639. Vector3 mnormal = p_motion.normalized();
  640. for (int k = 0; k < 8; k++) { //steps should be customizable..
  641. real_t ofs = (low + hi) * 0.5;
  642. Vector3 sep = mnormal; //important optimization for this to work fast enough
  643. mshape.motion = body_shape_xform_inv.basis.xform(p_motion * ofs);
  644. Vector3 lA, lB;
  645. bool collided = !CollisionSolverSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep);
  646. if (collided) {
  647. hi = ofs;
  648. } else {
  649. point_A = lA;
  650. point_B = lB;
  651. low = ofs;
  652. }
  653. }
  654. if (low < best_safe) {
  655. best_safe = low;
  656. best_unsafe = hi;
  657. }
  658. }
  659. if (stuck) {
  660. safe = 0;
  661. unsafe = 0;
  662. best_shape = j; //sadly it's the best
  663. break;
  664. }
  665. if (best_safe == 1.0) {
  666. continue;
  667. }
  668. if (best_safe < safe) {
  669. safe = best_safe;
  670. unsafe = best_unsafe;
  671. best_shape = j;
  672. }
  673. }
  674. }
  675. bool collided = false;
  676. if (safe >= 1) {
  677. //not collided
  678. collided = false;
  679. if (r_result) {
  680. r_result->motion = p_motion;
  681. r_result->remainder = Vector3();
  682. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  683. }
  684. } else {
  685. //it collided, let's get the rest info in unsafe advance
  686. Transform ugt = body_transform;
  687. ugt.origin += p_motion * unsafe;
  688. _RestCallbackData rcd;
  689. rcd.best_len = 0;
  690. rcd.best_object = NULL;
  691. rcd.best_shape = 0;
  692. rcd.min_allowed_depth = test_motion_min_contact_depth;
  693. Transform body_shape_xform = ugt * p_body->get_shape_transform(best_shape);
  694. ShapeSW *body_shape = p_body->get_shape(best_shape);
  695. body_aabb.position += p_motion * unsafe;
  696. int amount = _cull_aabb_for_body(p_body, body_aabb);
  697. for (int i = 0; i < amount; i++) {
  698. const CollisionObjectSW *col_obj = intersection_query_results[i];
  699. int shape_idx = intersection_query_subindex_results[i];
  700. rcd.object = col_obj;
  701. rcd.shape = shape_idx;
  702. bool sc = CollisionSolverSW::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, NULL, p_margin);
  703. if (!sc)
  704. continue;
  705. }
  706. if (rcd.best_len != 0) {
  707. if (r_result) {
  708. r_result->collider = rcd.best_object->get_self();
  709. r_result->collider_id = rcd.best_object->get_instance_id();
  710. r_result->collider_shape = rcd.best_shape;
  711. r_result->collision_local_shape = best_shape;
  712. r_result->collision_normal = rcd.best_normal;
  713. r_result->collision_point = rcd.best_contact;
  714. //r_result->collider_metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  715. const BodySW *body = static_cast<const BodySW *>(rcd.best_object);
  716. //Vector3 rel_vec = r_result->collision_point - body->get_transform().get_origin();
  717. // r_result->collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  718. r_result->collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  719. r_result->motion = safe * p_motion;
  720. r_result->remainder = p_motion - safe * p_motion;
  721. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  722. }
  723. collided = true;
  724. } else {
  725. if (r_result) {
  726. r_result->motion = p_motion;
  727. r_result->remainder = Vector3();
  728. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  729. }
  730. collided = false;
  731. }
  732. }
  733. return collided;
  734. }
  735. void *SpaceSW::_broadphase_pair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_self) {
  736. CollisionObjectSW::Type type_A = A->get_type();
  737. CollisionObjectSW::Type type_B = B->get_type();
  738. if (type_A > type_B) {
  739. SWAP(A, B);
  740. SWAP(p_subindex_A, p_subindex_B);
  741. SWAP(type_A, type_B);
  742. }
  743. SpaceSW *self = (SpaceSW *)p_self;
  744. self->collision_pairs++;
  745. if (type_A == CollisionObjectSW::TYPE_AREA) {
  746. AreaSW *area = static_cast<AreaSW *>(A);
  747. if (type_B == CollisionObjectSW::TYPE_AREA) {
  748. AreaSW *area_b = static_cast<AreaSW *>(B);
  749. Area2PairSW *area2_pair = memnew(Area2PairSW(area_b, p_subindex_B, area, p_subindex_A));
  750. return area2_pair;
  751. } else {
  752. BodySW *body = static_cast<BodySW *>(B);
  753. AreaPairSW *area_pair = memnew(AreaPairSW(body, p_subindex_B, area, p_subindex_A));
  754. return area_pair;
  755. }
  756. } else {
  757. BodyPairSW *b = memnew(BodyPairSW((BodySW *)A, p_subindex_A, (BodySW *)B, p_subindex_B));
  758. return b;
  759. }
  760. return NULL;
  761. }
  762. void SpaceSW::_broadphase_unpair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_data, void *p_self) {
  763. SpaceSW *self = (SpaceSW *)p_self;
  764. self->collision_pairs--;
  765. ConstraintSW *c = (ConstraintSW *)p_data;
  766. memdelete(c);
  767. }
  768. const SelfList<BodySW>::List &SpaceSW::get_active_body_list() const {
  769. return active_list;
  770. }
  771. void SpaceSW::body_add_to_active_list(SelfList<BodySW> *p_body) {
  772. active_list.add(p_body);
  773. }
  774. void SpaceSW::body_remove_from_active_list(SelfList<BodySW> *p_body) {
  775. active_list.remove(p_body);
  776. }
  777. void SpaceSW::body_add_to_inertia_update_list(SelfList<BodySW> *p_body) {
  778. inertia_update_list.add(p_body);
  779. }
  780. void SpaceSW::body_remove_from_inertia_update_list(SelfList<BodySW> *p_body) {
  781. inertia_update_list.remove(p_body);
  782. }
  783. BroadPhaseSW *SpaceSW::get_broadphase() {
  784. return broadphase;
  785. }
  786. void SpaceSW::add_object(CollisionObjectSW *p_object) {
  787. ERR_FAIL_COND(objects.has(p_object));
  788. objects.insert(p_object);
  789. }
  790. void SpaceSW::remove_object(CollisionObjectSW *p_object) {
  791. ERR_FAIL_COND(!objects.has(p_object));
  792. objects.erase(p_object);
  793. }
  794. const Set<CollisionObjectSW *> &SpaceSW::get_objects() const {
  795. return objects;
  796. }
  797. void SpaceSW::body_add_to_state_query_list(SelfList<BodySW> *p_body) {
  798. state_query_list.add(p_body);
  799. }
  800. void SpaceSW::body_remove_from_state_query_list(SelfList<BodySW> *p_body) {
  801. state_query_list.remove(p_body);
  802. }
  803. void SpaceSW::area_add_to_monitor_query_list(SelfList<AreaSW> *p_area) {
  804. monitor_query_list.add(p_area);
  805. }
  806. void SpaceSW::area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area) {
  807. monitor_query_list.remove(p_area);
  808. }
  809. void SpaceSW::area_add_to_moved_list(SelfList<AreaSW> *p_area) {
  810. area_moved_list.add(p_area);
  811. }
  812. void SpaceSW::area_remove_from_moved_list(SelfList<AreaSW> *p_area) {
  813. area_moved_list.remove(p_area);
  814. }
  815. const SelfList<AreaSW>::List &SpaceSW::get_moved_area_list() const {
  816. return area_moved_list;
  817. }
  818. void SpaceSW::call_queries() {
  819. while (state_query_list.first()) {
  820. BodySW *b = state_query_list.first()->self();
  821. state_query_list.remove(state_query_list.first());
  822. b->call_queries();
  823. }
  824. while (monitor_query_list.first()) {
  825. AreaSW *a = monitor_query_list.first()->self();
  826. monitor_query_list.remove(monitor_query_list.first());
  827. a->call_queries();
  828. }
  829. }
  830. void SpaceSW::setup() {
  831. contact_debug_count = 0;
  832. while (inertia_update_list.first()) {
  833. inertia_update_list.first()->self()->update_inertias();
  834. inertia_update_list.remove(inertia_update_list.first());
  835. }
  836. }
  837. void SpaceSW::update() {
  838. broadphase->update();
  839. }
  840. void SpaceSW::set_param(PhysicsServer::SpaceParameter p_param, real_t p_value) {
  841. switch (p_param) {
  842. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: contact_recycle_radius = p_value; break;
  843. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: contact_max_separation = p_value; break;
  844. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: contact_max_allowed_penetration = p_value; break;
  845. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: body_linear_velocity_sleep_threshold = p_value; break;
  846. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: body_angular_velocity_sleep_threshold = p_value; break;
  847. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: body_time_to_sleep = p_value; break;
  848. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: body_angular_velocity_damp_ratio = p_value; break;
  849. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: constraint_bias = p_value; break;
  850. case PhysicsServer::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH: test_motion_min_contact_depth = p_value; break;
  851. }
  852. }
  853. real_t SpaceSW::get_param(PhysicsServer::SpaceParameter p_param) const {
  854. switch (p_param) {
  855. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: return contact_recycle_radius;
  856. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: return contact_max_separation;
  857. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: return contact_max_allowed_penetration;
  858. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: return body_linear_velocity_sleep_threshold;
  859. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: return body_angular_velocity_sleep_threshold;
  860. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: return body_time_to_sleep;
  861. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: return body_angular_velocity_damp_ratio;
  862. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: return constraint_bias;
  863. case PhysicsServer::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH: return test_motion_min_contact_depth;
  864. }
  865. return 0;
  866. }
  867. void SpaceSW::lock() {
  868. locked = true;
  869. }
  870. void SpaceSW::unlock() {
  871. locked = false;
  872. }
  873. bool SpaceSW::is_locked() const {
  874. return locked;
  875. }
  876. PhysicsDirectSpaceStateSW *SpaceSW::get_direct_state() {
  877. return direct_access;
  878. }
  879. SpaceSW::SpaceSW() {
  880. collision_pairs = 0;
  881. active_objects = 0;
  882. island_count = 0;
  883. contact_debug_count = 0;
  884. locked = false;
  885. contact_recycle_radius = 0.01;
  886. contact_max_separation = 0.05;
  887. contact_max_allowed_penetration = 0.01;
  888. test_motion_min_contact_depth = 0.00001;
  889. constraint_bias = 0.01;
  890. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
  891. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", (8.0 / 180.0 * Math_PI));
  892. body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
  893. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/time_before_sleep", PropertyInfo(Variant::REAL, "physics/3d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  894. body_angular_velocity_damp_ratio = 10;
  895. broadphase = BroadPhaseSW::create_func();
  896. broadphase->set_pair_callback(_broadphase_pair, this);
  897. broadphase->set_unpair_callback(_broadphase_unpair, this);
  898. area = NULL;
  899. direct_access = memnew(PhysicsDirectSpaceStateSW);
  900. direct_access->space = this;
  901. for (int i = 0; i < ELAPSED_TIME_MAX; i++)
  902. elapsed_time[i] = 0;
  903. }
  904. SpaceSW::~SpaceSW() {
  905. memdelete(broadphase);
  906. memdelete(direct_access);
  907. }