physics_2d_server_sw.cpp 39 KB

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