space_sw.cpp 38 KB

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