physics_2d_server_sw.cpp 39 KB

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