godot_physics_server_2d.cpp 41 KB

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