godot_physics_server_2d.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = static_cast<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_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
  272. GodotArea2D *area = area_owner.get_or_null(p_area);
  273. ERR_FAIL_COND(!area);
  274. GodotShape2D *shape = shape_owner.get_or_null(p_shape);
  275. ERR_FAIL_COND(!shape);
  276. area->add_shape(shape, p_transform, p_disabled);
  277. }
  278. void GodotPhysicsServer2D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  279. GodotArea2D *area = area_owner.get_or_null(p_area);
  280. ERR_FAIL_COND(!area);
  281. GodotShape2D *shape = shape_owner.get_or_null(p_shape);
  282. ERR_FAIL_COND(!shape);
  283. ERR_FAIL_COND(!shape->is_configured());
  284. area->set_shape(p_shape_idx, shape);
  285. }
  286. void GodotPhysicsServer2D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) {
  287. GodotArea2D *area = area_owner.get_or_null(p_area);
  288. ERR_FAIL_COND(!area);
  289. area->set_shape_transform(p_shape_idx, p_transform);
  290. }
  291. void GodotPhysicsServer2D::area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) {
  292. GodotArea2D *area = area_owner.get_or_null(p_area);
  293. ERR_FAIL_COND(!area);
  294. ERR_FAIL_INDEX(p_shape, area->get_shape_count());
  295. FLUSH_QUERY_CHECK(area);
  296. area->set_shape_disabled(p_shape, p_disabled);
  297. }
  298. int GodotPhysicsServer2D::area_get_shape_count(RID p_area) const {
  299. GodotArea2D *area = area_owner.get_or_null(p_area);
  300. ERR_FAIL_COND_V(!area, -1);
  301. return area->get_shape_count();
  302. }
  303. RID GodotPhysicsServer2D::area_get_shape(RID p_area, int p_shape_idx) const {
  304. GodotArea2D *area = area_owner.get_or_null(p_area);
  305. ERR_FAIL_COND_V(!area, RID());
  306. GodotShape2D *shape = area->get_shape(p_shape_idx);
  307. ERR_FAIL_COND_V(!shape, RID());
  308. return shape->get_self();
  309. }
  310. Transform2D GodotPhysicsServer2D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  311. GodotArea2D *area = area_owner.get_or_null(p_area);
  312. ERR_FAIL_COND_V(!area, Transform2D());
  313. return area->get_shape_transform(p_shape_idx);
  314. }
  315. void GodotPhysicsServer2D::area_remove_shape(RID p_area, int p_shape_idx) {
  316. GodotArea2D *area = area_owner.get_or_null(p_area);
  317. ERR_FAIL_COND(!area);
  318. area->remove_shape(p_shape_idx);
  319. }
  320. void GodotPhysicsServer2D::area_clear_shapes(RID p_area) {
  321. GodotArea2D *area = area_owner.get_or_null(p_area);
  322. ERR_FAIL_COND(!area);
  323. while (area->get_shape_count()) {
  324. area->remove_shape(0);
  325. }
  326. }
  327. void GodotPhysicsServer2D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  328. if (space_owner.owns(p_area)) {
  329. GodotSpace2D *space = space_owner.get_or_null(p_area);
  330. p_area = space->get_default_area()->get_self();
  331. }
  332. GodotArea2D *area = area_owner.get_or_null(p_area);
  333. ERR_FAIL_COND(!area);
  334. area->set_instance_id(p_id);
  335. }
  336. ObjectID GodotPhysicsServer2D::area_get_object_instance_id(RID p_area) const {
  337. if (space_owner.owns(p_area)) {
  338. GodotSpace2D *space = space_owner.get_or_null(p_area);
  339. p_area = space->get_default_area()->get_self();
  340. }
  341. GodotArea2D *area = area_owner.get_or_null(p_area);
  342. ERR_FAIL_COND_V(!area, ObjectID());
  343. return area->get_instance_id();
  344. }
  345. void GodotPhysicsServer2D::area_attach_canvas_instance_id(RID p_area, ObjectID p_id) {
  346. if (space_owner.owns(p_area)) {
  347. GodotSpace2D *space = space_owner.get_or_null(p_area);
  348. p_area = space->get_default_area()->get_self();
  349. }
  350. GodotArea2D *area = area_owner.get_or_null(p_area);
  351. ERR_FAIL_COND(!area);
  352. area->set_canvas_instance_id(p_id);
  353. }
  354. ObjectID GodotPhysicsServer2D::area_get_canvas_instance_id(RID p_area) const {
  355. if (space_owner.owns(p_area)) {
  356. GodotSpace2D *space = space_owner.get_or_null(p_area);
  357. p_area = space->get_default_area()->get_self();
  358. }
  359. GodotArea2D *area = area_owner.get_or_null(p_area);
  360. ERR_FAIL_COND_V(!area, ObjectID());
  361. return area->get_canvas_instance_id();
  362. }
  363. void GodotPhysicsServer2D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  364. if (space_owner.owns(p_area)) {
  365. GodotSpace2D *space = space_owner.get_or_null(p_area);
  366. p_area = space->get_default_area()->get_self();
  367. }
  368. GodotArea2D *area = area_owner.get_or_null(p_area);
  369. ERR_FAIL_COND(!area);
  370. area->set_param(p_param, p_value);
  371. };
  372. void GodotPhysicsServer2D::area_set_transform(RID p_area, const Transform2D &p_transform) {
  373. GodotArea2D *area = area_owner.get_or_null(p_area);
  374. ERR_FAIL_COND(!area);
  375. area->set_transform(p_transform);
  376. };
  377. Variant GodotPhysicsServer2D::area_get_param(RID p_area, AreaParameter p_param) const {
  378. if (space_owner.owns(p_area)) {
  379. GodotSpace2D *space = space_owner.get_or_null(p_area);
  380. p_area = space->get_default_area()->get_self();
  381. }
  382. GodotArea2D *area = area_owner.get_or_null(p_area);
  383. ERR_FAIL_COND_V(!area, Variant());
  384. return area->get_param(p_param);
  385. };
  386. Transform2D GodotPhysicsServer2D::area_get_transform(RID p_area) const {
  387. GodotArea2D *area = area_owner.get_or_null(p_area);
  388. ERR_FAIL_COND_V(!area, Transform2D());
  389. return area->get_transform();
  390. };
  391. void GodotPhysicsServer2D::area_set_pickable(RID p_area, bool p_pickable) {
  392. GodotArea2D *area = area_owner.get_or_null(p_area);
  393. ERR_FAIL_COND(!area);
  394. area->set_pickable(p_pickable);
  395. }
  396. void GodotPhysicsServer2D::area_set_monitorable(RID p_area, bool p_monitorable) {
  397. GodotArea2D *area = area_owner.get_or_null(p_area);
  398. ERR_FAIL_COND(!area);
  399. FLUSH_QUERY_CHECK(area);
  400. area->set_monitorable(p_monitorable);
  401. }
  402. void GodotPhysicsServer2D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  403. GodotArea2D *area = area_owner.get_or_null(p_area);
  404. ERR_FAIL_COND(!area);
  405. area->set_collision_layer(p_layer);
  406. }
  407. uint32_t GodotPhysicsServer2D::area_get_collision_layer(RID p_area) const {
  408. GodotArea2D *area = area_owner.get_or_null(p_area);
  409. ERR_FAIL_COND_V(!area, 0);
  410. return area->get_collision_layer();
  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. uint32_t GodotPhysicsServer2D::area_get_collision_mask(RID p_area) const {
  418. GodotArea2D *area = area_owner.get_or_null(p_area);
  419. ERR_FAIL_COND_V(!area, 0);
  420. return area->get_collision_mask();
  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_collision_priority(RID p_body, real_t p_priority) {
  587. GodotBody2D *body = body_owner.get_or_null(p_body);
  588. ERR_FAIL_COND(!body);
  589. body->set_collision_priority(p_priority);
  590. }
  591. real_t GodotPhysicsServer2D::body_get_collision_priority(RID p_body) const {
  592. const GodotBody2D *body = body_owner.get_or_null(p_body);
  593. ERR_FAIL_COND_V(!body, 0);
  594. return body->get_collision_priority();
  595. }
  596. void GodotPhysicsServer2D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
  597. GodotBody2D *body = body_owner.get_or_null(p_body);
  598. ERR_FAIL_COND(!body);
  599. body->set_param(p_param, p_value);
  600. }
  601. Variant GodotPhysicsServer2D::body_get_param(RID p_body, BodyParameter p_param) const {
  602. GodotBody2D *body = body_owner.get_or_null(p_body);
  603. ERR_FAIL_COND_V(!body, 0);
  604. return body->get_param(p_param);
  605. }
  606. void GodotPhysicsServer2D::body_reset_mass_properties(RID p_body) {
  607. GodotBody2D *body = body_owner.get_or_null(p_body);
  608. ERR_FAIL_COND(!body);
  609. return body->reset_mass_properties();
  610. }
  611. void GodotPhysicsServer2D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  612. GodotBody2D *body = body_owner.get_or_null(p_body);
  613. ERR_FAIL_COND(!body);
  614. body->set_state(p_state, p_variant);
  615. }
  616. Variant GodotPhysicsServer2D::body_get_state(RID p_body, BodyState p_state) const {
  617. GodotBody2D *body = body_owner.get_or_null(p_body);
  618. ERR_FAIL_COND_V(!body, Variant());
  619. return body->get_state(p_state);
  620. }
  621. void GodotPhysicsServer2D::body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) {
  622. GodotBody2D *body = body_owner.get_or_null(p_body);
  623. ERR_FAIL_COND(!body);
  624. body->apply_central_impulse(p_impulse);
  625. body->wakeup();
  626. }
  627. void GodotPhysicsServer2D::body_apply_torque_impulse(RID p_body, real_t p_torque) {
  628. GodotBody2D *body = body_owner.get_or_null(p_body);
  629. ERR_FAIL_COND(!body);
  630. _update_shapes();
  631. body->apply_torque_impulse(p_torque);
  632. body->wakeup();
  633. }
  634. void GodotPhysicsServer2D::body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position) {
  635. GodotBody2D *body = body_owner.get_or_null(p_body);
  636. ERR_FAIL_COND(!body);
  637. _update_shapes();
  638. body->apply_impulse(p_impulse, p_position);
  639. body->wakeup();
  640. }
  641. void GodotPhysicsServer2D::body_apply_central_force(RID p_body, const Vector2 &p_force) {
  642. GodotBody2D *body = body_owner.get_or_null(p_body);
  643. ERR_FAIL_COND(!body);
  644. body->apply_central_force(p_force);
  645. body->wakeup();
  646. }
  647. void GodotPhysicsServer2D::body_apply_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position) {
  648. GodotBody2D *body = body_owner.get_or_null(p_body);
  649. ERR_FAIL_COND(!body);
  650. body->apply_force(p_force, p_position);
  651. body->wakeup();
  652. }
  653. void GodotPhysicsServer2D::body_apply_torque(RID p_body, real_t p_torque) {
  654. GodotBody2D *body = body_owner.get_or_null(p_body);
  655. ERR_FAIL_COND(!body);
  656. body->apply_torque(p_torque);
  657. body->wakeup();
  658. }
  659. void GodotPhysicsServer2D::body_add_constant_central_force(RID p_body, const Vector2 &p_force) {
  660. GodotBody2D *body = body_owner.get_or_null(p_body);
  661. ERR_FAIL_COND(!body);
  662. body->add_constant_central_force(p_force);
  663. body->wakeup();
  664. }
  665. void GodotPhysicsServer2D::body_add_constant_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position) {
  666. GodotBody2D *body = body_owner.get_or_null(p_body);
  667. ERR_FAIL_COND(!body);
  668. body->add_constant_force(p_force, p_position);
  669. body->wakeup();
  670. }
  671. void GodotPhysicsServer2D::body_add_constant_torque(RID p_body, real_t p_torque) {
  672. GodotBody2D *body = body_owner.get_or_null(p_body);
  673. ERR_FAIL_COND(!body);
  674. body->add_constant_torque(p_torque);
  675. body->wakeup();
  676. }
  677. void GodotPhysicsServer2D::body_set_constant_force(RID p_body, const Vector2 &p_force) {
  678. GodotBody2D *body = body_owner.get_or_null(p_body);
  679. ERR_FAIL_COND(!body);
  680. body->set_constant_force(p_force);
  681. if (!p_force.is_zero_approx()) {
  682. body->wakeup();
  683. }
  684. }
  685. Vector2 GodotPhysicsServer2D::body_get_constant_force(RID p_body) const {
  686. GodotBody2D *body = body_owner.get_or_null(p_body);
  687. ERR_FAIL_COND_V(!body, Vector2());
  688. return body->get_constant_force();
  689. }
  690. void GodotPhysicsServer2D::body_set_constant_torque(RID p_body, real_t p_torque) {
  691. GodotBody2D *body = body_owner.get_or_null(p_body);
  692. ERR_FAIL_COND(!body);
  693. body->set_constant_torque(p_torque);
  694. if (!Math::is_zero_approx(p_torque)) {
  695. body->wakeup();
  696. }
  697. }
  698. real_t GodotPhysicsServer2D::body_get_constant_torque(RID p_body) const {
  699. GodotBody2D *body = body_owner.get_or_null(p_body);
  700. ERR_FAIL_COND_V(!body, 0);
  701. return body->get_constant_torque();
  702. }
  703. void GodotPhysicsServer2D::body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) {
  704. GodotBody2D *body = body_owner.get_or_null(p_body);
  705. ERR_FAIL_COND(!body);
  706. _update_shapes();
  707. Vector2 v = body->get_linear_velocity();
  708. Vector2 axis = p_axis_velocity.normalized();
  709. v -= axis * axis.dot(v);
  710. v += p_axis_velocity;
  711. body->set_linear_velocity(v);
  712. body->wakeup();
  713. };
  714. void GodotPhysicsServer2D::body_add_collision_exception(RID p_body, RID p_body_b) {
  715. GodotBody2D *body = body_owner.get_or_null(p_body);
  716. ERR_FAIL_COND(!body);
  717. body->add_exception(p_body_b);
  718. body->wakeup();
  719. };
  720. void GodotPhysicsServer2D::body_remove_collision_exception(RID p_body, RID p_body_b) {
  721. GodotBody2D *body = body_owner.get_or_null(p_body);
  722. ERR_FAIL_COND(!body);
  723. body->remove_exception(p_body_b);
  724. body->wakeup();
  725. };
  726. void GodotPhysicsServer2D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  727. GodotBody2D *body = body_owner.get_or_null(p_body);
  728. ERR_FAIL_COND(!body);
  729. for (int i = 0; i < body->get_exceptions().size(); i++) {
  730. p_exceptions->push_back(body->get_exceptions()[i]);
  731. }
  732. };
  733. void GodotPhysicsServer2D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  734. GodotBody2D *body = body_owner.get_or_null(p_body);
  735. ERR_FAIL_COND(!body);
  736. };
  737. real_t GodotPhysicsServer2D::body_get_contacts_reported_depth_threshold(RID p_body) const {
  738. GodotBody2D *body = body_owner.get_or_null(p_body);
  739. ERR_FAIL_COND_V(!body, 0);
  740. return 0;
  741. };
  742. void GodotPhysicsServer2D::body_set_omit_force_integration(RID p_body, bool p_omit) {
  743. GodotBody2D *body = body_owner.get_or_null(p_body);
  744. ERR_FAIL_COND(!body);
  745. body->set_omit_force_integration(p_omit);
  746. };
  747. bool GodotPhysicsServer2D::body_is_omitting_force_integration(RID p_body) const {
  748. GodotBody2D *body = body_owner.get_or_null(p_body);
  749. ERR_FAIL_COND_V(!body, false);
  750. return body->get_omit_force_integration();
  751. };
  752. void GodotPhysicsServer2D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  753. GodotBody2D *body = body_owner.get_or_null(p_body);
  754. ERR_FAIL_COND(!body);
  755. body->set_max_contacts_reported(p_contacts);
  756. }
  757. int GodotPhysicsServer2D::body_get_max_contacts_reported(RID p_body) const {
  758. GodotBody2D *body = body_owner.get_or_null(p_body);
  759. ERR_FAIL_COND_V(!body, -1);
  760. return body->get_max_contacts_reported();
  761. }
  762. void GodotPhysicsServer2D::body_set_state_sync_callback(RID p_body, const Callable &p_callable) {
  763. GodotBody2D *body = body_owner.get_or_null(p_body);
  764. ERR_FAIL_COND(!body);
  765. body->set_state_sync_callback(p_callable);
  766. }
  767. void GodotPhysicsServer2D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
  768. GodotBody2D *body = body_owner.get_or_null(p_body);
  769. ERR_FAIL_COND(!body);
  770. body->set_force_integration_callback(p_callable, p_udata);
  771. }
  772. 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) {
  773. GodotBody2D *body = body_owner.get_or_null(p_body);
  774. ERR_FAIL_COND_V(!body, false);
  775. ERR_FAIL_INDEX_V(p_body_shape, body->get_shape_count(), false);
  776. 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);
  777. }
  778. void GodotPhysicsServer2D::body_set_pickable(RID p_body, bool p_pickable) {
  779. GodotBody2D *body = body_owner.get_or_null(p_body);
  780. ERR_FAIL_COND(!body);
  781. body->set_pickable(p_pickable);
  782. }
  783. bool GodotPhysicsServer2D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
  784. GodotBody2D *body = body_owner.get_or_null(p_body);
  785. ERR_FAIL_COND_V(!body, false);
  786. ERR_FAIL_COND_V(!body->get_space(), false);
  787. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  788. _update_shapes();
  789. return body->get_space()->test_body_motion(body, p_parameters, r_result);
  790. }
  791. PhysicsDirectBodyState2D *GodotPhysicsServer2D::body_get_direct_state(RID p_body) {
  792. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  793. if (!body_owner.owns(p_body)) {
  794. return nullptr;
  795. }
  796. GodotBody2D *body = body_owner.get_or_null(p_body);
  797. ERR_FAIL_COND_V(!body, nullptr);
  798. if (!body->get_space()) {
  799. return nullptr;
  800. }
  801. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  802. return body->get_direct_state();
  803. }
  804. /* JOINT API */
  805. RID GodotPhysicsServer2D::joint_create() {
  806. GodotJoint2D *joint = memnew(GodotJoint2D);
  807. RID joint_rid = joint_owner.make_rid(joint);
  808. joint->set_self(joint_rid);
  809. return joint_rid;
  810. }
  811. void GodotPhysicsServer2D::joint_clear(RID p_joint) {
  812. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  813. ERR_FAIL_NULL(joint);
  814. if (joint->get_type() != JOINT_TYPE_MAX) {
  815. GodotJoint2D *empty_joint = memnew(GodotJoint2D);
  816. empty_joint->copy_settings_from(joint);
  817. joint_owner.replace(p_joint, empty_joint);
  818. memdelete(joint);
  819. }
  820. }
  821. void GodotPhysicsServer2D::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  822. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  823. ERR_FAIL_COND(!joint);
  824. switch (p_param) {
  825. case JOINT_PARAM_BIAS:
  826. joint->set_bias(p_value);
  827. break;
  828. case JOINT_PARAM_MAX_BIAS:
  829. joint->set_max_bias(p_value);
  830. break;
  831. case JOINT_PARAM_MAX_FORCE:
  832. joint->set_max_force(p_value);
  833. break;
  834. }
  835. }
  836. real_t GodotPhysicsServer2D::joint_get_param(RID p_joint, JointParam p_param) const {
  837. const GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  838. ERR_FAIL_COND_V(!joint, -1);
  839. switch (p_param) {
  840. case JOINT_PARAM_BIAS:
  841. return joint->get_bias();
  842. break;
  843. case JOINT_PARAM_MAX_BIAS:
  844. return joint->get_max_bias();
  845. break;
  846. case JOINT_PARAM_MAX_FORCE:
  847. return joint->get_max_force();
  848. break;
  849. }
  850. return 0;
  851. }
  852. void GodotPhysicsServer2D::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  853. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  854. ERR_FAIL_COND(!joint);
  855. joint->disable_collisions_between_bodies(p_disable);
  856. if (2 == joint->get_body_count()) {
  857. GodotBody2D *body_a = *joint->get_body_ptr();
  858. GodotBody2D *body_b = *(joint->get_body_ptr() + 1);
  859. if (p_disable) {
  860. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  861. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  862. } else {
  863. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  864. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  865. }
  866. }
  867. }
  868. bool GodotPhysicsServer2D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  869. const GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  870. ERR_FAIL_COND_V(!joint, true);
  871. return joint->is_disabled_collisions_between_bodies();
  872. }
  873. void GodotPhysicsServer2D::joint_make_pin(RID p_joint, const Vector2 &p_pos, RID p_body_a, RID p_body_b) {
  874. GodotBody2D *A = body_owner.get_or_null(p_body_a);
  875. ERR_FAIL_COND(!A);
  876. GodotBody2D *B = nullptr;
  877. if (body_owner.owns(p_body_b)) {
  878. B = body_owner.get_or_null(p_body_b);
  879. ERR_FAIL_COND(!B);
  880. }
  881. GodotJoint2D *prev_joint = joint_owner.get_or_null(p_joint);
  882. ERR_FAIL_COND(prev_joint == nullptr);
  883. GodotJoint2D *joint = memnew(GodotPinJoint2D(p_pos, A, B));
  884. joint_owner.replace(p_joint, joint);
  885. joint->copy_settings_from(prev_joint);
  886. memdelete(prev_joint);
  887. }
  888. 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) {
  889. GodotBody2D *A = body_owner.get_or_null(p_body_a);
  890. ERR_FAIL_COND(!A);
  891. GodotBody2D *B = body_owner.get_or_null(p_body_b);
  892. ERR_FAIL_COND(!B);
  893. GodotJoint2D *prev_joint = joint_owner.get_or_null(p_joint);
  894. ERR_FAIL_COND(prev_joint == nullptr);
  895. GodotJoint2D *joint = memnew(GodotGrooveJoint2D(p_a_groove1, p_a_groove2, p_b_anchor, A, B));
  896. joint_owner.replace(p_joint, joint);
  897. joint->copy_settings_from(prev_joint);
  898. memdelete(prev_joint);
  899. }
  900. 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) {
  901. GodotBody2D *A = body_owner.get_or_null(p_body_a);
  902. ERR_FAIL_COND(!A);
  903. GodotBody2D *B = body_owner.get_or_null(p_body_b);
  904. ERR_FAIL_COND(!B);
  905. GodotJoint2D *prev_joint = joint_owner.get_or_null(p_joint);
  906. ERR_FAIL_COND(prev_joint == nullptr);
  907. GodotJoint2D *joint = memnew(GodotDampedSpringJoint2D(p_anchor_a, p_anchor_b, A, B));
  908. joint_owner.replace(p_joint, joint);
  909. joint->copy_settings_from(prev_joint);
  910. memdelete(prev_joint);
  911. }
  912. void GodotPhysicsServer2D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  913. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  914. ERR_FAIL_NULL(joint);
  915. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
  916. GodotPinJoint2D *pin_joint = static_cast<GodotPinJoint2D *>(joint);
  917. pin_joint->set_param(p_param, p_value);
  918. }
  919. real_t GodotPhysicsServer2D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  920. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  921. ERR_FAIL_NULL_V(joint, 0);
  922. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
  923. GodotPinJoint2D *pin_joint = static_cast<GodotPinJoint2D *>(joint);
  924. return pin_joint->get_param(p_param);
  925. }
  926. void GodotPhysicsServer2D::damped_spring_joint_set_param(RID p_joint, DampedSpringParam p_param, real_t p_value) {
  927. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  928. ERR_FAIL_NULL(joint);
  929. ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_DAMPED_SPRING);
  930. GodotDampedSpringJoint2D *dsj = static_cast<GodotDampedSpringJoint2D *>(joint);
  931. dsj->set_param(p_param, p_value);
  932. }
  933. real_t GodotPhysicsServer2D::damped_spring_joint_get_param(RID p_joint, DampedSpringParam p_param) const {
  934. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  935. ERR_FAIL_NULL_V(joint, 0);
  936. ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_DAMPED_SPRING, 0);
  937. GodotDampedSpringJoint2D *dsj = static_cast<GodotDampedSpringJoint2D *>(joint);
  938. return dsj->get_param(p_param);
  939. }
  940. PhysicsServer2D::JointType GodotPhysicsServer2D::joint_get_type(RID p_joint) const {
  941. GodotJoint2D *joint = joint_owner.get_or_null(p_joint);
  942. ERR_FAIL_COND_V(!joint, JOINT_TYPE_PIN);
  943. return joint->get_type();
  944. }
  945. void GodotPhysicsServer2D::free(RID p_rid) {
  946. _update_shapes(); // just in case
  947. if (shape_owner.owns(p_rid)) {
  948. GodotShape2D *shape = shape_owner.get_or_null(p_rid);
  949. while (shape->get_owners().size()) {
  950. GodotShapeOwner2D *so = shape->get_owners().begin()->key;
  951. so->remove_shape(shape);
  952. }
  953. shape_owner.free(p_rid);
  954. memdelete(shape);
  955. } else if (body_owner.owns(p_rid)) {
  956. GodotBody2D *body = body_owner.get_or_null(p_rid);
  957. body_set_space(p_rid, RID());
  958. while (body->get_shape_count()) {
  959. body->remove_shape(0);
  960. }
  961. body_owner.free(p_rid);
  962. memdelete(body);
  963. } else if (area_owner.owns(p_rid)) {
  964. GodotArea2D *area = area_owner.get_or_null(p_rid);
  965. area->set_space(nullptr);
  966. while (area->get_shape_count()) {
  967. area->remove_shape(0);
  968. }
  969. area_owner.free(p_rid);
  970. memdelete(area);
  971. } else if (space_owner.owns(p_rid)) {
  972. GodotSpace2D *space = space_owner.get_or_null(p_rid);
  973. while (space->get_objects().size()) {
  974. GodotCollisionObject2D *co = static_cast<GodotCollisionObject2D *>(*space->get_objects().begin());
  975. co->set_space(nullptr);
  976. }
  977. active_spaces.erase(space);
  978. free(space->get_default_area()->get_self());
  979. space_owner.free(p_rid);
  980. memdelete(space);
  981. } else if (joint_owner.owns(p_rid)) {
  982. GodotJoint2D *joint = joint_owner.get_or_null(p_rid);
  983. joint_owner.free(p_rid);
  984. memdelete(joint);
  985. } else {
  986. ERR_FAIL_MSG("Invalid ID.");
  987. }
  988. }
  989. void GodotPhysicsServer2D::set_active(bool p_active) {
  990. active = p_active;
  991. }
  992. void GodotPhysicsServer2D::init() {
  993. doing_sync = false;
  994. stepper = memnew(GodotStep2D);
  995. }
  996. void GodotPhysicsServer2D::step(real_t p_step) {
  997. if (!active) {
  998. return;
  999. }
  1000. _update_shapes();
  1001. island_count = 0;
  1002. active_objects = 0;
  1003. collision_pairs = 0;
  1004. for (const GodotSpace2D *E : active_spaces) {
  1005. stepper->step(const_cast<GodotSpace2D *>(E), p_step);
  1006. island_count += E->get_island_count();
  1007. active_objects += E->get_active_objects();
  1008. collision_pairs += E->get_collision_pairs();
  1009. }
  1010. }
  1011. void GodotPhysicsServer2D::sync() {
  1012. doing_sync = true;
  1013. }
  1014. void GodotPhysicsServer2D::flush_queries() {
  1015. if (!active) {
  1016. return;
  1017. }
  1018. flushing_queries = true;
  1019. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  1020. for (const GodotSpace2D *E : active_spaces) {
  1021. GodotSpace2D *space = const_cast<GodotSpace2D *>(E);
  1022. space->call_queries();
  1023. }
  1024. flushing_queries = false;
  1025. if (EngineDebugger::is_profiling("servers")) {
  1026. uint64_t total_time[GodotSpace2D::ELAPSED_TIME_MAX];
  1027. static const char *time_name[GodotSpace2D::ELAPSED_TIME_MAX] = {
  1028. "integrate_forces",
  1029. "generate_islands",
  1030. "setup_constraints",
  1031. "solve_constraints",
  1032. "integrate_velocities"
  1033. };
  1034. for (int i = 0; i < GodotSpace2D::ELAPSED_TIME_MAX; i++) {
  1035. total_time[i] = 0;
  1036. }
  1037. for (const GodotSpace2D *E : active_spaces) {
  1038. for (int i = 0; i < GodotSpace2D::ELAPSED_TIME_MAX; i++) {
  1039. total_time[i] += E->get_elapsed_time(GodotSpace2D::ElapsedTime(i));
  1040. }
  1041. }
  1042. Array values;
  1043. values.resize(GodotSpace2D::ELAPSED_TIME_MAX * 2);
  1044. for (int i = 0; i < GodotSpace2D::ELAPSED_TIME_MAX; i++) {
  1045. values[i * 2 + 0] = time_name[i];
  1046. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  1047. }
  1048. values.push_back("flush_queries");
  1049. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  1050. values.push_front("physics_2d");
  1051. EngineDebugger::profiler_add_frame_data("servers", values);
  1052. }
  1053. }
  1054. void GodotPhysicsServer2D::end_sync() {
  1055. doing_sync = false;
  1056. }
  1057. void GodotPhysicsServer2D::finish() {
  1058. memdelete(stepper);
  1059. }
  1060. void GodotPhysicsServer2D::_update_shapes() {
  1061. while (pending_shape_update_list.first()) {
  1062. pending_shape_update_list.first()->self()->_shape_changed();
  1063. pending_shape_update_list.remove(pending_shape_update_list.first());
  1064. }
  1065. }
  1066. int GodotPhysicsServer2D::get_process_info(ProcessInfo p_info) {
  1067. switch (p_info) {
  1068. case INFO_ACTIVE_OBJECTS: {
  1069. return active_objects;
  1070. } break;
  1071. case INFO_COLLISION_PAIRS: {
  1072. return collision_pairs;
  1073. } break;
  1074. case INFO_ISLAND_COUNT: {
  1075. return island_count;
  1076. } break;
  1077. }
  1078. return 0;
  1079. }
  1080. GodotPhysicsServer2D *GodotPhysicsServer2D::godot_singleton = nullptr;
  1081. GodotPhysicsServer2D::GodotPhysicsServer2D(bool p_using_threads) {
  1082. godot_singleton = this;
  1083. GodotBroadPhase2D::create_func = GodotBroadPhase2DBVH::_create;
  1084. using_threads = p_using_threads;
  1085. }