physics_2d_server_sw.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*************************************************************************/
  2. /* physics_2d_server_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 "physics_2d_server_sw.h"
  31. #include "broad_phase_2d_basic.h"
  32. #include "broad_phase_2d_hash_grid.h"
  33. #include "collision_solver_2d_sw.h"
  34. #include "globals.h"
  35. #include "os/os.h"
  36. #include "script_language.h"
  37. RID Physics2DServerSW::shape_create(ShapeType p_shape) {
  38. Shape2DSW *shape = NULL;
  39. switch (p_shape) {
  40. case SHAPE_LINE: {
  41. shape = memnew(LineShape2DSW);
  42. } break;
  43. case SHAPE_RAY: {
  44. shape = memnew(RayShape2DSW);
  45. } break;
  46. case SHAPE_SEGMENT: {
  47. shape = memnew(SegmentShape2DSW);
  48. } break;
  49. case SHAPE_CIRCLE: {
  50. shape = memnew(CircleShape2DSW);
  51. } break;
  52. case SHAPE_RECTANGLE: {
  53. shape = memnew(RectangleShape2DSW);
  54. } break;
  55. case SHAPE_CAPSULE: {
  56. shape = memnew(CapsuleShape2DSW);
  57. } break;
  58. case SHAPE_CONVEX_POLYGON: {
  59. shape = memnew(ConvexPolygonShape2DSW);
  60. } break;
  61. case SHAPE_CONCAVE_POLYGON: {
  62. shape = memnew(ConcavePolygonShape2DSW);
  63. } break;
  64. case SHAPE_CUSTOM: {
  65. ERR_FAIL_V(RID());
  66. } break;
  67. }
  68. RID id = shape_owner.make_rid(shape);
  69. shape->set_self(id);
  70. return id;
  71. };
  72. void Physics2DServerSW::shape_set_data(RID p_shape, const Variant &p_data) {
  73. Shape2DSW *shape = shape_owner.get(p_shape);
  74. ERR_FAIL_COND(!shape);
  75. shape->set_data(p_data);
  76. };
  77. void Physics2DServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  78. Shape2DSW *shape = shape_owner.get(p_shape);
  79. ERR_FAIL_COND(!shape);
  80. shape->set_custom_bias(p_bias);
  81. }
  82. Physics2DServer::ShapeType Physics2DServerSW::shape_get_type(RID p_shape) const {
  83. const Shape2DSW *shape = shape_owner.get(p_shape);
  84. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  85. return shape->get_type();
  86. };
  87. Variant Physics2DServerSW::shape_get_data(RID p_shape) const {
  88. const Shape2DSW *shape = shape_owner.get(p_shape);
  89. ERR_FAIL_COND_V(!shape, Variant());
  90. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  91. return shape->get_data();
  92. };
  93. real_t Physics2DServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  94. const Shape2DSW *shape = shape_owner.get(p_shape);
  95. ERR_FAIL_COND_V(!shape, 0);
  96. return shape->get_custom_bias();
  97. }
  98. void Physics2DServerSW::_shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
  99. CollCbkData *cbk = (CollCbkData *)p_userdata;
  100. if (cbk->max == 0)
  101. return;
  102. if (cbk->valid_dir != Vector2()) {
  103. if (p_point_A.distance_squared_to(p_point_B) > cbk->valid_depth * cbk->valid_depth) {
  104. return;
  105. }
  106. if (cbk->valid_dir.dot((p_point_A - p_point_B).normalized()) < 0.7071) {
  107. /* print_line("A: "+p_point_A);
  108. print_line("B: "+p_point_B);
  109. print_line("discard too angled "+rtos(cbk->valid_dir.dot((p_point_A-p_point_B))));
  110. print_line("resnorm: "+(p_point_A-p_point_B).normalized());
  111. print_line("distance: "+rtos(p_point_A.distance_to(p_point_B)));
  112. */
  113. return;
  114. }
  115. }
  116. if (cbk->amount == cbk->max) {
  117. //find least deep
  118. float min_depth = 1e20;
  119. int min_depth_idx = 0;
  120. for (int i = 0; i < cbk->amount; i++) {
  121. float d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  122. if (d < min_depth) {
  123. min_depth = d;
  124. min_depth_idx = i;
  125. }
  126. }
  127. float d = p_point_A.distance_squared_to(p_point_B);
  128. if (d < min_depth)
  129. return;
  130. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  131. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  132. } else {
  133. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  134. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  135. cbk->amount++;
  136. }
  137. }
  138. bool Physics2DServerSW::shape_collide(RID p_shape_A, const Matrix32 &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Matrix32 &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) {
  139. Shape2DSW *shape_A = shape_owner.get(p_shape_A);
  140. ERR_FAIL_COND_V(!shape_A, false);
  141. Shape2DSW *shape_B = shape_owner.get(p_shape_B);
  142. ERR_FAIL_COND_V(!shape_B, false);
  143. if (p_result_max == 0) {
  144. return CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, NULL, NULL);
  145. }
  146. CollCbkData cbk;
  147. cbk.max = p_result_max;
  148. cbk.amount = 0;
  149. cbk.ptr = r_results;
  150. bool res = CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, _shape_col_cbk, &cbk);
  151. r_result_count = cbk.amount;
  152. return res;
  153. }
  154. RID Physics2DServerSW::space_create() {
  155. Space2DSW *space = memnew(Space2DSW);
  156. RID id = space_owner.make_rid(space);
  157. space->set_self(id);
  158. RID area_id = area_create();
  159. Area2DSW *area = area_owner.get(area_id);
  160. ERR_FAIL_COND_V(!area, RID());
  161. space->set_default_area(area);
  162. area->set_space(space);
  163. area->set_priority(-1);
  164. return id;
  165. };
  166. void Physics2DServerSW::space_set_active(RID p_space, bool p_active) {
  167. Space2DSW *space = space_owner.get(p_space);
  168. ERR_FAIL_COND(!space);
  169. if (p_active)
  170. active_spaces.insert(space);
  171. else
  172. active_spaces.erase(space);
  173. }
  174. bool Physics2DServerSW::space_is_active(RID p_space) const {
  175. const Space2DSW *space = space_owner.get(p_space);
  176. ERR_FAIL_COND_V(!space, false);
  177. return active_spaces.has(space);
  178. }
  179. void Physics2DServerSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  180. Space2DSW *space = space_owner.get(p_space);
  181. ERR_FAIL_COND(!space);
  182. space->set_param(p_param, p_value);
  183. }
  184. real_t Physics2DServerSW::space_get_param(RID p_space, SpaceParameter p_param) const {
  185. const Space2DSW *space = space_owner.get(p_space);
  186. ERR_FAIL_COND_V(!space, 0);
  187. return space->get_param(p_param);
  188. }
  189. void Physics2DServerSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  190. Space2DSW *space = space_owner.get(p_space);
  191. ERR_FAIL_COND(!space);
  192. space->set_debug_contacts(p_max_contacts);
  193. }
  194. Vector<Vector2> Physics2DServerSW::space_get_contacts(RID p_space) const {
  195. Space2DSW *space = space_owner.get(p_space);
  196. ERR_FAIL_COND_V(!space, Vector<Vector2>());
  197. return space->get_debug_contacts();
  198. }
  199. int Physics2DServerSW::space_get_contact_count(RID p_space) const {
  200. Space2DSW *space = space_owner.get(p_space);
  201. ERR_FAIL_COND_V(!space, 0);
  202. return space->get_debug_contact_count();
  203. }
  204. Physics2DDirectSpaceState *Physics2DServerSW::space_get_direct_state(RID p_space) {
  205. Space2DSW *space = space_owner.get(p_space);
  206. ERR_FAIL_COND_V(!space, NULL);
  207. if ((using_threads && !doing_sync) || space->is_locked()) {
  208. ERR_EXPLAIN("Space state is inaccesible right now, wait for iteration or fixed process notification.");
  209. ERR_FAIL_V(NULL);
  210. }
  211. return space->get_direct_state();
  212. }
  213. RID Physics2DServerSW::area_create() {
  214. Area2DSW *area = memnew(Area2DSW);
  215. RID rid = area_owner.make_rid(area);
  216. area->set_self(rid);
  217. return rid;
  218. };
  219. void Physics2DServerSW::area_set_space(RID p_area, RID p_space) {
  220. Area2DSW *area = area_owner.get(p_area);
  221. ERR_FAIL_COND(!area);
  222. Space2DSW *space = NULL;
  223. if (p_space.is_valid()) {
  224. space = space_owner.get(p_space);
  225. ERR_FAIL_COND(!space);
  226. }
  227. if (area->get_space() == space)
  228. return; //pointless
  229. area->clear_constraints();
  230. area->set_space(space);
  231. };
  232. RID Physics2DServerSW::area_get_space(RID p_area) const {
  233. Area2DSW *area = area_owner.get(p_area);
  234. ERR_FAIL_COND_V(!area, RID());
  235. Space2DSW *space = area->get_space();
  236. if (!space)
  237. return RID();
  238. return space->get_self();
  239. };
  240. void Physics2DServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  241. Area2DSW *area = area_owner.get(p_area);
  242. ERR_FAIL_COND(!area);
  243. area->set_space_override_mode(p_mode);
  244. }
  245. Physics2DServer::AreaSpaceOverrideMode Physics2DServerSW::area_get_space_override_mode(RID p_area) const {
  246. const Area2DSW *area = area_owner.get(p_area);
  247. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  248. return area->get_space_override_mode();
  249. }
  250. void Physics2DServerSW::area_add_shape(RID p_area, RID p_shape, const Matrix32 &p_transform) {
  251. Area2DSW *area = area_owner.get(p_area);
  252. ERR_FAIL_COND(!area);
  253. Shape2DSW *shape = shape_owner.get(p_shape);
  254. ERR_FAIL_COND(!shape);
  255. area->add_shape(shape, p_transform);
  256. }
  257. void Physics2DServerSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  258. Area2DSW *area = area_owner.get(p_area);
  259. ERR_FAIL_COND(!area);
  260. Shape2DSW *shape = shape_owner.get(p_shape);
  261. ERR_FAIL_COND(!shape);
  262. ERR_FAIL_COND(!shape->is_configured());
  263. area->set_shape(p_shape_idx, shape);
  264. }
  265. void Physics2DServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Matrix32 &p_transform) {
  266. Area2DSW *area = area_owner.get(p_area);
  267. ERR_FAIL_COND(!area);
  268. area->set_shape_transform(p_shape_idx, p_transform);
  269. }
  270. int Physics2DServerSW::area_get_shape_count(RID p_area) const {
  271. Area2DSW *area = area_owner.get(p_area);
  272. ERR_FAIL_COND_V(!area, -1);
  273. return area->get_shape_count();
  274. }
  275. RID Physics2DServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  276. Area2DSW *area = area_owner.get(p_area);
  277. ERR_FAIL_COND_V(!area, RID());
  278. Shape2DSW *shape = area->get_shape(p_shape_idx);
  279. ERR_FAIL_COND_V(!shape, RID());
  280. return shape->get_self();
  281. }
  282. Matrix32 Physics2DServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  283. Area2DSW *area = area_owner.get(p_area);
  284. ERR_FAIL_COND_V(!area, Matrix32());
  285. return area->get_shape_transform(p_shape_idx);
  286. }
  287. void Physics2DServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  288. Area2DSW *area = area_owner.get(p_area);
  289. ERR_FAIL_COND(!area);
  290. area->remove_shape(p_shape_idx);
  291. }
  292. void Physics2DServerSW::area_clear_shapes(RID p_area) {
  293. Area2DSW *area = area_owner.get(p_area);
  294. ERR_FAIL_COND(!area);
  295. while (area->get_shape_count())
  296. area->remove_shape(0);
  297. }
  298. void Physics2DServerSW::area_attach_object_instance_ID(RID p_area, ObjectID p_ID) {
  299. if (space_owner.owns(p_area)) {
  300. Space2DSW *space = space_owner.get(p_area);
  301. p_area = space->get_default_area()->get_self();
  302. }
  303. Area2DSW *area = area_owner.get(p_area);
  304. ERR_FAIL_COND(!area);
  305. area->set_instance_id(p_ID);
  306. }
  307. ObjectID Physics2DServerSW::area_get_object_instance_ID(RID p_area) const {
  308. if (space_owner.owns(p_area)) {
  309. Space2DSW *space = space_owner.get(p_area);
  310. p_area = space->get_default_area()->get_self();
  311. }
  312. Area2DSW *area = area_owner.get(p_area);
  313. ERR_FAIL_COND_V(!area, 0);
  314. return area->get_instance_id();
  315. }
  316. void Physics2DServerSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  317. if (space_owner.owns(p_area)) {
  318. Space2DSW *space = space_owner.get(p_area);
  319. p_area = space->get_default_area()->get_self();
  320. }
  321. Area2DSW *area = area_owner.get(p_area);
  322. ERR_FAIL_COND(!area);
  323. area->set_param(p_param, p_value);
  324. };
  325. void Physics2DServerSW::area_set_transform(RID p_area, const Matrix32 &p_transform) {
  326. Area2DSW *area = area_owner.get(p_area);
  327. ERR_FAIL_COND(!area);
  328. area->set_transform(p_transform);
  329. };
  330. Variant Physics2DServerSW::area_get_param(RID p_area, AreaParameter p_param) const {
  331. if (space_owner.owns(p_area)) {
  332. Space2DSW *space = space_owner.get(p_area);
  333. p_area = space->get_default_area()->get_self();
  334. }
  335. Area2DSW *area = area_owner.get(p_area);
  336. ERR_FAIL_COND_V(!area, Variant());
  337. return area->get_param(p_param);
  338. };
  339. Matrix32 Physics2DServerSW::area_get_transform(RID p_area) const {
  340. Area2DSW *area = area_owner.get(p_area);
  341. ERR_FAIL_COND_V(!area, Matrix32());
  342. return area->get_transform();
  343. };
  344. void Physics2DServerSW::area_set_pickable(RID p_area, bool p_pickable) {
  345. Area2DSW *area = area_owner.get(p_area);
  346. ERR_FAIL_COND(!area);
  347. area->set_pickable(p_pickable);
  348. }
  349. void Physics2DServerSW::area_set_monitorable(RID p_area, bool p_monitorable) {
  350. Area2DSW *area = area_owner.get(p_area);
  351. ERR_FAIL_COND(!area);
  352. area->set_monitorable(p_monitorable);
  353. }
  354. void Physics2DServerSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  355. Area2DSW *area = area_owner.get(p_area);
  356. ERR_FAIL_COND(!area);
  357. area->set_collision_mask(p_mask);
  358. }
  359. void Physics2DServerSW::area_set_layer_mask(RID p_area, uint32_t p_mask) {
  360. Area2DSW *area = area_owner.get(p_area);
  361. ERR_FAIL_COND(!area);
  362. area->set_layer_mask(p_mask);
  363. }
  364. void Physics2DServerSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  365. Area2DSW *area = area_owner.get(p_area);
  366. ERR_FAIL_COND(!area);
  367. area->set_monitor_callback(p_receiver ? p_receiver->get_instance_ID() : 0, p_method);
  368. }
  369. void Physics2DServerSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  370. Area2DSW *area = area_owner.get(p_area);
  371. ERR_FAIL_COND(!area);
  372. area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_ID() : 0, p_method);
  373. }
  374. /* BODY API */
  375. RID Physics2DServerSW::body_create(BodyMode p_mode, bool p_init_sleeping) {
  376. Body2DSW *body = memnew(Body2DSW);
  377. if (p_mode != BODY_MODE_RIGID)
  378. body->set_mode(p_mode);
  379. if (p_init_sleeping)
  380. body->set_state(BODY_STATE_SLEEPING, p_init_sleeping);
  381. RID rid = body_owner.make_rid(body);
  382. body->set_self(rid);
  383. return rid;
  384. };
  385. void Physics2DServerSW::body_set_space(RID p_body, RID p_space) {
  386. Body2DSW *body = body_owner.get(p_body);
  387. ERR_FAIL_COND(!body);
  388. Space2DSW *space = NULL;
  389. if (p_space.is_valid()) {
  390. space = space_owner.get(p_space);
  391. ERR_FAIL_COND(!space);
  392. }
  393. if (body->get_space() == space)
  394. return; //pointless
  395. body->clear_constraint_map();
  396. body->set_space(space);
  397. };
  398. RID Physics2DServerSW::body_get_space(RID p_body) const {
  399. Body2DSW *body = body_owner.get(p_body);
  400. ERR_FAIL_COND_V(!body, RID());
  401. Space2DSW *space = body->get_space();
  402. if (!space)
  403. return RID();
  404. return space->get_self();
  405. };
  406. void Physics2DServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  407. Body2DSW *body = body_owner.get(p_body);
  408. ERR_FAIL_COND(!body);
  409. body->set_mode(p_mode);
  410. };
  411. Physics2DServer::BodyMode Physics2DServerSW::body_get_mode(RID p_body) const {
  412. Body2DSW *body = body_owner.get(p_body);
  413. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  414. return body->get_mode();
  415. };
  416. void Physics2DServerSW::body_add_shape(RID p_body, RID p_shape, const Matrix32 &p_transform) {
  417. Body2DSW *body = body_owner.get(p_body);
  418. ERR_FAIL_COND(!body);
  419. Shape2DSW *shape = shape_owner.get(p_shape);
  420. ERR_FAIL_COND(!shape);
  421. body->add_shape(shape, p_transform);
  422. }
  423. void Physics2DServerSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  424. Body2DSW *body = body_owner.get(p_body);
  425. ERR_FAIL_COND(!body);
  426. Shape2DSW *shape = shape_owner.get(p_shape);
  427. ERR_FAIL_COND(!shape);
  428. ERR_FAIL_COND(!shape->is_configured());
  429. body->set_shape(p_shape_idx, shape);
  430. }
  431. void Physics2DServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Matrix32 &p_transform) {
  432. Body2DSW *body = body_owner.get(p_body);
  433. ERR_FAIL_COND(!body);
  434. body->set_shape_transform(p_shape_idx, p_transform);
  435. }
  436. void Physics2DServerSW::body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) {
  437. Body2DSW *body = body_owner.get(p_body);
  438. ERR_FAIL_COND(!body);
  439. body->set_shape_metadata(p_shape_idx, p_metadata);
  440. }
  441. Variant Physics2DServerSW::body_get_shape_metadata(RID p_body, int p_shape_idx) const {
  442. Body2DSW *body = body_owner.get(p_body);
  443. ERR_FAIL_COND_V(!body, Variant());
  444. return body->get_shape_metadata(p_shape_idx);
  445. }
  446. int Physics2DServerSW::body_get_shape_count(RID p_body) const {
  447. Body2DSW *body = body_owner.get(p_body);
  448. ERR_FAIL_COND_V(!body, -1);
  449. return body->get_shape_count();
  450. }
  451. RID Physics2DServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  452. Body2DSW *body = body_owner.get(p_body);
  453. ERR_FAIL_COND_V(!body, RID());
  454. Shape2DSW *shape = body->get_shape(p_shape_idx);
  455. ERR_FAIL_COND_V(!shape, RID());
  456. return shape->get_self();
  457. }
  458. Matrix32 Physics2DServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  459. Body2DSW *body = body_owner.get(p_body);
  460. ERR_FAIL_COND_V(!body, Matrix32());
  461. return body->get_shape_transform(p_shape_idx);
  462. }
  463. void Physics2DServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  464. Body2DSW *body = body_owner.get(p_body);
  465. ERR_FAIL_COND(!body);
  466. body->remove_shape(p_shape_idx);
  467. }
  468. void Physics2DServerSW::body_clear_shapes(RID p_body) {
  469. Body2DSW *body = body_owner.get(p_body);
  470. ERR_FAIL_COND(!body);
  471. while (body->get_shape_count())
  472. body->remove_shape(0);
  473. }
  474. void Physics2DServerSW::body_set_shape_as_trigger(RID p_body, int p_shape_idx, bool p_enable) {
  475. Body2DSW *body = body_owner.get(p_body);
  476. ERR_FAIL_COND(!body);
  477. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  478. body->set_shape_as_trigger(p_shape_idx, p_enable);
  479. }
  480. bool Physics2DServerSW::body_is_shape_set_as_trigger(RID p_body, int p_shape_idx) const {
  481. const Body2DSW *body = body_owner.get(p_body);
  482. ERR_FAIL_COND_V(!body, false);
  483. ERR_FAIL_INDEX_V(p_shape_idx, body->get_shape_count(), false);
  484. return body->is_shape_set_as_trigger(p_shape_idx);
  485. }
  486. void Physics2DServerSW::body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) {
  487. Body2DSW *body = body_owner.get(p_body);
  488. ERR_FAIL_COND(!body);
  489. body->set_continuous_collision_detection_mode(p_mode);
  490. }
  491. Physics2DServerSW::CCDMode Physics2DServerSW::body_get_continuous_collision_detection_mode(RID p_body) const {
  492. const Body2DSW *body = body_owner.get(p_body);
  493. ERR_FAIL_COND_V(!body, CCD_MODE_DISABLED);
  494. return body->get_continuous_collision_detection_mode();
  495. }
  496. void Physics2DServerSW::body_attach_object_instance_ID(RID p_body, uint32_t p_ID) {
  497. Body2DSW *body = body_owner.get(p_body);
  498. ERR_FAIL_COND(!body);
  499. body->set_instance_id(p_ID);
  500. };
  501. uint32_t Physics2DServerSW::body_get_object_instance_ID(RID p_body) const {
  502. Body2DSW *body = body_owner.get(p_body);
  503. ERR_FAIL_COND_V(!body, 0);
  504. return body->get_instance_id();
  505. };
  506. void Physics2DServerSW::body_set_layer_mask(RID p_body, uint32_t p_flags) {
  507. Body2DSW *body = body_owner.get(p_body);
  508. ERR_FAIL_COND(!body);
  509. body->set_layer_mask(p_flags);
  510. };
  511. uint32_t Physics2DServerSW::body_get_layer_mask(RID p_body) const {
  512. Body2DSW *body = body_owner.get(p_body);
  513. ERR_FAIL_COND_V(!body, 0);
  514. return body->get_layer_mask();
  515. };
  516. void Physics2DServerSW::body_set_collision_mask(RID p_body, uint32_t p_flags) {
  517. Body2DSW *body = body_owner.get(p_body);
  518. ERR_FAIL_COND(!body);
  519. body->set_collision_mask(p_flags);
  520. };
  521. uint32_t Physics2DServerSW::body_get_collision_mask(RID p_body) const {
  522. Body2DSW *body = body_owner.get(p_body);
  523. ERR_FAIL_COND_V(!body, 0);
  524. return body->get_collision_mask();
  525. };
  526. void Physics2DServerSW::body_set_param(RID p_body, BodyParameter p_param, float p_value) {
  527. Body2DSW *body = body_owner.get(p_body);
  528. ERR_FAIL_COND(!body);
  529. body->set_param(p_param, p_value);
  530. };
  531. float Physics2DServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  532. Body2DSW *body = body_owner.get(p_body);
  533. ERR_FAIL_COND_V(!body, 0);
  534. return body->get_param(p_param);
  535. };
  536. void Physics2DServerSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  537. Body2DSW *body = body_owner.get(p_body);
  538. ERR_FAIL_COND(!body);
  539. body->set_state(p_state, p_variant);
  540. };
  541. Variant Physics2DServerSW::body_get_state(RID p_body, BodyState p_state) const {
  542. Body2DSW *body = body_owner.get(p_body);
  543. ERR_FAIL_COND_V(!body, Variant());
  544. return body->get_state(p_state);
  545. };
  546. void Physics2DServerSW::body_set_applied_force(RID p_body, const Vector2 &p_force) {
  547. Body2DSW *body = body_owner.get(p_body);
  548. ERR_FAIL_COND(!body);
  549. body->set_applied_force(p_force);
  550. body->wakeup();
  551. };
  552. Vector2 Physics2DServerSW::body_get_applied_force(RID p_body) const {
  553. Body2DSW *body = body_owner.get(p_body);
  554. ERR_FAIL_COND_V(!body, Vector2());
  555. return body->get_applied_force();
  556. };
  557. void Physics2DServerSW::body_set_applied_torque(RID p_body, float p_torque) {
  558. Body2DSW *body = body_owner.get(p_body);
  559. ERR_FAIL_COND(!body);
  560. body->set_applied_torque(p_torque);
  561. body->wakeup();
  562. };
  563. float Physics2DServerSW::body_get_applied_torque(RID p_body) const {
  564. Body2DSW *body = body_owner.get(p_body);
  565. ERR_FAIL_COND_V(!body, 0);
  566. return body->get_applied_torque();
  567. };
  568. void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse) {
  569. Body2DSW *body = body_owner.get(p_body);
  570. ERR_FAIL_COND(!body);
  571. body->apply_impulse(p_pos, p_impulse);
  572. body->wakeup();
  573. };
  574. void Physics2DServerSW::body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) {
  575. Body2DSW *body = body_owner.get(p_body);
  576. ERR_FAIL_COND(!body);
  577. body->add_force(p_force, p_offset);
  578. body->wakeup();
  579. };
  580. void Physics2DServerSW::body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) {
  581. Body2DSW *body = body_owner.get(p_body);
  582. ERR_FAIL_COND(!body);
  583. Vector2 v = body->get_linear_velocity();
  584. Vector2 axis = p_axis_velocity.normalized();
  585. v -= axis * axis.dot(v);
  586. v += p_axis_velocity;
  587. body->set_linear_velocity(v);
  588. body->wakeup();
  589. };
  590. void Physics2DServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  591. Body2DSW *body = body_owner.get(p_body);
  592. ERR_FAIL_COND(!body);
  593. body->add_exception(p_body_b);
  594. body->wakeup();
  595. };
  596. void Physics2DServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  597. Body2DSW *body = body_owner.get(p_body);
  598. ERR_FAIL_COND(!body);
  599. body->remove_exception(p_body_b);
  600. body->wakeup();
  601. };
  602. void Physics2DServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  603. Body2DSW *body = body_owner.get(p_body);
  604. ERR_FAIL_COND(!body);
  605. for (int i = 0; i < body->get_exceptions().size(); i++) {
  606. p_exceptions->push_back(body->get_exceptions()[i]);
  607. }
  608. };
  609. void Physics2DServerSW::body_set_contacts_reported_depth_treshold(RID p_body, float p_treshold) {
  610. Body2DSW *body = body_owner.get(p_body);
  611. ERR_FAIL_COND(!body);
  612. };
  613. float Physics2DServerSW::body_get_contacts_reported_depth_treshold(RID p_body) const {
  614. Body2DSW *body = body_owner.get(p_body);
  615. ERR_FAIL_COND_V(!body, 0);
  616. return 0;
  617. };
  618. void Physics2DServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  619. Body2DSW *body = body_owner.get(p_body);
  620. ERR_FAIL_COND(!body);
  621. body->set_omit_force_integration(p_omit);
  622. };
  623. bool Physics2DServerSW::body_is_omitting_force_integration(RID p_body) const {
  624. Body2DSW *body = body_owner.get(p_body);
  625. ERR_FAIL_COND_V(!body, false);
  626. return body->get_omit_force_integration();
  627. };
  628. void Physics2DServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  629. Body2DSW *body = body_owner.get(p_body);
  630. ERR_FAIL_COND(!body);
  631. body->set_max_contacts_reported(p_contacts);
  632. }
  633. int Physics2DServerSW::body_get_max_contacts_reported(RID p_body) const {
  634. Body2DSW *body = body_owner.get(p_body);
  635. ERR_FAIL_COND_V(!body, -1);
  636. return body->get_max_contacts_reported();
  637. }
  638. void Physics2DServerSW::body_set_one_way_collision_direction(RID p_body, const Vector2 &p_direction) {
  639. Body2DSW *body = body_owner.get(p_body);
  640. ERR_FAIL_COND(!body);
  641. body->set_one_way_collision_direction(p_direction);
  642. }
  643. Vector2 Physics2DServerSW::body_get_one_way_collision_direction(RID p_body) const {
  644. Body2DSW *body = body_owner.get(p_body);
  645. ERR_FAIL_COND_V(!body, Vector2());
  646. return body->get_one_way_collision_direction();
  647. }
  648. void Physics2DServerSW::body_set_one_way_collision_max_depth(RID p_body, float p_max_depth) {
  649. Body2DSW *body = body_owner.get(p_body);
  650. ERR_FAIL_COND(!body);
  651. body->set_one_way_collision_max_depth(p_max_depth);
  652. }
  653. float Physics2DServerSW::body_get_one_way_collision_max_depth(RID p_body) const {
  654. Body2DSW *body = body_owner.get(p_body);
  655. ERR_FAIL_COND_V(!body, 0);
  656. return body->get_one_way_collision_max_depth();
  657. }
  658. void Physics2DServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  659. Body2DSW *body = body_owner.get(p_body);
  660. ERR_FAIL_COND(!body);
  661. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_ID() : ObjectID(0), p_method, p_udata);
  662. }
  663. bool Physics2DServerSW::body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Matrix32 &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) {
  664. Body2DSW *body = body_owner.get(p_body);
  665. ERR_FAIL_COND_V(!body, false);
  666. ERR_FAIL_INDEX_V(p_body_shape, body->get_shape_count(), false);
  667. return shape_collide(body->get_shape(p_body_shape)->get_self(), body->get_transform() * body->get_shape_transform(p_body_shape), Vector2(), p_shape, p_shape_xform, p_motion, r_results, p_result_max, r_result_count);
  668. }
  669. void Physics2DServerSW::body_set_pickable(RID p_body, bool p_pickable) {
  670. Body2DSW *body = body_owner.get(p_body);
  671. ERR_FAIL_COND(!body);
  672. body->set_pickable(p_pickable);
  673. }
  674. bool Physics2DServerSW::body_test_motion(RID p_body, const Vector2 &p_motion, float p_margin, MotionResult *r_result) {
  675. Body2DSW *body = body_owner.get(p_body);
  676. ERR_FAIL_COND_V(!body, false);
  677. ERR_FAIL_COND_V(!body->get_space(), false);
  678. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  679. // Since this is the old-style, broken version, the transform to be used
  680. // is that the physics server is aware of
  681. return body->get_space()->test_body_motion(body, body->get_transform(), p_motion, p_margin, r_result);
  682. }
  683. bool Physics2DServerSW::body_test_motion_from(RID p_body, const Matrix32 &p_from, const Vector2 &p_motion, float p_margin, MotionResult *r_result) {
  684. Body2DSW *body = body_owner.get(p_body);
  685. ERR_FAIL_COND_V(!body, false);
  686. ERR_FAIL_COND_V(!body->get_space(), false);
  687. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  688. return body->get_space()->test_body_motion(body, p_from, p_motion, p_margin, r_result);
  689. }
  690. /* JOINT API */
  691. void Physics2DServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  692. Joint2DSW *joint = joint_owner.get(p_joint);
  693. ERR_FAIL_COND(!joint);
  694. switch (p_param) {
  695. case JOINT_PARAM_BIAS: joint->set_bias(p_value); break;
  696. case JOINT_PARAM_MAX_BIAS: joint->set_max_bias(p_value); break;
  697. case JOINT_PARAM_MAX_FORCE: joint->set_max_force(p_value); break;
  698. }
  699. }
  700. real_t Physics2DServerSW::joint_get_param(RID p_joint, JointParam p_param) const {
  701. const Joint2DSW *joint = joint_owner.get(p_joint);
  702. ERR_FAIL_COND_V(!joint, -1);
  703. switch (p_param) {
  704. case JOINT_PARAM_BIAS: return joint->get_bias(); break;
  705. case JOINT_PARAM_MAX_BIAS: return joint->get_max_bias(); break;
  706. case JOINT_PARAM_MAX_FORCE: return joint->get_max_force(); break;
  707. }
  708. return 0;
  709. }
  710. RID Physics2DServerSW::pin_joint_create(const Vector2 &p_pos, RID p_body_a, RID p_body_b) {
  711. Body2DSW *A = body_owner.get(p_body_a);
  712. ERR_FAIL_COND_V(!A, RID());
  713. Body2DSW *B = NULL;
  714. if (body_owner.owns(p_body_b)) {
  715. B = body_owner.get(p_body_b);
  716. ERR_FAIL_COND_V(!B, RID());
  717. }
  718. Joint2DSW *joint = memnew(PinJoint2DSW(p_pos, A, B));
  719. RID self = joint_owner.make_rid(joint);
  720. joint->set_self(self);
  721. return self;
  722. }
  723. RID Physics2DServerSW::groove_joint_create(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b) {
  724. Body2DSW *A = body_owner.get(p_body_a);
  725. ERR_FAIL_COND_V(!A, RID());
  726. Body2DSW *B = body_owner.get(p_body_b);
  727. ERR_FAIL_COND_V(!B, RID());
  728. Joint2DSW *joint = memnew(GrooveJoint2DSW(p_a_groove1, p_a_groove2, p_b_anchor, A, B));
  729. RID self = joint_owner.make_rid(joint);
  730. joint->set_self(self);
  731. return self;
  732. }
  733. RID Physics2DServerSW::damped_spring_joint_create(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b) {
  734. Body2DSW *A = body_owner.get(p_body_a);
  735. ERR_FAIL_COND_V(!A, RID());
  736. Body2DSW *B = body_owner.get(p_body_b);
  737. ERR_FAIL_COND_V(!B, RID());
  738. Joint2DSW *joint = memnew(DampedSpringJoint2DSW(p_anchor_a, p_anchor_b, A, B));
  739. RID self = joint_owner.make_rid(joint);
  740. joint->set_self(self);
  741. return self;
  742. }
  743. void Physics2DServerSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  744. Joint2DSW *j = joint_owner.get(p_joint);
  745. ERR_FAIL_COND(!j);
  746. ERR_FAIL_COND(j->get_type() != JOINT_PIN);
  747. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  748. pin_joint->set_param(p_param, p_value);
  749. }
  750. real_t Physics2DServerSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  751. Joint2DSW *j = joint_owner.get(p_joint);
  752. ERR_FAIL_COND_V(!j, 0);
  753. ERR_FAIL_COND_V(j->get_type() != JOINT_PIN, 0);
  754. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  755. return pin_joint->get_param(p_param);
  756. }
  757. void Physics2DServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) {
  758. Joint2DSW *j = joint_owner.get(p_joint);
  759. ERR_FAIL_COND(!j);
  760. ERR_FAIL_COND(j->get_type() != JOINT_DAMPED_SPRING);
  761. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  762. dsj->set_param(p_param, p_value);
  763. }
  764. real_t Physics2DServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const {
  765. Joint2DSW *j = joint_owner.get(p_joint);
  766. ERR_FAIL_COND_V(!j, 0);
  767. ERR_FAIL_COND_V(j->get_type() != JOINT_DAMPED_SPRING, 0);
  768. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  769. return dsj->get_param(p_param);
  770. }
  771. Physics2DServer::JointType Physics2DServerSW::joint_get_type(RID p_joint) const {
  772. Joint2DSW *joint = joint_owner.get(p_joint);
  773. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  774. return joint->get_type();
  775. }
  776. void Physics2DServerSW::free(RID p_rid) {
  777. if (shape_owner.owns(p_rid)) {
  778. Shape2DSW *shape = shape_owner.get(p_rid);
  779. while (shape->get_owners().size()) {
  780. ShapeOwner2DSW *so = shape->get_owners().front()->key();
  781. so->remove_shape(shape);
  782. }
  783. shape_owner.free(p_rid);
  784. memdelete(shape);
  785. } else if (body_owner.owns(p_rid)) {
  786. Body2DSW *body = body_owner.get(p_rid);
  787. // if (body->get_state_query())
  788. // _clear_query(body->get_state_query());
  789. // if (body->get_direct_state_query())
  790. // _clear_query(body->get_direct_state_query());
  791. body_set_space(p_rid, RID());
  792. while (body->get_shape_count()) {
  793. body->remove_shape(0);
  794. }
  795. body_owner.free(p_rid);
  796. memdelete(body);
  797. } else if (area_owner.owns(p_rid)) {
  798. Area2DSW *area = area_owner.get(p_rid);
  799. // if (area->get_monitor_query())
  800. // _clear_query(area->get_monitor_query());
  801. area_set_space(p_rid, RID());
  802. while (area->get_shape_count()) {
  803. area->remove_shape(0);
  804. }
  805. area_owner.free(p_rid);
  806. memdelete(area);
  807. } else if (space_owner.owns(p_rid)) {
  808. Space2DSW *space = space_owner.get(p_rid);
  809. while (space->get_objects().size()) {
  810. CollisionObject2DSW *co = (CollisionObject2DSW *)space->get_objects().front()->get();
  811. co->set_space(NULL);
  812. }
  813. active_spaces.erase(space);
  814. free(space->get_default_area()->get_self());
  815. space_owner.free(p_rid);
  816. memdelete(space);
  817. } else if (joint_owner.owns(p_rid)) {
  818. Joint2DSW *joint = joint_owner.get(p_rid);
  819. joint_owner.free(p_rid);
  820. memdelete(joint);
  821. } else {
  822. ERR_EXPLAIN("Invalid ID");
  823. ERR_FAIL();
  824. }
  825. };
  826. void Physics2DServerSW::set_active(bool p_active) {
  827. active = p_active;
  828. };
  829. void Physics2DServerSW::init() {
  830. doing_sync = false;
  831. last_step = 0.001;
  832. iterations = 8; // 8?
  833. stepper = memnew(Step2DSW);
  834. direct_state = memnew(Physics2DDirectBodyStateSW);
  835. };
  836. void Physics2DServerSW::step(float p_step) {
  837. if (!active)
  838. return;
  839. doing_sync = false;
  840. last_step = p_step;
  841. Physics2DDirectBodyStateSW::singleton->step = p_step;
  842. island_count = 0;
  843. active_objects = 0;
  844. collision_pairs = 0;
  845. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  846. stepper->step((Space2DSW *)E->get(), p_step, iterations);
  847. island_count += E->get()->get_island_count();
  848. active_objects += E->get()->get_active_objects();
  849. collision_pairs += E->get()->get_collision_pairs();
  850. }
  851. };
  852. void Physics2DServerSW::sync() {
  853. doing_sync = true;
  854. };
  855. void Physics2DServerSW::flush_queries() {
  856. if (!active)
  857. return;
  858. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  859. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  860. Space2DSW *space = (Space2DSW *)E->get();
  861. space->call_queries();
  862. }
  863. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  864. uint64_t total_time[Space2DSW::ELAPSED_TIME_MAX];
  865. static const char *time_name[Space2DSW::ELAPSED_TIME_MAX] = {
  866. "integrate_forces",
  867. "generate_islands",
  868. "setup_constraints",
  869. "solve_constraints",
  870. "integrate_velocities"
  871. };
  872. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  873. total_time[i] = 0;
  874. }
  875. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  876. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  877. total_time[i] += E->get()->get_elapsed_time(Space2DSW::ElapsedTime(i));
  878. }
  879. }
  880. Array values;
  881. values.resize(Space2DSW::ELAPSED_TIME_MAX * 2);
  882. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  883. values[i * 2 + 0] = time_name[i];
  884. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  885. }
  886. values.push_back("flush_queries");
  887. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  888. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics_2d", values);
  889. }
  890. }
  891. void Physics2DServerSW::end_sync() {
  892. doing_sync = false;
  893. }
  894. void Physics2DServerSW::finish() {
  895. memdelete(stepper);
  896. memdelete(direct_state);
  897. };
  898. int Physics2DServerSW::get_process_info(ProcessInfo p_info) {
  899. switch (p_info) {
  900. case INFO_ACTIVE_OBJECTS: {
  901. return active_objects;
  902. } break;
  903. case INFO_COLLISION_PAIRS: {
  904. return collision_pairs;
  905. } break;
  906. case INFO_ISLAND_COUNT: {
  907. return island_count;
  908. } break;
  909. }
  910. return 0;
  911. }
  912. Physics2DServerSW *Physics2DServerSW::singletonsw = NULL;
  913. Physics2DServerSW::Physics2DServerSW() {
  914. singletonsw = this;
  915. BroadPhase2DSW::create_func = BroadPhase2DHashGrid::_create;
  916. //BroadPhase2DSW::create_func = BroadPhase2DBasic::_create;
  917. active = true;
  918. island_count = 0;
  919. active_objects = 0;
  920. collision_pairs = 0;
  921. using_threads = int(Globals::get_singleton()->get("physics_2d/thread_model")) == 2;
  922. };
  923. Physics2DServerSW::~Physics2DServerSW(){
  924. };