physics_2d_server_sw.cpp 35 KB

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