physics_2d_server_sw.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_bvh.h"
  33. #include "broad_phase_2d_hash_grid.h"
  34. #include "collision_solver_2d_sw.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "core/script_language.h"
  38. #define FLUSH_QUERY_CHECK(m_object) \
  39. ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");
  40. RID Physics2DServerSW::_shape_create(ShapeType p_shape) {
  41. Shape2DSW *shape = nullptr;
  42. switch (p_shape) {
  43. case SHAPE_LINE: {
  44. shape = memnew(LineShape2DSW);
  45. } break;
  46. case SHAPE_RAY: {
  47. shape = memnew(RayShape2DSW);
  48. } break;
  49. case SHAPE_SEGMENT: {
  50. shape = memnew(SegmentShape2DSW);
  51. } break;
  52. case SHAPE_CIRCLE: {
  53. shape = memnew(CircleShape2DSW);
  54. } break;
  55. case SHAPE_RECTANGLE: {
  56. shape = memnew(RectangleShape2DSW);
  57. } break;
  58. case SHAPE_CAPSULE: {
  59. shape = memnew(CapsuleShape2DSW);
  60. } break;
  61. case SHAPE_CONVEX_POLYGON: {
  62. shape = memnew(ConvexPolygonShape2DSW);
  63. } break;
  64. case SHAPE_CONCAVE_POLYGON: {
  65. shape = memnew(ConcavePolygonShape2DSW);
  66. } break;
  67. case SHAPE_CUSTOM: {
  68. ERR_FAIL_V(RID());
  69. } break;
  70. }
  71. RID id = shape_owner.make_rid(shape);
  72. shape->set_self(id);
  73. return id;
  74. }
  75. RID Physics2DServerSW::line_shape_create() {
  76. return _shape_create(SHAPE_LINE);
  77. }
  78. RID Physics2DServerSW::ray_shape_create() {
  79. return _shape_create(SHAPE_RAY);
  80. }
  81. RID Physics2DServerSW::segment_shape_create() {
  82. return _shape_create(SHAPE_SEGMENT);
  83. }
  84. RID Physics2DServerSW::circle_shape_create() {
  85. return _shape_create(SHAPE_CIRCLE);
  86. }
  87. RID Physics2DServerSW::rectangle_shape_create() {
  88. return _shape_create(SHAPE_RECTANGLE);
  89. }
  90. RID Physics2DServerSW::capsule_shape_create() {
  91. return _shape_create(SHAPE_CAPSULE);
  92. }
  93. RID Physics2DServerSW::convex_polygon_shape_create() {
  94. return _shape_create(SHAPE_CONVEX_POLYGON);
  95. }
  96. RID Physics2DServerSW::concave_polygon_shape_create() {
  97. return _shape_create(SHAPE_CONCAVE_POLYGON);
  98. }
  99. void Physics2DServerSW::shape_set_data(RID p_shape, const Variant &p_data) {
  100. Shape2DSW *shape = shape_owner.get(p_shape);
  101. ERR_FAIL_COND(!shape);
  102. shape->set_data(p_data);
  103. };
  104. void Physics2DServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  105. Shape2DSW *shape = shape_owner.get(p_shape);
  106. ERR_FAIL_COND(!shape);
  107. shape->set_custom_bias(p_bias);
  108. }
  109. Physics2DServer::ShapeType Physics2DServerSW::shape_get_type(RID p_shape) const {
  110. const Shape2DSW *shape = shape_owner.get(p_shape);
  111. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  112. return shape->get_type();
  113. };
  114. Variant Physics2DServerSW::shape_get_data(RID p_shape) const {
  115. const Shape2DSW *shape = shape_owner.get(p_shape);
  116. ERR_FAIL_COND_V(!shape, Variant());
  117. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  118. return shape->get_data();
  119. };
  120. real_t Physics2DServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  121. const Shape2DSW *shape = shape_owner.get(p_shape);
  122. ERR_FAIL_COND_V(!shape, 0);
  123. return shape->get_custom_bias();
  124. }
  125. void Physics2DServerSW::_shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
  126. CollCbkData *cbk = (CollCbkData *)p_userdata;
  127. if (cbk->max == 0) {
  128. return;
  129. }
  130. Vector2 rel_dir = (p_point_A - p_point_B);
  131. real_t rel_length2 = rel_dir.length_squared();
  132. if (cbk->valid_dir != Vector2()) {
  133. if (cbk->valid_depth < 10e20) {
  134. if (rel_length2 > cbk->valid_depth * cbk->valid_depth ||
  135. (rel_length2 > CMP_EPSILON && cbk->valid_dir.dot(rel_dir.normalized()) < CMP_EPSILON)) {
  136. cbk->invalid_by_dir++;
  137. return;
  138. }
  139. } else {
  140. if (rel_length2 > 0 && cbk->valid_dir.dot(rel_dir.normalized()) < CMP_EPSILON) {
  141. return;
  142. }
  143. }
  144. }
  145. if (cbk->amount == cbk->max) {
  146. //find least deep
  147. real_t min_depth = 1e20;
  148. int min_depth_idx = 0;
  149. for (int i = 0; i < cbk->amount; i++) {
  150. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  151. if (d < min_depth) {
  152. min_depth = d;
  153. min_depth_idx = i;
  154. }
  155. }
  156. if (rel_length2 < min_depth) {
  157. return;
  158. }
  159. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  160. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  161. cbk->passed++;
  162. } else {
  163. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  164. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  165. cbk->amount++;
  166. cbk->passed++;
  167. }
  168. }
  169. 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) {
  170. Shape2DSW *shape_A = shape_owner.get(p_shape_A);
  171. ERR_FAIL_COND_V(!shape_A, false);
  172. Shape2DSW *shape_B = shape_owner.get(p_shape_B);
  173. ERR_FAIL_COND_V(!shape_B, false);
  174. if (p_result_max == 0) {
  175. return CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, nullptr, nullptr);
  176. }
  177. CollCbkData cbk;
  178. cbk.max = p_result_max;
  179. cbk.amount = 0;
  180. cbk.passed = 0;
  181. cbk.ptr = r_results;
  182. bool res = CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, _shape_col_cbk, &cbk);
  183. r_result_count = cbk.amount;
  184. return res;
  185. }
  186. RID Physics2DServerSW::space_create() {
  187. Space2DSW *space = memnew(Space2DSW);
  188. RID id = space_owner.make_rid(space);
  189. space->set_self(id);
  190. RID area_id = area_create();
  191. Area2DSW *area = area_owner.get(area_id);
  192. ERR_FAIL_COND_V(!area, RID());
  193. space->set_default_area(area);
  194. area->set_space(space);
  195. area->set_priority(-1);
  196. return id;
  197. };
  198. void Physics2DServerSW::space_set_active(RID p_space, bool p_active) {
  199. Space2DSW *space = space_owner.get(p_space);
  200. ERR_FAIL_COND(!space);
  201. if (p_active) {
  202. active_spaces.insert(space);
  203. } else {
  204. active_spaces.erase(space);
  205. }
  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, nullptr);
  240. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  241. return space->get_direct_state();
  242. }
  243. RID Physics2DServerSW::area_create() {
  244. Area2DSW *area = memnew(Area2DSW);
  245. RID rid = area_owner.make_rid(area);
  246. area->set_self(rid);
  247. return rid;
  248. };
  249. void Physics2DServerSW::area_set_space(RID p_area, RID p_space) {
  250. Area2DSW *area = area_owner.get(p_area);
  251. ERR_FAIL_COND(!area);
  252. Space2DSW *space = nullptr;
  253. if (p_space.is_valid()) {
  254. space = space_owner.get(p_space);
  255. ERR_FAIL_COND(!space);
  256. }
  257. if (area->get_space() == space) {
  258. return; //pointless
  259. }
  260. area->clear_constraints();
  261. area->set_space(space);
  262. };
  263. RID Physics2DServerSW::area_get_space(RID p_area) const {
  264. Area2DSW *area = area_owner.get(p_area);
  265. ERR_FAIL_COND_V(!area, RID());
  266. Space2DSW *space = area->get_space();
  267. if (!space) {
  268. return RID();
  269. }
  270. return space->get_self();
  271. };
  272. void Physics2DServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  273. Area2DSW *area = area_owner.get(p_area);
  274. ERR_FAIL_COND(!area);
  275. area->set_space_override_mode(p_mode);
  276. }
  277. Physics2DServer::AreaSpaceOverrideMode Physics2DServerSW::area_get_space_override_mode(RID p_area) const {
  278. const Area2DSW *area = area_owner.get(p_area);
  279. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  280. return area->get_space_override_mode();
  281. }
  282. void Physics2DServerSW::area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
  283. Area2DSW *area = area_owner.get(p_area);
  284. ERR_FAIL_COND(!area);
  285. Shape2DSW *shape = shape_owner.get(p_shape);
  286. ERR_FAIL_COND(!shape);
  287. area->add_shape(shape, p_transform, p_disabled);
  288. }
  289. void Physics2DServerSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  290. Area2DSW *area = area_owner.get(p_area);
  291. ERR_FAIL_COND(!area);
  292. Shape2DSW *shape = shape_owner.get(p_shape);
  293. ERR_FAIL_COND(!shape);
  294. ERR_FAIL_COND(!shape->is_configured());
  295. area->set_shape(p_shape_idx, shape);
  296. }
  297. void Physics2DServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) {
  298. Area2DSW *area = area_owner.get(p_area);
  299. ERR_FAIL_COND(!area);
  300. area->set_shape_transform(p_shape_idx, p_transform);
  301. }
  302. void Physics2DServerSW::area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) {
  303. Area2DSW *area = area_owner.get(p_area);
  304. ERR_FAIL_COND(!area);
  305. ERR_FAIL_INDEX(p_shape, area->get_shape_count());
  306. FLUSH_QUERY_CHECK(area);
  307. area->set_shape_disabled(p_shape, p_disabled);
  308. }
  309. int Physics2DServerSW::area_get_shape_count(RID p_area) const {
  310. Area2DSW *area = area_owner.get(p_area);
  311. ERR_FAIL_COND_V(!area, -1);
  312. return area->get_shape_count();
  313. }
  314. RID Physics2DServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  315. Area2DSW *area = area_owner.get(p_area);
  316. ERR_FAIL_COND_V(!area, RID());
  317. Shape2DSW *shape = area->get_shape(p_shape_idx);
  318. ERR_FAIL_COND_V(!shape, RID());
  319. return shape->get_self();
  320. }
  321. Transform2D Physics2DServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  322. Area2DSW *area = area_owner.get(p_area);
  323. ERR_FAIL_COND_V(!area, Transform2D());
  324. return area->get_shape_transform(p_shape_idx);
  325. }
  326. void Physics2DServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  327. Area2DSW *area = area_owner.get(p_area);
  328. ERR_FAIL_COND(!area);
  329. area->remove_shape(p_shape_idx);
  330. }
  331. void Physics2DServerSW::area_clear_shapes(RID p_area) {
  332. Area2DSW *area = area_owner.get(p_area);
  333. ERR_FAIL_COND(!area);
  334. while (area->get_shape_count()) {
  335. area->remove_shape(0);
  336. }
  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. Area2DSW *area = area_owner.get(p_area);
  409. ERR_FAIL_COND(!area);
  410. FLUSH_QUERY_CHECK(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 = nullptr;
  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. }
  451. body->clear_constraint_map();
  452. body->set_space(space);
  453. };
  454. RID Physics2DServerSW::body_get_space(RID p_body) const {
  455. Body2DSW *body = body_owner.get(p_body);
  456. ERR_FAIL_COND_V(!body, RID());
  457. Space2DSW *space = body->get_space();
  458. if (!space) {
  459. return RID();
  460. }
  461. return space->get_self();
  462. };
  463. void Physics2DServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  464. Body2DSW *body = body_owner.get(p_body);
  465. ERR_FAIL_COND(!body);
  466. FLUSH_QUERY_CHECK(body);
  467. body->set_mode(p_mode);
  468. };
  469. Physics2DServer::BodyMode Physics2DServerSW::body_get_mode(RID p_body) const {
  470. Body2DSW *body = body_owner.get(p_body);
  471. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  472. return body->get_mode();
  473. };
  474. void Physics2DServerSW::body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
  475. Body2DSW *body = body_owner.get(p_body);
  476. ERR_FAIL_COND(!body);
  477. Shape2DSW *shape = shape_owner.get(p_shape);
  478. ERR_FAIL_COND(!shape);
  479. body->add_shape(shape, p_transform, p_disabled);
  480. }
  481. void Physics2DServerSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  482. Body2DSW *body = body_owner.get(p_body);
  483. ERR_FAIL_COND(!body);
  484. Shape2DSW *shape = shape_owner.get(p_shape);
  485. ERR_FAIL_COND(!shape);
  486. ERR_FAIL_COND(!shape->is_configured());
  487. body->set_shape(p_shape_idx, shape);
  488. }
  489. void Physics2DServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) {
  490. Body2DSW *body = body_owner.get(p_body);
  491. ERR_FAIL_COND(!body);
  492. body->set_shape_transform(p_shape_idx, p_transform);
  493. }
  494. void Physics2DServerSW::body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) {
  495. Body2DSW *body = body_owner.get(p_body);
  496. ERR_FAIL_COND(!body);
  497. body->set_shape_metadata(p_shape_idx, p_metadata);
  498. }
  499. Variant Physics2DServerSW::body_get_shape_metadata(RID p_body, int p_shape_idx) const {
  500. Body2DSW *body = body_owner.get(p_body);
  501. ERR_FAIL_COND_V(!body, Variant());
  502. return body->get_shape_metadata(p_shape_idx);
  503. }
  504. int Physics2DServerSW::body_get_shape_count(RID p_body) const {
  505. Body2DSW *body = body_owner.get(p_body);
  506. ERR_FAIL_COND_V(!body, -1);
  507. return body->get_shape_count();
  508. }
  509. RID Physics2DServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  510. Body2DSW *body = body_owner.get(p_body);
  511. ERR_FAIL_COND_V(!body, RID());
  512. Shape2DSW *shape = body->get_shape(p_shape_idx);
  513. ERR_FAIL_COND_V(!shape, RID());
  514. return shape->get_self();
  515. }
  516. Transform2D Physics2DServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  517. Body2DSW *body = body_owner.get(p_body);
  518. ERR_FAIL_COND_V(!body, Transform2D());
  519. return body->get_shape_transform(p_shape_idx);
  520. }
  521. void Physics2DServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  522. Body2DSW *body = body_owner.get(p_body);
  523. ERR_FAIL_COND(!body);
  524. body->remove_shape(p_shape_idx);
  525. }
  526. void Physics2DServerSW::body_clear_shapes(RID p_body) {
  527. Body2DSW *body = body_owner.get(p_body);
  528. ERR_FAIL_COND(!body);
  529. while (body->get_shape_count()) {
  530. body->remove_shape(0);
  531. }
  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_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. _update_shapes();
  649. body->apply_torque_impulse(p_torque);
  650. body->wakeup();
  651. }
  652. void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse) {
  653. Body2DSW *body = body_owner.get(p_body);
  654. ERR_FAIL_COND(!body);
  655. _update_shapes();
  656. body->apply_impulse(p_pos, p_impulse);
  657. body->wakeup();
  658. };
  659. void Physics2DServerSW::body_add_central_force(RID p_body, const Vector2 &p_force) {
  660. Body2DSW *body = body_owner.get(p_body);
  661. ERR_FAIL_COND(!body);
  662. body->add_central_force(p_force);
  663. body->wakeup();
  664. };
  665. void Physics2DServerSW::body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) {
  666. Body2DSW *body = body_owner.get(p_body);
  667. ERR_FAIL_COND(!body);
  668. body->add_force(p_offset, p_force);
  669. body->wakeup();
  670. };
  671. void Physics2DServerSW::body_add_torque(RID p_body, real_t p_torque) {
  672. Body2DSW *body = body_owner.get(p_body);
  673. ERR_FAIL_COND(!body);
  674. body->add_torque(p_torque);
  675. body->wakeup();
  676. };
  677. void Physics2DServerSW::body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) {
  678. Body2DSW *body = body_owner.get(p_body);
  679. ERR_FAIL_COND(!body);
  680. _update_shapes();
  681. Vector2 v = body->get_linear_velocity();
  682. Vector2 axis = p_axis_velocity.normalized();
  683. v -= axis * axis.dot(v);
  684. v += p_axis_velocity;
  685. body->set_linear_velocity(v);
  686. body->wakeup();
  687. };
  688. void Physics2DServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  689. Body2DSW *body = body_owner.get(p_body);
  690. ERR_FAIL_COND(!body);
  691. body->add_exception(p_body_b);
  692. body->wakeup();
  693. };
  694. void Physics2DServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  695. Body2DSW *body = body_owner.get(p_body);
  696. ERR_FAIL_COND(!body);
  697. body->remove_exception(p_body_b);
  698. body->wakeup();
  699. };
  700. void Physics2DServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  701. Body2DSW *body = body_owner.get(p_body);
  702. ERR_FAIL_COND(!body);
  703. for (int i = 0; i < body->get_exceptions().size(); i++) {
  704. p_exceptions->push_back(body->get_exceptions()[i]);
  705. }
  706. };
  707. void Physics2DServerSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  708. Body2DSW *body = body_owner.get(p_body);
  709. ERR_FAIL_COND(!body);
  710. };
  711. real_t Physics2DServerSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  712. Body2DSW *body = body_owner.get(p_body);
  713. ERR_FAIL_COND_V(!body, 0);
  714. return 0;
  715. };
  716. void Physics2DServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  717. Body2DSW *body = body_owner.get(p_body);
  718. ERR_FAIL_COND(!body);
  719. body->set_omit_force_integration(p_omit);
  720. };
  721. bool Physics2DServerSW::body_is_omitting_force_integration(RID p_body) const {
  722. Body2DSW *body = body_owner.get(p_body);
  723. ERR_FAIL_COND_V(!body, false);
  724. return body->get_omit_force_integration();
  725. };
  726. void Physics2DServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  727. Body2DSW *body = body_owner.get(p_body);
  728. ERR_FAIL_COND(!body);
  729. body->set_max_contacts_reported(p_contacts);
  730. }
  731. int Physics2DServerSW::body_get_max_contacts_reported(RID p_body) const {
  732. Body2DSW *body = body_owner.get(p_body);
  733. ERR_FAIL_COND_V(!body, -1);
  734. return body->get_max_contacts_reported();
  735. }
  736. void Physics2DServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  737. Body2DSW *body = body_owner.get(p_body);
  738. ERR_FAIL_COND(!body);
  739. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  740. }
  741. 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) {
  742. Body2DSW *body = body_owner.get(p_body);
  743. ERR_FAIL_COND_V(!body, false);
  744. ERR_FAIL_INDEX_V(p_body_shape, body->get_shape_count(), false);
  745. 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);
  746. }
  747. void Physics2DServerSW::body_set_pickable(RID p_body, bool p_pickable) {
  748. Body2DSW *body = body_owner.get(p_body);
  749. ERR_FAIL_COND(!body);
  750. body->set_pickable(p_pickable);
  751. }
  752. 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, const Set<RID> &p_exclude) {
  753. Body2DSW *body = body_owner.get(p_body);
  754. ERR_FAIL_COND_V(!body, false);
  755. ERR_FAIL_COND_V(!body->get_space(), false);
  756. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  757. _update_shapes();
  758. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, p_margin, r_result, p_exclude_raycast_shapes, p_exclude);
  759. }
  760. 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) {
  761. Body2DSW *body = body_owner.get(p_body);
  762. ERR_FAIL_COND_V(!body, false);
  763. ERR_FAIL_COND_V(!body->get_space(), false);
  764. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  765. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  766. }
  767. Physics2DDirectBodyState *Physics2DServerSW::body_get_direct_state(RID p_body) {
  768. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  769. if (!body_owner.owns(p_body)) {
  770. return nullptr;
  771. }
  772. Body2DSW *body = body_owner.get(p_body);
  773. ERR_FAIL_COND_V(!body, nullptr);
  774. if (!body->get_space()) {
  775. return nullptr;
  776. }
  777. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  778. direct_state->body = body;
  779. return direct_state;
  780. }
  781. /* JOINT API */
  782. void Physics2DServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  783. Joint2DSW *joint = joint_owner.get(p_joint);
  784. ERR_FAIL_COND(!joint);
  785. switch (p_param) {
  786. case JOINT_PARAM_BIAS:
  787. joint->set_bias(p_value);
  788. break;
  789. case JOINT_PARAM_MAX_BIAS:
  790. joint->set_max_bias(p_value);
  791. break;
  792. case JOINT_PARAM_MAX_FORCE:
  793. joint->set_max_force(p_value);
  794. break;
  795. }
  796. }
  797. real_t Physics2DServerSW::joint_get_param(RID p_joint, JointParam p_param) const {
  798. const Joint2DSW *joint = joint_owner.get(p_joint);
  799. ERR_FAIL_COND_V(!joint, -1);
  800. switch (p_param) {
  801. case JOINT_PARAM_BIAS:
  802. return joint->get_bias();
  803. break;
  804. case JOINT_PARAM_MAX_BIAS:
  805. return joint->get_max_bias();
  806. break;
  807. case JOINT_PARAM_MAX_FORCE:
  808. return joint->get_max_force();
  809. break;
  810. }
  811. return 0;
  812. }
  813. void Physics2DServerSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  814. Joint2DSW *joint = joint_owner.get(p_joint);
  815. ERR_FAIL_COND(!joint);
  816. joint->disable_collisions_between_bodies(p_disable);
  817. if (2 == joint->get_body_count()) {
  818. Body2DSW *body_a = *joint->get_body_ptr();
  819. Body2DSW *body_b = *(joint->get_body_ptr() + 1);
  820. if (p_disable) {
  821. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  822. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  823. } else {
  824. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  825. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  826. }
  827. }
  828. }
  829. bool Physics2DServerSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  830. const Joint2DSW *joint = joint_owner.get(p_joint);
  831. ERR_FAIL_COND_V(!joint, true);
  832. return joint->is_disabled_collisions_between_bodies();
  833. }
  834. RID Physics2DServerSW::pin_joint_create(const Vector2 &p_pos, RID p_body_a, RID p_body_b) {
  835. Body2DSW *A = body_owner.get(p_body_a);
  836. ERR_FAIL_COND_V(!A, RID());
  837. Body2DSW *B = nullptr;
  838. if (body_owner.owns(p_body_b)) {
  839. B = body_owner.get(p_body_b);
  840. ERR_FAIL_COND_V(!B, RID());
  841. }
  842. Joint2DSW *joint = memnew(PinJoint2DSW(p_pos, A, B));
  843. RID self = joint_owner.make_rid(joint);
  844. joint->set_self(self);
  845. return self;
  846. }
  847. 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) {
  848. Body2DSW *A = body_owner.get(p_body_a);
  849. ERR_FAIL_COND_V(!A, RID());
  850. Body2DSW *B = body_owner.get(p_body_b);
  851. ERR_FAIL_COND_V(!B, RID());
  852. Joint2DSW *joint = memnew(GrooveJoint2DSW(p_a_groove1, p_a_groove2, p_b_anchor, A, B));
  853. RID self = joint_owner.make_rid(joint);
  854. joint->set_self(self);
  855. return self;
  856. }
  857. RID Physics2DServerSW::damped_spring_joint_create(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b) {
  858. Body2DSW *A = body_owner.get(p_body_a);
  859. ERR_FAIL_COND_V(!A, RID());
  860. Body2DSW *B = body_owner.get(p_body_b);
  861. ERR_FAIL_COND_V(!B, RID());
  862. Joint2DSW *joint = memnew(DampedSpringJoint2DSW(p_anchor_a, p_anchor_b, A, B));
  863. RID self = joint_owner.make_rid(joint);
  864. joint->set_self(self);
  865. return self;
  866. }
  867. void Physics2DServerSW::pin_joint_set_param(RID p_joint, PinJointParam 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_PIN);
  871. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  872. pin_joint->set_param(p_param, p_value);
  873. }
  874. real_t Physics2DServerSW::pin_joint_get_param(RID p_joint, PinJointParam 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_PIN, 0);
  878. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  879. return pin_joint->get_param(p_param);
  880. }
  881. void Physics2DServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) {
  882. Joint2DSW *j = joint_owner.get(p_joint);
  883. ERR_FAIL_COND(!j);
  884. ERR_FAIL_COND(j->get_type() != JOINT_DAMPED_SPRING);
  885. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  886. dsj->set_param(p_param, p_value);
  887. }
  888. real_t Physics2DServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const {
  889. Joint2DSW *j = joint_owner.get(p_joint);
  890. ERR_FAIL_COND_V(!j, 0);
  891. ERR_FAIL_COND_V(j->get_type() != JOINT_DAMPED_SPRING, 0);
  892. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  893. return dsj->get_param(p_param);
  894. }
  895. Physics2DServer::JointType Physics2DServerSW::joint_get_type(RID p_joint) const {
  896. Joint2DSW *joint = joint_owner.get(p_joint);
  897. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  898. return joint->get_type();
  899. }
  900. void Physics2DServerSW::free(RID p_rid) {
  901. _update_shapes(); // just in case
  902. if (shape_owner.owns(p_rid)) {
  903. Shape2DSW *shape = shape_owner.get(p_rid);
  904. while (shape->get_owners().size()) {
  905. ShapeOwner2DSW *so = shape->get_owners().front()->key();
  906. so->remove_shape(shape);
  907. }
  908. shape_owner.free(p_rid);
  909. memdelete(shape);
  910. } else if (body_owner.owns(p_rid)) {
  911. Body2DSW *body = body_owner.get(p_rid);
  912. /*
  913. if (body->get_state_query())
  914. _clear_query(body->get_state_query());
  915. if (body->get_direct_state_query())
  916. _clear_query(body->get_direct_state_query());
  917. */
  918. body_set_space(p_rid, RID());
  919. while (body->get_shape_count()) {
  920. body->remove_shape(0);
  921. }
  922. body_owner.free(p_rid);
  923. memdelete(body);
  924. } else if (area_owner.owns(p_rid)) {
  925. Area2DSW *area = area_owner.get(p_rid);
  926. /*
  927. if (area->get_monitor_query())
  928. _clear_query(area->get_monitor_query());
  929. */
  930. area->set_space(nullptr);
  931. while (area->get_shape_count()) {
  932. area->remove_shape(0);
  933. }
  934. area_owner.free(p_rid);
  935. memdelete(area);
  936. } else if (space_owner.owns(p_rid)) {
  937. Space2DSW *space = space_owner.get(p_rid);
  938. while (space->get_objects().size()) {
  939. CollisionObject2DSW *co = (CollisionObject2DSW *)space->get_objects().front()->get();
  940. co->set_space(nullptr);
  941. }
  942. active_spaces.erase(space);
  943. free(space->get_default_area()->get_self());
  944. space_owner.free(p_rid);
  945. memdelete(space);
  946. } else if (joint_owner.owns(p_rid)) {
  947. Joint2DSW *joint = joint_owner.get(p_rid);
  948. joint_owner.free(p_rid);
  949. memdelete(joint);
  950. } else {
  951. ERR_FAIL_MSG("Invalid ID.");
  952. }
  953. };
  954. void Physics2DServerSW::set_active(bool p_active) {
  955. active = p_active;
  956. };
  957. void Physics2DServerSW::set_collision_iterations(int p_iterations) {
  958. iterations = p_iterations;
  959. };
  960. void Physics2DServerSW::init() {
  961. doing_sync = false;
  962. last_step = 0.001;
  963. iterations = 8; // 8?
  964. stepper = memnew(Step2DSW);
  965. direct_state = memnew(Physics2DDirectBodyStateSW);
  966. };
  967. void Physics2DServerSW::step(real_t p_step) {
  968. if (!active) {
  969. return;
  970. }
  971. _update_shapes();
  972. last_step = p_step;
  973. Physics2DDirectBodyStateSW::singleton->step = p_step;
  974. island_count = 0;
  975. active_objects = 0;
  976. collision_pairs = 0;
  977. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  978. stepper->step((Space2DSW *)E->get(), p_step, iterations);
  979. island_count += E->get()->get_island_count();
  980. active_objects += E->get()->get_active_objects();
  981. collision_pairs += E->get()->get_collision_pairs();
  982. }
  983. };
  984. void Physics2DServerSW::sync() {
  985. doing_sync = true;
  986. };
  987. void Physics2DServerSW::flush_queries() {
  988. if (!active) {
  989. return;
  990. }
  991. flushing_queries = true;
  992. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  993. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  994. Space2DSW *space = (Space2DSW *)E->get();
  995. space->call_queries();
  996. }
  997. flushing_queries = false;
  998. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  999. uint64_t total_time[Space2DSW::ELAPSED_TIME_MAX];
  1000. static const char *time_name[Space2DSW::ELAPSED_TIME_MAX] = {
  1001. "integrate_forces",
  1002. "generate_islands",
  1003. "setup_constraints",
  1004. "solve_constraints",
  1005. "integrate_velocities"
  1006. };
  1007. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  1008. total_time[i] = 0;
  1009. }
  1010. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  1011. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  1012. total_time[i] += E->get()->get_elapsed_time(Space2DSW::ElapsedTime(i));
  1013. }
  1014. }
  1015. Array values;
  1016. values.resize(Space2DSW::ELAPSED_TIME_MAX * 2);
  1017. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  1018. values[i * 2 + 0] = time_name[i];
  1019. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1020. }
  1021. values.push_back("flush_queries");
  1022. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1023. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics_2d", values);
  1024. }
  1025. }
  1026. void Physics2DServerSW::end_sync() {
  1027. doing_sync = false;
  1028. }
  1029. void Physics2DServerSW::finish() {
  1030. memdelete(stepper);
  1031. memdelete(direct_state);
  1032. };
  1033. void Physics2DServerSW::_update_shapes() {
  1034. while (pending_shape_update_list.first()) {
  1035. pending_shape_update_list.first()->self()->_shape_changed();
  1036. pending_shape_update_list.remove(pending_shape_update_list.first());
  1037. }
  1038. }
  1039. int Physics2DServerSW::get_process_info(ProcessInfo p_info) {
  1040. switch (p_info) {
  1041. case INFO_ACTIVE_OBJECTS: {
  1042. return active_objects;
  1043. } break;
  1044. case INFO_COLLISION_PAIRS: {
  1045. return collision_pairs;
  1046. } break;
  1047. case INFO_ISLAND_COUNT: {
  1048. return island_count;
  1049. } break;
  1050. }
  1051. return 0;
  1052. }
  1053. Physics2DServerSW *Physics2DServerSW::singletonsw = nullptr;
  1054. Physics2DServerSW::Physics2DServerSW() {
  1055. singletonsw = this;
  1056. GLOBAL_DEF("physics/2d/use_bvh", true);
  1057. GLOBAL_DEF("physics/2d/bp_hash_table_size", 4096);
  1058. GLOBAL_DEF("physics/2d/cell_size", 128);
  1059. GLOBAL_DEF("physics/2d/large_object_surface_threshold_in_cells", 512);
  1060. GLOBAL_DEF("physics/2d/bvh_collision_margin", 1.0);
  1061. ProjectSettings::get_singleton()->set_custom_property_info("physics/2d/bvh_collision_margin", PropertyInfo(Variant::REAL, "physics/2d/bvh_collision_margin", PROPERTY_HINT_RANGE, "0.0,20.0,0.1"));
  1062. bool use_bvh = GLOBAL_GET("physics/2d/use_bvh");
  1063. if (use_bvh) {
  1064. BroadPhase2DSW::create_func = BroadPhase2DBVH::_create;
  1065. } else {
  1066. BroadPhase2DSW::create_func = BroadPhase2DHashGrid::_create;
  1067. }
  1068. active = true;
  1069. island_count = 0;
  1070. active_objects = 0;
  1071. collision_pairs = 0;
  1072. #ifdef NO_THREADS
  1073. using_threads = false;
  1074. #else
  1075. using_threads = int(GLOBAL_GET("physics/2d/thread_model")) == 2;
  1076. #endif
  1077. flushing_queries = false;
  1078. };
  1079. Physics2DServerSW::~Physics2DServerSW(){
  1080. };