physics_2d_server_sw.cpp 39 KB

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