space_2d_sw.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*************************************************************************/
  2. /* space_2d_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_2d_sw.h"
  31. #include "collision_solver_2d_sw.h"
  32. #include "core/os/os.h"
  33. #include "core/pair.h"
  34. #include "physics_2d_server_sw.h"
  35. _FORCE_INLINE_ static bool _can_collide_with(CollisionObject2DSW *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  36. if (!(p_object->get_collision_layer() & p_collision_mask)) {
  37. return false;
  38. }
  39. if (p_object->get_type() == CollisionObject2DSW::TYPE_AREA && !p_collide_with_areas)
  40. return false;
  41. if (p_object->get_type() == CollisionObject2DSW::TYPE_BODY && !p_collide_with_bodies)
  42. return false;
  43. return true;
  44. }
  45. int Physics2DDirectSpaceStateSW::_intersect_point_impl(const Vector2 &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, bool p_pick_point, bool p_filter_by_canvas, ObjectID p_canvas_instance_id) {
  46. if (p_result_max <= 0)
  47. return 0;
  48. Rect2 aabb;
  49. aabb.position = p_point - Vector2(0.00001, 0.00001);
  50. aabb.size = Vector2(0.00002, 0.00002);
  51. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  52. int cc = 0;
  53. for (int i = 0; i < amount; i++) {
  54. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  55. continue;
  56. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  57. continue;
  58. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  59. if (p_pick_point && !col_obj->is_pickable())
  60. continue;
  61. if (p_filter_by_canvas && col_obj->get_canvas_instance_id() != p_canvas_instance_id)
  62. continue;
  63. int shape_idx = space->intersection_query_subindex_results[i];
  64. Shape2DSW *shape = col_obj->get_shape(shape_idx);
  65. Vector2 local_point = (col_obj->get_transform() * col_obj->get_shape_transform(shape_idx)).affine_inverse().xform(p_point);
  66. if (!shape->contains_point(local_point))
  67. continue;
  68. if (cc >= p_result_max)
  69. continue;
  70. r_results[cc].collider_id = col_obj->get_instance_id();
  71. if (r_results[cc].collider_id != 0)
  72. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  73. r_results[cc].rid = col_obj->get_self();
  74. r_results[cc].shape = shape_idx;
  75. r_results[cc].metadata = col_obj->get_shape_metadata(shape_idx);
  76. cc++;
  77. }
  78. return cc;
  79. }
  80. int Physics2DDirectSpaceStateSW::intersect_point(const Vector2 &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, bool p_pick_point) {
  81. return _intersect_point_impl(p_point, r_results, p_result_max, p_exclude, p_collision_mask, p_collide_with_bodies, p_collide_with_areas, p_pick_point);
  82. }
  83. int Physics2DDirectSpaceStateSW::intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_instance_id, 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, bool p_pick_point) {
  84. return _intersect_point_impl(p_point, r_results, p_result_max, p_exclude, p_collision_mask, p_collide_with_bodies, p_collide_with_areas, p_pick_point, true, p_canvas_instance_id);
  85. }
  86. bool Physics2DDirectSpaceStateSW::intersect_ray(const Vector2 &p_from, const Vector2 &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) {
  87. ERR_FAIL_COND_V(space->locked, false);
  88. Vector2 begin, end;
  89. Vector2 normal;
  90. begin = p_from;
  91. end = p_to;
  92. normal = (end - begin).normalized();
  93. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  94. //todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
  95. bool collided = false;
  96. Vector2 res_point, res_normal;
  97. int res_shape;
  98. const CollisionObject2DSW *res_obj;
  99. real_t min_d = 1e10;
  100. for (int i = 0; i < amount; i++) {
  101. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  102. continue;
  103. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  104. continue;
  105. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  106. int shape_idx = space->intersection_query_subindex_results[i];
  107. Transform2D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  108. Vector2 local_from = inv_xform.xform(begin);
  109. Vector2 local_to = inv_xform.xform(end);
  110. /*local_from = col_obj->get_inv_transform().xform(begin);
  111. local_from = col_obj->get_shape_inv_transform(shape_idx).xform(local_from);
  112. local_to = col_obj->get_inv_transform().xform(end);
  113. local_to = col_obj->get_shape_inv_transform(shape_idx).xform(local_to);*/
  114. const Shape2DSW *shape = col_obj->get_shape(shape_idx);
  115. Vector2 shape_point, shape_normal;
  116. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
  117. Transform2D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  118. shape_point = xform.xform(shape_point);
  119. real_t ld = normal.dot(shape_point);
  120. if (ld < min_d) {
  121. min_d = ld;
  122. res_point = shape_point;
  123. res_normal = inv_xform.basis_xform_inv(shape_normal).normalized();
  124. res_shape = shape_idx;
  125. res_obj = col_obj;
  126. collided = true;
  127. }
  128. }
  129. }
  130. if (!collided)
  131. return false;
  132. r_result.collider_id = res_obj->get_instance_id();
  133. if (r_result.collider_id != 0)
  134. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  135. r_result.normal = res_normal;
  136. r_result.metadata = res_obj->get_shape_metadata(res_shape);
  137. r_result.position = res_point;
  138. r_result.rid = res_obj->get_self();
  139. r_result.shape = res_shape;
  140. return true;
  141. }
  142. int Physics2DDirectSpaceStateSW::intersect_shape(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, 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) {
  143. if (p_result_max <= 0)
  144. return 0;
  145. Shape2DSW *shape = Physics2DServerSW::singletonsw->shape_owner.get(p_shape);
  146. ERR_FAIL_COND_V(!shape, 0);
  147. Rect2 aabb = p_xform.xform(shape->get_aabb());
  148. aabb = aabb.grow(p_margin);
  149. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  150. int cc = 0;
  151. for (int i = 0; i < amount; i++) {
  152. if (cc >= p_result_max)
  153. break;
  154. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  155. continue;
  156. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  157. continue;
  158. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  159. int shape_idx = space->intersection_query_subindex_results[i];
  160. if (!CollisionSolver2DSW::solve(shape, p_xform, p_motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), NULL, NULL, NULL, p_margin))
  161. continue;
  162. r_results[cc].collider_id = col_obj->get_instance_id();
  163. if (r_results[cc].collider_id != 0)
  164. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  165. r_results[cc].rid = col_obj->get_self();
  166. r_results[cc].shape = shape_idx;
  167. r_results[cc].metadata = col_obj->get_shape_metadata(shape_idx);
  168. cc++;
  169. }
  170. return cc;
  171. }
  172. bool Physics2DDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transform2D &p_xform, const Vector2 &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) {
  173. Shape2DSW *shape = Physics2DServerSW::singletonsw->shape_owner.get(p_shape);
  174. ERR_FAIL_COND_V(!shape, false);
  175. Rect2 aabb = p_xform.xform(shape->get_aabb());
  176. aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion
  177. aabb = aabb.grow(p_margin);
  178. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  179. real_t best_safe = 1;
  180. real_t best_unsafe = 1;
  181. for (int i = 0; i < amount; i++) {
  182. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  183. continue;
  184. if (p_exclude.has(space->intersection_query_results[i]->get_self()))
  185. continue; //ignore excluded
  186. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  187. int shape_idx = space->intersection_query_subindex_results[i];
  188. Transform2D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  189. //test initial overlap, does it collide if going all the way?
  190. if (!CollisionSolver2DSW::solve(shape, p_xform, p_motion, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), NULL, NULL, NULL, p_margin)) {
  191. continue;
  192. }
  193. //test initial overlap
  194. if (CollisionSolver2DSW::solve(shape, p_xform, Vector2(), col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), NULL, NULL, NULL, p_margin)) {
  195. return false;
  196. }
  197. //just do kinematic solving
  198. real_t low = 0;
  199. real_t hi = 1;
  200. Vector2 mnormal = p_motion.normalized();
  201. for (int i = 0; i < 8; i++) { //steps should be customizable..
  202. real_t ofs = (low + hi) * 0.5;
  203. Vector2 sep = mnormal; //important optimization for this to work fast enough
  204. bool collided = CollisionSolver2DSW::solve(shape, p_xform, p_motion * ofs, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), NULL, NULL, &sep, p_margin);
  205. if (collided) {
  206. hi = ofs;
  207. } else {
  208. low = ofs;
  209. }
  210. }
  211. if (low < best_safe) {
  212. best_safe = low;
  213. best_unsafe = hi;
  214. }
  215. }
  216. p_closest_safe = best_safe;
  217. p_closest_unsafe = best_unsafe;
  218. return true;
  219. }
  220. bool Physics2DDirectSpaceStateSW::collide_shape(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, real_t p_margin, Vector2 *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) {
  221. if (p_result_max <= 0)
  222. return 0;
  223. Shape2DSW *shape = Physics2DServerSW::singletonsw->shape_owner.get(p_shape);
  224. ERR_FAIL_COND_V(!shape, 0);
  225. Rect2 aabb = p_shape_xform.xform(shape->get_aabb());
  226. aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion
  227. aabb = aabb.grow(p_margin);
  228. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  229. bool collided = false;
  230. r_result_count = 0;
  231. Physics2DServerSW::CollCbkData cbk;
  232. cbk.max = p_result_max;
  233. cbk.amount = 0;
  234. cbk.ptr = r_results;
  235. CollisionSolver2DSW::CallbackResult cbkres = NULL;
  236. Physics2DServerSW::CollCbkData *cbkptr = NULL;
  237. if (p_result_max > 0) {
  238. cbkptr = &cbk;
  239. cbkres = Physics2DServerSW::_shape_col_cbk;
  240. }
  241. for (int i = 0; i < amount; i++) {
  242. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  243. continue;
  244. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  245. int shape_idx = space->intersection_query_subindex_results[i];
  246. if (p_exclude.has(col_obj->get_self()))
  247. continue;
  248. cbk.valid_dir = Vector2();
  249. cbk.valid_depth = 0;
  250. if (CollisionSolver2DSW::solve(shape, p_shape_xform, p_motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), cbkres, cbkptr, NULL, p_margin)) {
  251. collided = p_result_max == 0 || cbk.amount > 0;
  252. }
  253. }
  254. r_result_count = cbk.amount;
  255. return collided;
  256. }
  257. struct _RestCallbackData2D {
  258. const CollisionObject2DSW *object;
  259. const CollisionObject2DSW *best_object;
  260. int local_shape;
  261. int best_local_shape;
  262. int shape;
  263. int best_shape;
  264. Vector2 best_contact;
  265. Vector2 best_normal;
  266. real_t best_len;
  267. Vector2 valid_dir;
  268. real_t valid_depth;
  269. };
  270. static void _rest_cbk_result(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
  271. _RestCallbackData2D *rd = (_RestCallbackData2D *)p_userdata;
  272. if (rd->valid_dir != Vector2()) {
  273. if (p_point_A.distance_squared_to(p_point_B) > rd->valid_depth * rd->valid_depth)
  274. return;
  275. if (rd->valid_dir.dot((p_point_A - p_point_B).normalized()) < Math_PI * 0.25)
  276. return;
  277. }
  278. Vector2 contact_rel = p_point_B - p_point_A;
  279. real_t len = contact_rel.length();
  280. if (len <= rd->best_len)
  281. return;
  282. rd->best_len = len;
  283. rd->best_contact = p_point_B;
  284. rd->best_normal = contact_rel / len;
  285. rd->best_object = rd->object;
  286. rd->best_shape = rd->shape;
  287. rd->best_local_shape = rd->local_shape;
  288. }
  289. bool Physics2DDirectSpaceStateSW::rest_info(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, 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) {
  290. Shape2DSW *shape = Physics2DServerSW::singletonsw->shape_owner.get(p_shape);
  291. ERR_FAIL_COND_V(!shape, 0);
  292. Rect2 aabb = p_shape_xform.xform(shape->get_aabb());
  293. aabb = aabb.merge(Rect2(aabb.position + p_motion, aabb.size)); //motion
  294. aabb = aabb.grow(p_margin);
  295. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space2DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  296. _RestCallbackData2D rcd;
  297. rcd.best_len = 0;
  298. rcd.best_object = NULL;
  299. rcd.best_shape = 0;
  300. for (int i = 0; i < amount; i++) {
  301. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas))
  302. continue;
  303. const CollisionObject2DSW *col_obj = space->intersection_query_results[i];
  304. int shape_idx = space->intersection_query_subindex_results[i];
  305. if (p_exclude.has(col_obj->get_self()))
  306. continue;
  307. rcd.valid_dir = Vector2();
  308. rcd.valid_depth = 0;
  309. rcd.object = col_obj;
  310. rcd.shape = shape_idx;
  311. rcd.local_shape = 0;
  312. bool sc = CollisionSolver2DSW::solve(shape, p_shape_xform, p_motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), _rest_cbk_result, &rcd, NULL, p_margin);
  313. if (!sc)
  314. continue;
  315. }
  316. if (rcd.best_len == 0)
  317. return false;
  318. r_info->collider_id = rcd.best_object->get_instance_id();
  319. r_info->shape = rcd.best_shape;
  320. r_info->normal = rcd.best_normal;
  321. r_info->point = rcd.best_contact;
  322. r_info->rid = rcd.best_object->get_self();
  323. r_info->metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  324. if (rcd.best_object->get_type() == CollisionObject2DSW::TYPE_BODY) {
  325. const Body2DSW *body = static_cast<const Body2DSW *>(rcd.best_object);
  326. Vector2 rel_vec = r_info->point - body->get_transform().get_origin();
  327. r_info->linear_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  328. } else {
  329. r_info->linear_velocity = Vector2();
  330. }
  331. return true;
  332. }
  333. Physics2DDirectSpaceStateSW::Physics2DDirectSpaceStateSW() {
  334. space = NULL;
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  337. int Space2DSW::_cull_aabb_for_body(Body2DSW *p_body, const Rect2 &p_aabb) {
  338. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  339. for (int i = 0; i < amount; i++) {
  340. bool keep = true;
  341. if (intersection_query_results[i] == p_body)
  342. keep = false;
  343. else if (intersection_query_results[i]->get_type() == CollisionObject2DSW::TYPE_AREA)
  344. keep = false;
  345. else if ((static_cast<Body2DSW *>(intersection_query_results[i])->test_collision_mask(p_body)) == 0)
  346. keep = false;
  347. else if (static_cast<Body2DSW *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self()))
  348. keep = false;
  349. else if (static_cast<Body2DSW *>(intersection_query_results[i])->is_shape_set_as_disabled(intersection_query_subindex_results[i]))
  350. keep = false;
  351. if (!keep) {
  352. if (i < amount - 1) {
  353. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  354. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  355. }
  356. amount--;
  357. i--;
  358. }
  359. }
  360. return amount;
  361. }
  362. int Space2DSW::test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, Physics2DServer::SeparationResult *r_results, int p_result_max, real_t p_margin) {
  363. Rect2 body_aabb;
  364. bool shapes_found = false;
  365. for (int i = 0; i < p_body->get_shape_count(); i++) {
  366. if (p_body->is_shape_set_as_disabled(i))
  367. continue;
  368. if (!shapes_found) {
  369. body_aabb = p_body->get_shape_aabb(i);
  370. shapes_found = true;
  371. } else {
  372. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  373. }
  374. }
  375. if (!shapes_found) {
  376. return 0;
  377. }
  378. // Undo the currently transform the physics server is aware of and apply the provided one
  379. body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
  380. body_aabb = body_aabb.grow(p_margin);
  381. Transform2D body_transform = p_transform;
  382. for (int i = 0; i < p_result_max; i++) {
  383. //reset results
  384. r_results[i].collision_depth = 0;
  385. }
  386. int rays_found = 0;
  387. {
  388. // raycast AND separate
  389. const int max_results = 32;
  390. int recover_attempts = 4;
  391. Vector2 sr[max_results * 2];
  392. Physics2DServerSW::CollCbkData cbk;
  393. cbk.max = max_results;
  394. Physics2DServerSW::CollCbkData *cbkptr = &cbk;
  395. CollisionSolver2DSW::CallbackResult cbkres = Physics2DServerSW::_shape_col_cbk;
  396. do {
  397. Vector2 recover_motion;
  398. bool collided = false;
  399. int amount = _cull_aabb_for_body(p_body, body_aabb);
  400. int ray_index = 0;
  401. for (int j = 0; j < p_body->get_shape_count(); j++) {
  402. if (p_body->is_shape_set_as_disabled(j))
  403. continue;
  404. Shape2DSW *body_shape = p_body->get_shape(j);
  405. if (body_shape->get_type() != Physics2DServer::SHAPE_RAY)
  406. continue;
  407. Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  408. for (int i = 0; i < amount; i++) {
  409. const CollisionObject2DSW *col_obj = intersection_query_results[i];
  410. int shape_idx = intersection_query_subindex_results[i];
  411. cbk.amount = 0;
  412. cbk.ptr = sr;
  413. cbk.invalid_by_dir = 0;
  414. if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
  415. const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
  416. if (p_infinite_inertia && Physics2DServer::BODY_MODE_STATIC != b->get_mode() && Physics2DServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  417. continue;
  418. }
  419. }
  420. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  421. if (col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
  422. cbk.valid_dir = col_obj_shape_xform.get_axis(1).normalized();
  423. cbk.valid_depth = p_margin; //only valid depth is the collision margin
  424. cbk.invalid_by_dir = 0;
  425. } else {
  426. cbk.valid_dir = Vector2();
  427. cbk.valid_depth = 0;
  428. cbk.invalid_by_dir = 0;
  429. }
  430. Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
  431. if (CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, NULL, p_margin)) {
  432. if (cbk.amount > 0) {
  433. collided = true;
  434. }
  435. if (ray_index < p_result_max) {
  436. Physics2DServer::SeparationResult &result = r_results[ray_index];
  437. for (int k = 0; k < cbk.amount; k++) {
  438. Vector2 a = sr[k * 2 + 0];
  439. Vector2 b = sr[k * 2 + 1];
  440. recover_motion += (b - a) * 0.4;
  441. float depth = a.distance_to(b);
  442. if (depth > result.collision_depth) {
  443. result.collision_depth = depth;
  444. result.collision_point = b;
  445. result.collision_normal = (b - a).normalized();
  446. result.collision_local_shape = shape_idx;
  447. result.collider = col_obj->get_self();
  448. result.collider_id = col_obj->get_instance_id();
  449. result.collider_metadata = col_obj->get_shape_metadata(shape_idx);
  450. if (col_obj->get_type() == CollisionObject2DSW::TYPE_BODY) {
  451. Body2DSW *body = (Body2DSW *)col_obj;
  452. Vector2 rel_vec = b - body->get_transform().get_origin();
  453. result.collider_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. ray_index++;
  461. }
  462. rays_found = MAX(ray_index, rays_found);
  463. if (!collided || recover_motion == Vector2()) {
  464. break;
  465. }
  466. body_transform.elements[2] += recover_motion;
  467. body_aabb.position += recover_motion;
  468. recover_attempts--;
  469. } while (recover_attempts);
  470. }
  471. //optimize results (remove non colliding)
  472. for (int i = 0; i < rays_found; i++) {
  473. if (r_results[i].collision_depth == 0) {
  474. rays_found--;
  475. SWAP(r_results[i], r_results[rays_found]);
  476. }
  477. }
  478. r_recover_motion = body_transform.elements[2] - p_transform.elements[2];
  479. return rays_found;
  480. }
  481. bool Space2DSW::test_body_motion(Body2DSW *p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, Physics2DServer::MotionResult *r_result, bool p_exclude_raycast_shapes) {
  482. //give me back regular physics engine logic
  483. //this is madness
  484. //and most people using this function will think
  485. //what it does is simpler than using physics
  486. //this took about a week to get right..
  487. //but is it right? who knows at this point..
  488. if (r_result) {
  489. r_result->collider_id = 0;
  490. r_result->collider_shape = 0;
  491. }
  492. Rect2 body_aabb;
  493. bool shapes_found = false;
  494. for (int i = 0; i < p_body->get_shape_count(); i++) {
  495. if (p_body->is_shape_set_as_disabled(i))
  496. continue;
  497. if (!shapes_found) {
  498. body_aabb = p_body->get_shape_aabb(i);
  499. shapes_found = true;
  500. } else {
  501. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  502. }
  503. }
  504. if (!shapes_found) {
  505. return false;
  506. }
  507. // Undo the currently transform the physics server is aware of and apply the provided one
  508. body_aabb = p_from.xform(p_body->get_inv_transform().xform(body_aabb));
  509. body_aabb = body_aabb.grow(p_margin);
  510. static const int max_excluded_shape_pairs = 32;
  511. ExcludedShapeSW excluded_shape_pairs[max_excluded_shape_pairs];
  512. int excluded_shape_pair_count = 0;
  513. float separation_margin = MIN(p_margin, MAX(0.0, p_motion.length() - CMP_EPSILON)); //don't separate by more than the intended motion
  514. Transform2D body_transform = p_from;
  515. {
  516. //STEP 1, FREE BODY IF STUCK
  517. const int max_results = 32;
  518. int recover_attempts = 4;
  519. Vector2 sr[max_results * 2];
  520. do {
  521. Physics2DServerSW::CollCbkData cbk;
  522. cbk.max = max_results;
  523. cbk.amount = 0;
  524. cbk.ptr = sr;
  525. cbk.invalid_by_dir = 0;
  526. excluded_shape_pair_count = 0; //last step is the one valid
  527. Physics2DServerSW::CollCbkData *cbkptr = &cbk;
  528. CollisionSolver2DSW::CallbackResult cbkres = Physics2DServerSW::_shape_col_cbk;
  529. bool collided = false;
  530. int amount = _cull_aabb_for_body(p_body, body_aabb);
  531. for (int j = 0; j < p_body->get_shape_count(); j++) {
  532. if (p_body->is_shape_set_as_disabled(j))
  533. continue;
  534. Shape2DSW *body_shape = p_body->get_shape(j);
  535. if (p_exclude_raycast_shapes && body_shape->get_type() == Physics2DServer::SHAPE_RAY) {
  536. continue;
  537. }
  538. Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  539. for (int i = 0; i < amount; i++) {
  540. const CollisionObject2DSW *col_obj = intersection_query_results[i];
  541. int shape_idx = intersection_query_subindex_results[i];
  542. if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
  543. const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
  544. if (p_infinite_inertia && Physics2DServer::BODY_MODE_STATIC != b->get_mode() && Physics2DServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  545. continue;
  546. }
  547. }
  548. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  549. if (col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
  550. cbk.valid_dir = col_obj_shape_xform.get_axis(1).normalized();
  551. cbk.valid_depth = p_margin; //only valid depth is the collision margin
  552. cbk.invalid_by_dir = 0;
  553. if (col_obj->get_type() == CollisionObject2DSW::TYPE_BODY) {
  554. const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
  555. if (b->get_mode() == Physics2DServer::BODY_MODE_KINEMATIC || b->get_mode() == Physics2DServer::BODY_MODE_RIGID) {
  556. //fix for moving platforms (kinematic and dynamic), margin is increased by how much it moved in the given direction
  557. Vector2 lv = b->get_linear_velocity();
  558. //compute displacement from linear velocity
  559. Vector2 motion = lv * Physics2DDirectBodyStateSW::singleton->step;
  560. float motion_len = motion.length();
  561. motion.normalize();
  562. cbk.valid_depth += motion_len * MAX(motion.dot(-cbk.valid_dir), 0.0);
  563. }
  564. }
  565. } else {
  566. cbk.valid_dir = Vector2();
  567. cbk.valid_depth = 0;
  568. cbk.invalid_by_dir = 0;
  569. }
  570. int current_collisions = cbk.amount;
  571. bool did_collide = false;
  572. Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
  573. if (CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, NULL, separation_margin)) {
  574. did_collide = cbk.amount > current_collisions;
  575. }
  576. if (!did_collide && cbk.invalid_by_dir > 0) {
  577. //this shape must be excluded
  578. if (excluded_shape_pair_count < max_excluded_shape_pairs) {
  579. ExcludedShapeSW esp;
  580. esp.local_shape = body_shape;
  581. esp.against_object = col_obj;
  582. esp.against_shape_index = shape_idx;
  583. excluded_shape_pairs[excluded_shape_pair_count++] = esp;
  584. }
  585. }
  586. if (did_collide) {
  587. collided = true;
  588. }
  589. }
  590. }
  591. if (!collided) {
  592. break;
  593. }
  594. Vector2 recover_motion;
  595. for (int i = 0; i < cbk.amount; i++) {
  596. Vector2 a = sr[i * 2 + 0];
  597. Vector2 b = sr[i * 2 + 1];
  598. recover_motion += (b - a) * 0.4;
  599. }
  600. if (recover_motion == Vector2()) {
  601. collided = false;
  602. break;
  603. }
  604. body_transform.elements[2] += recover_motion;
  605. body_aabb.position += recover_motion;
  606. recover_attempts--;
  607. } while (recover_attempts);
  608. }
  609. real_t safe = 1.0;
  610. real_t unsafe = 1.0;
  611. int best_shape = -1;
  612. {
  613. // STEP 2 ATTEMPT MOTION
  614. Rect2 motion_aabb = body_aabb;
  615. motion_aabb.position += p_motion;
  616. motion_aabb = motion_aabb.merge(body_aabb);
  617. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  618. for (int body_shape_idx = 0; body_shape_idx < p_body->get_shape_count(); body_shape_idx++) {
  619. if (p_body->is_shape_set_as_disabled(body_shape_idx))
  620. continue;
  621. Shape2DSW *body_shape = p_body->get_shape(body_shape_idx);
  622. if (p_exclude_raycast_shapes && body_shape->get_type() == Physics2DServer::SHAPE_RAY) {
  623. continue;
  624. }
  625. Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(body_shape_idx);
  626. bool stuck = false;
  627. real_t best_safe = 1;
  628. real_t best_unsafe = 1;
  629. for (int i = 0; i < amount; i++) {
  630. const CollisionObject2DSW *col_obj = intersection_query_results[i];
  631. int col_shape_idx = intersection_query_subindex_results[i];
  632. Shape2DSW *against_shape = col_obj->get_shape(col_shape_idx);
  633. if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
  634. const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
  635. if (p_infinite_inertia && Physics2DServer::BODY_MODE_STATIC != b->get_mode() && Physics2DServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  636. continue;
  637. }
  638. }
  639. bool excluded = false;
  640. for (int k = 0; k < excluded_shape_pair_count; k++) {
  641. if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == col_shape_idx) {
  642. excluded = true;
  643. break;
  644. }
  645. }
  646. if (excluded) {
  647. continue;
  648. }
  649. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(col_shape_idx);
  650. //test initial overlap, does it collide if going all the way?
  651. if (!CollisionSolver2DSW::solve(body_shape, body_shape_xform, p_motion, against_shape, col_obj_shape_xform, Vector2(), NULL, NULL, NULL, 0)) {
  652. continue;
  653. }
  654. //test initial overlap
  655. if (CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), NULL, NULL, NULL, 0)) {
  656. if (col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
  657. continue;
  658. }
  659. stuck = true;
  660. break;
  661. }
  662. //just do kinematic solving
  663. real_t low = 0;
  664. real_t hi = 1;
  665. Vector2 mnormal = p_motion.normalized();
  666. for (int k = 0; k < 8; k++) { //steps should be customizable..
  667. real_t ofs = (low + hi) * 0.5;
  668. Vector2 sep = mnormal; //important optimization for this to work fast enough
  669. bool collided = CollisionSolver2DSW::solve(body_shape, body_shape_xform, p_motion * ofs, against_shape, col_obj_shape_xform, Vector2(), NULL, NULL, &sep, 0);
  670. if (collided) {
  671. hi = ofs;
  672. } else {
  673. low = ofs;
  674. }
  675. }
  676. if (col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
  677. Vector2 cd[2];
  678. Physics2DServerSW::CollCbkData cbk;
  679. cbk.max = 1;
  680. cbk.amount = 0;
  681. cbk.ptr = cd;
  682. cbk.valid_dir = col_obj_shape_xform.get_axis(1).normalized();
  683. cbk.valid_depth = 10e20;
  684. Vector2 sep = mnormal; //important optimization for this to work fast enough
  685. bool collided = CollisionSolver2DSW::solve(body_shape, body_shape_xform, p_motion * (hi + contact_max_allowed_penetration), col_obj->get_shape(col_shape_idx), col_obj_shape_xform, Vector2(), Physics2DServerSW::_shape_col_cbk, &cbk, &sep, 0);
  686. if (!collided || cbk.amount == 0) {
  687. continue;
  688. }
  689. }
  690. if (low < best_safe) {
  691. best_safe = low;
  692. best_unsafe = hi;
  693. }
  694. }
  695. if (stuck) {
  696. safe = 0;
  697. unsafe = 0;
  698. best_shape = body_shape_idx; //sadly it's the best
  699. break;
  700. }
  701. if (best_safe == 1.0) {
  702. continue;
  703. }
  704. if (best_safe < safe) {
  705. safe = best_safe;
  706. unsafe = best_unsafe;
  707. best_shape = body_shape_idx;
  708. }
  709. }
  710. }
  711. bool collided = false;
  712. if (safe >= 1) {
  713. best_shape = -1; //no best shape with cast, reset to -1
  714. }
  715. {
  716. //it collided, let's get the rest info in unsafe advance
  717. Transform2D ugt = body_transform;
  718. ugt.elements[2] += p_motion * unsafe;
  719. _RestCallbackData2D rcd;
  720. rcd.best_len = 0;
  721. rcd.best_object = NULL;
  722. rcd.best_shape = 0;
  723. //optimization
  724. int from_shape = best_shape != -1 ? best_shape : 0;
  725. int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
  726. for (int j = from_shape; j < to_shape; j++) {
  727. Transform2D body_shape_xform = ugt * p_body->get_shape_transform(j);
  728. Shape2DSW *body_shape = p_body->get_shape(j);
  729. body_aabb.position += p_motion * unsafe;
  730. int amount = _cull_aabb_for_body(p_body, body_aabb);
  731. for (int i = 0; i < amount; i++) {
  732. const CollisionObject2DSW *col_obj = intersection_query_results[i];
  733. int shape_idx = intersection_query_subindex_results[i];
  734. if (CollisionObject2DSW::TYPE_BODY == col_obj->get_type()) {
  735. const Body2DSW *b = static_cast<const Body2DSW *>(col_obj);
  736. if (p_infinite_inertia && Physics2DServer::BODY_MODE_STATIC != b->get_mode() && Physics2DServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  737. continue;
  738. }
  739. }
  740. Shape2DSW *against_shape = col_obj->get_shape(shape_idx);
  741. bool excluded = false;
  742. for (int k = 0; k < excluded_shape_pair_count; k++) {
  743. if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == shape_idx) {
  744. excluded = true;
  745. break;
  746. }
  747. }
  748. if (excluded)
  749. continue;
  750. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  751. if (col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
  752. rcd.valid_dir = col_obj_shape_xform.get_axis(1).normalized();
  753. rcd.valid_depth = 10e20;
  754. } else {
  755. rcd.valid_dir = Vector2();
  756. rcd.valid_depth = 0;
  757. }
  758. rcd.object = col_obj;
  759. rcd.shape = shape_idx;
  760. rcd.local_shape = j;
  761. bool sc = CollisionSolver2DSW::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), _rest_cbk_result, &rcd, NULL, p_margin);
  762. if (!sc)
  763. continue;
  764. }
  765. }
  766. if (rcd.best_len != 0) {
  767. if (r_result) {
  768. r_result->collider = rcd.best_object->get_self();
  769. r_result->collider_id = rcd.best_object->get_instance_id();
  770. r_result->collider_shape = rcd.best_shape;
  771. r_result->collision_local_shape = rcd.best_local_shape;
  772. r_result->collision_normal = rcd.best_normal;
  773. r_result->collision_point = rcd.best_contact;
  774. r_result->collider_metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  775. const Body2DSW *body = static_cast<const Body2DSW *>(rcd.best_object);
  776. Vector2 rel_vec = r_result->collision_point - body->get_transform().get_origin();
  777. r_result->collider_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  778. r_result->motion = safe * p_motion;
  779. r_result->remainder = p_motion - safe * p_motion;
  780. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  781. }
  782. collided = true;
  783. }
  784. }
  785. if (!collided && r_result) {
  786. r_result->motion = p_motion;
  787. r_result->remainder = Vector2();
  788. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  789. }
  790. return collided;
  791. }
  792. void *Space2DSW::_broadphase_pair(CollisionObject2DSW *A, int p_subindex_A, CollisionObject2DSW *B, int p_subindex_B, void *p_self) {
  793. CollisionObject2DSW::Type type_A = A->get_type();
  794. CollisionObject2DSW::Type type_B = B->get_type();
  795. if (type_A > type_B) {
  796. SWAP(A, B);
  797. SWAP(p_subindex_A, p_subindex_B);
  798. SWAP(type_A, type_B);
  799. }
  800. Space2DSW *self = (Space2DSW *)p_self;
  801. self->collision_pairs++;
  802. if (type_A == CollisionObject2DSW::TYPE_AREA) {
  803. Area2DSW *area = static_cast<Area2DSW *>(A);
  804. if (type_B == CollisionObject2DSW::TYPE_AREA) {
  805. Area2DSW *area_b = static_cast<Area2DSW *>(B);
  806. Area2Pair2DSW *area2_pair = memnew(Area2Pair2DSW(area_b, p_subindex_B, area, p_subindex_A));
  807. return area2_pair;
  808. } else {
  809. Body2DSW *body = static_cast<Body2DSW *>(B);
  810. AreaPair2DSW *area_pair = memnew(AreaPair2DSW(body, p_subindex_B, area, p_subindex_A));
  811. return area_pair;
  812. }
  813. } else {
  814. BodyPair2DSW *b = memnew(BodyPair2DSW((Body2DSW *)A, p_subindex_A, (Body2DSW *)B, p_subindex_B));
  815. return b;
  816. }
  817. return NULL;
  818. }
  819. void Space2DSW::_broadphase_unpair(CollisionObject2DSW *A, int p_subindex_A, CollisionObject2DSW *B, int p_subindex_B, void *p_data, void *p_self) {
  820. Space2DSW *self = (Space2DSW *)p_self;
  821. self->collision_pairs--;
  822. Constraint2DSW *c = (Constraint2DSW *)p_data;
  823. memdelete(c);
  824. }
  825. const SelfList<Body2DSW>::List &Space2DSW::get_active_body_list() const {
  826. return active_list;
  827. }
  828. void Space2DSW::body_add_to_active_list(SelfList<Body2DSW> *p_body) {
  829. active_list.add(p_body);
  830. }
  831. void Space2DSW::body_remove_from_active_list(SelfList<Body2DSW> *p_body) {
  832. active_list.remove(p_body);
  833. }
  834. void Space2DSW::body_add_to_inertia_update_list(SelfList<Body2DSW> *p_body) {
  835. inertia_update_list.add(p_body);
  836. }
  837. void Space2DSW::body_remove_from_inertia_update_list(SelfList<Body2DSW> *p_body) {
  838. inertia_update_list.remove(p_body);
  839. }
  840. BroadPhase2DSW *Space2DSW::get_broadphase() {
  841. return broadphase;
  842. }
  843. void Space2DSW::add_object(CollisionObject2DSW *p_object) {
  844. ERR_FAIL_COND(objects.has(p_object));
  845. objects.insert(p_object);
  846. }
  847. void Space2DSW::remove_object(CollisionObject2DSW *p_object) {
  848. ERR_FAIL_COND(!objects.has(p_object));
  849. objects.erase(p_object);
  850. }
  851. const Set<CollisionObject2DSW *> &Space2DSW::get_objects() const {
  852. return objects;
  853. }
  854. void Space2DSW::body_add_to_state_query_list(SelfList<Body2DSW> *p_body) {
  855. state_query_list.add(p_body);
  856. }
  857. void Space2DSW::body_remove_from_state_query_list(SelfList<Body2DSW> *p_body) {
  858. state_query_list.remove(p_body);
  859. }
  860. void Space2DSW::area_add_to_monitor_query_list(SelfList<Area2DSW> *p_area) {
  861. monitor_query_list.add(p_area);
  862. }
  863. void Space2DSW::area_remove_from_monitor_query_list(SelfList<Area2DSW> *p_area) {
  864. monitor_query_list.remove(p_area);
  865. }
  866. void Space2DSW::area_add_to_moved_list(SelfList<Area2DSW> *p_area) {
  867. area_moved_list.add(p_area);
  868. }
  869. void Space2DSW::area_remove_from_moved_list(SelfList<Area2DSW> *p_area) {
  870. area_moved_list.remove(p_area);
  871. }
  872. const SelfList<Area2DSW>::List &Space2DSW::get_moved_area_list() const {
  873. return area_moved_list;
  874. }
  875. void Space2DSW::call_queries() {
  876. while (state_query_list.first()) {
  877. Body2DSW *b = state_query_list.first()->self();
  878. state_query_list.remove(state_query_list.first());
  879. b->call_queries();
  880. }
  881. while (monitor_query_list.first()) {
  882. Area2DSW *a = monitor_query_list.first()->self();
  883. monitor_query_list.remove(monitor_query_list.first());
  884. a->call_queries();
  885. }
  886. }
  887. void Space2DSW::setup() {
  888. contact_debug_count = 0;
  889. while (inertia_update_list.first()) {
  890. inertia_update_list.first()->self()->update_inertias();
  891. inertia_update_list.remove(inertia_update_list.first());
  892. }
  893. }
  894. void Space2DSW::update() {
  895. broadphase->update();
  896. }
  897. void Space2DSW::set_param(Physics2DServer::SpaceParameter p_param, real_t p_value) {
  898. switch (p_param) {
  899. case Physics2DServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: contact_recycle_radius = p_value; break;
  900. case Physics2DServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: contact_max_separation = p_value; break;
  901. case Physics2DServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: contact_max_allowed_penetration = p_value; break;
  902. case Physics2DServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: body_linear_velocity_sleep_threshold = p_value; break;
  903. case Physics2DServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: body_angular_velocity_sleep_threshold = p_value; break;
  904. case Physics2DServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: body_time_to_sleep = p_value; break;
  905. case Physics2DServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: constraint_bias = p_value; break;
  906. }
  907. }
  908. real_t Space2DSW::get_param(Physics2DServer::SpaceParameter p_param) const {
  909. switch (p_param) {
  910. case Physics2DServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: return contact_recycle_radius;
  911. case Physics2DServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: return contact_max_separation;
  912. case Physics2DServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: return contact_max_allowed_penetration;
  913. case Physics2DServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD: return body_linear_velocity_sleep_threshold;
  914. case Physics2DServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD: return body_angular_velocity_sleep_threshold;
  915. case Physics2DServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: return body_time_to_sleep;
  916. case Physics2DServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: return constraint_bias;
  917. }
  918. return 0;
  919. }
  920. void Space2DSW::lock() {
  921. locked = true;
  922. }
  923. void Space2DSW::unlock() {
  924. locked = false;
  925. }
  926. bool Space2DSW::is_locked() const {
  927. return locked;
  928. }
  929. Physics2DDirectSpaceStateSW *Space2DSW::get_direct_state() {
  930. return direct_access;
  931. }
  932. Space2DSW::Space2DSW() {
  933. collision_pairs = 0;
  934. active_objects = 0;
  935. island_count = 0;
  936. contact_debug_count = 0;
  937. locked = false;
  938. contact_recycle_radius = 1.0;
  939. contact_max_separation = 1.5;
  940. contact_max_allowed_penetration = 0.3;
  941. constraint_bias = 0.2;
  942. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/2d/sleep_threshold_linear", 2.0);
  943. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/2d/sleep_threshold_angular", (8.0 / 180.0 * Math_PI));
  944. body_time_to_sleep = GLOBAL_DEF("physics/2d/time_before_sleep", 0.5);
  945. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/time_before_sleep", PropertyInfo(Variant::REAL, "physics/2d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  946. broadphase = BroadPhase2DSW::create_func();
  947. broadphase->set_pair_callback(_broadphase_pair, this);
  948. broadphase->set_unpair_callback(_broadphase_unpair, this);
  949. area = NULL;
  950. direct_access = memnew(Physics2DDirectSpaceStateSW);
  951. direct_access->space = this;
  952. for (int i = 0; i < ELAPSED_TIME_MAX; i++)
  953. elapsed_time[i] = 0;
  954. }
  955. Space2DSW::~Space2DSW() {
  956. memdelete(broadphase);
  957. memdelete(direct_access);
  958. }