collision_object_2d.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /**************************************************************************/
  2. /* collision_object_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 "collision_object_2d.h"
  31. #include "scene/resources/world_2d.h"
  32. void CollisionObject2D::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_ENTER_TREE: {
  35. Transform2D gl_transform = get_global_transform();
  36. if (area) {
  37. PhysicsServer2D::get_singleton()->area_set_transform(rid, gl_transform);
  38. } else {
  39. PhysicsServer2D::get_singleton()->body_set_state(rid, PhysicsServer2D::BODY_STATE_TRANSFORM, gl_transform);
  40. }
  41. bool disabled = !is_enabled();
  42. if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
  43. _apply_disabled();
  44. }
  45. if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
  46. Ref<World2D> world_ref = get_world_2d();
  47. ERR_FAIL_COND(!world_ref.is_valid());
  48. RID space = world_ref->get_space();
  49. if (area) {
  50. PhysicsServer2D::get_singleton()->area_set_space(rid, space);
  51. } else {
  52. PhysicsServer2D::get_singleton()->body_set_space(rid, space);
  53. }
  54. _space_changed(space);
  55. }
  56. _update_pickable();
  57. } break;
  58. case NOTIFICATION_ENTER_CANVAS: {
  59. if (area) {
  60. PhysicsServer2D::get_singleton()->area_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  61. } else {
  62. PhysicsServer2D::get_singleton()->body_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  63. }
  64. } break;
  65. case NOTIFICATION_VISIBILITY_CHANGED: {
  66. _update_pickable();
  67. } break;
  68. case NOTIFICATION_TRANSFORM_CHANGED: {
  69. if (only_update_transform_changes) {
  70. return;
  71. }
  72. Transform2D gl_transform = get_global_transform();
  73. if (area) {
  74. PhysicsServer2D::get_singleton()->area_set_transform(rid, gl_transform);
  75. } else {
  76. PhysicsServer2D::get_singleton()->body_set_state(rid, PhysicsServer2D::BODY_STATE_TRANSFORM, gl_transform);
  77. }
  78. } break;
  79. case NOTIFICATION_EXIT_TREE: {
  80. bool disabled = !is_enabled();
  81. if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
  82. if (callback_lock > 0) {
  83. ERR_PRINT("Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.");
  84. } else {
  85. if (area) {
  86. PhysicsServer2D::get_singleton()->area_set_space(rid, RID());
  87. } else {
  88. PhysicsServer2D::get_singleton()->body_set_space(rid, RID());
  89. }
  90. _space_changed(RID());
  91. }
  92. }
  93. if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
  94. _apply_enabled();
  95. }
  96. } break;
  97. case NOTIFICATION_EXIT_CANVAS: {
  98. if (area) {
  99. PhysicsServer2D::get_singleton()->area_attach_canvas_instance_id(rid, ObjectID());
  100. } else {
  101. PhysicsServer2D::get_singleton()->body_attach_canvas_instance_id(rid, ObjectID());
  102. }
  103. } break;
  104. case NOTIFICATION_WORLD_2D_CHANGED: {
  105. RID space = get_world_2d()->get_space();
  106. if (area) {
  107. PhysicsServer2D::get_singleton()->area_set_space(rid, space);
  108. } else {
  109. PhysicsServer2D::get_singleton()->body_set_space(rid, space);
  110. }
  111. _space_changed(space);
  112. } break;
  113. case NOTIFICATION_DISABLED: {
  114. _apply_disabled();
  115. } break;
  116. case NOTIFICATION_ENABLED: {
  117. _apply_enabled();
  118. } break;
  119. }
  120. }
  121. void CollisionObject2D::set_collision_layer(uint32_t p_layer) {
  122. collision_layer = p_layer;
  123. if (area) {
  124. PhysicsServer2D::get_singleton()->area_set_collision_layer(get_rid(), p_layer);
  125. } else {
  126. PhysicsServer2D::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
  127. }
  128. }
  129. uint32_t CollisionObject2D::get_collision_layer() const {
  130. return collision_layer;
  131. }
  132. void CollisionObject2D::set_collision_mask(uint32_t p_mask) {
  133. collision_mask = p_mask;
  134. if (area) {
  135. PhysicsServer2D::get_singleton()->area_set_collision_mask(get_rid(), p_mask);
  136. } else {
  137. PhysicsServer2D::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
  138. }
  139. }
  140. uint32_t CollisionObject2D::get_collision_mask() const {
  141. return collision_mask;
  142. }
  143. void CollisionObject2D::set_collision_layer_value(int p_layer_number, bool p_value) {
  144. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  145. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  146. uint32_t collision_layer_new = get_collision_layer();
  147. if (p_value) {
  148. collision_layer_new |= 1 << (p_layer_number - 1);
  149. } else {
  150. collision_layer_new &= ~(1 << (p_layer_number - 1));
  151. }
  152. set_collision_layer(collision_layer_new);
  153. }
  154. bool CollisionObject2D::get_collision_layer_value(int p_layer_number) const {
  155. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  156. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  157. return get_collision_layer() & (1 << (p_layer_number - 1));
  158. }
  159. void CollisionObject2D::set_collision_mask_value(int p_layer_number, bool p_value) {
  160. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  161. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  162. uint32_t mask = get_collision_mask();
  163. if (p_value) {
  164. mask |= 1 << (p_layer_number - 1);
  165. } else {
  166. mask &= ~(1 << (p_layer_number - 1));
  167. }
  168. set_collision_mask(mask);
  169. }
  170. bool CollisionObject2D::get_collision_mask_value(int p_layer_number) const {
  171. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  172. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  173. return get_collision_mask() & (1 << (p_layer_number - 1));
  174. }
  175. void CollisionObject2D::set_collision_priority(real_t p_priority) {
  176. collision_priority = p_priority;
  177. if (!area) {
  178. PhysicsServer2D::get_singleton()->body_set_collision_priority(get_rid(), p_priority);
  179. }
  180. }
  181. real_t CollisionObject2D::get_collision_priority() const {
  182. return collision_priority;
  183. }
  184. void CollisionObject2D::set_disable_mode(DisableMode p_mode) {
  185. if (disable_mode == p_mode) {
  186. return;
  187. }
  188. bool disabled = is_inside_tree() && !is_enabled();
  189. if (disabled) {
  190. // Cancel previous disable mode.
  191. _apply_enabled();
  192. }
  193. disable_mode = p_mode;
  194. if (disabled) {
  195. // Apply new disable mode.
  196. _apply_disabled();
  197. }
  198. }
  199. CollisionObject2D::DisableMode CollisionObject2D::get_disable_mode() const {
  200. return disable_mode;
  201. }
  202. void CollisionObject2D::_apply_disabled() {
  203. switch (disable_mode) {
  204. case DISABLE_MODE_REMOVE: {
  205. if (is_inside_tree()) {
  206. if (callback_lock > 0) {
  207. ERR_PRINT("Disabling a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Disable with call_deferred() instead.");
  208. } else {
  209. if (area) {
  210. PhysicsServer2D::get_singleton()->area_set_space(rid, RID());
  211. } else {
  212. PhysicsServer2D::get_singleton()->body_set_space(rid, RID());
  213. }
  214. _space_changed(RID());
  215. }
  216. }
  217. } break;
  218. case DISABLE_MODE_MAKE_STATIC: {
  219. if (!area && (body_mode != PhysicsServer2D::BODY_MODE_STATIC)) {
  220. PhysicsServer2D::get_singleton()->body_set_mode(rid, PhysicsServer2D::BODY_MODE_STATIC);
  221. }
  222. } break;
  223. case DISABLE_MODE_KEEP_ACTIVE: {
  224. // Nothing to do.
  225. } break;
  226. }
  227. }
  228. void CollisionObject2D::_apply_enabled() {
  229. switch (disable_mode) {
  230. case DISABLE_MODE_REMOVE: {
  231. if (is_inside_tree()) {
  232. RID space = get_world_2d()->get_space();
  233. if (area) {
  234. PhysicsServer2D::get_singleton()->area_set_space(rid, space);
  235. } else {
  236. PhysicsServer2D::get_singleton()->body_set_space(rid, space);
  237. }
  238. _space_changed(space);
  239. }
  240. } break;
  241. case DISABLE_MODE_MAKE_STATIC: {
  242. if (!area && (body_mode != PhysicsServer2D::BODY_MODE_STATIC)) {
  243. PhysicsServer2D::get_singleton()->body_set_mode(rid, body_mode);
  244. }
  245. } break;
  246. case DISABLE_MODE_KEEP_ACTIVE: {
  247. // Nothing to do.
  248. } break;
  249. }
  250. }
  251. uint32_t CollisionObject2D::create_shape_owner(Object *p_owner) {
  252. ShapeData sd;
  253. uint32_t id;
  254. if (shapes.size() == 0) {
  255. id = 0;
  256. } else {
  257. id = shapes.back()->key() + 1;
  258. }
  259. sd.owner_id = p_owner ? p_owner->get_instance_id() : ObjectID();
  260. shapes[id] = sd;
  261. return id;
  262. }
  263. void CollisionObject2D::remove_shape_owner(uint32_t owner) {
  264. ERR_FAIL_COND(!shapes.has(owner));
  265. shape_owner_clear_shapes(owner);
  266. shapes.erase(owner);
  267. }
  268. void CollisionObject2D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  269. ERR_FAIL_COND(!shapes.has(p_owner));
  270. ShapeData &sd = shapes[p_owner];
  271. sd.disabled = p_disabled;
  272. for (int i = 0; i < sd.shapes.size(); i++) {
  273. if (area) {
  274. PhysicsServer2D::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  275. } else {
  276. PhysicsServer2D::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  277. }
  278. }
  279. }
  280. bool CollisionObject2D::is_shape_owner_disabled(uint32_t p_owner) const {
  281. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  282. return shapes[p_owner].disabled;
  283. }
  284. void CollisionObject2D::shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable) {
  285. if (area) {
  286. return; //not for areas
  287. }
  288. ERR_FAIL_COND(!shapes.has(p_owner));
  289. ShapeData &sd = shapes[p_owner];
  290. sd.one_way_collision = p_enable;
  291. for (int i = 0; i < sd.shapes.size(); i++) {
  292. PhysicsServer2D::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, sd.one_way_collision, sd.one_way_collision_margin);
  293. }
  294. }
  295. bool CollisionObject2D::is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const {
  296. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  297. return shapes[p_owner].one_way_collision;
  298. }
  299. void CollisionObject2D::shape_owner_set_one_way_collision_margin(uint32_t p_owner, real_t p_margin) {
  300. if (area) {
  301. return; //not for areas
  302. }
  303. ERR_FAIL_COND(!shapes.has(p_owner));
  304. ShapeData &sd = shapes[p_owner];
  305. sd.one_way_collision_margin = p_margin;
  306. for (int i = 0; i < sd.shapes.size(); i++) {
  307. PhysicsServer2D::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, sd.one_way_collision, sd.one_way_collision_margin);
  308. }
  309. }
  310. real_t CollisionObject2D::get_shape_owner_one_way_collision_margin(uint32_t p_owner) const {
  311. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  312. return shapes[p_owner].one_way_collision_margin;
  313. }
  314. void CollisionObject2D::get_shape_owners(List<uint32_t> *r_owners) {
  315. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  316. r_owners->push_back(E.key);
  317. }
  318. }
  319. PackedInt32Array CollisionObject2D::_get_shape_owners() {
  320. PackedInt32Array ret;
  321. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  322. ret.push_back(E.key);
  323. }
  324. return ret;
  325. }
  326. void CollisionObject2D::shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform) {
  327. ERR_FAIL_COND(!shapes.has(p_owner));
  328. ShapeData &sd = shapes[p_owner];
  329. sd.xform = p_transform;
  330. for (int i = 0; i < sd.shapes.size(); i++) {
  331. if (area) {
  332. PhysicsServer2D::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, sd.xform);
  333. } else {
  334. PhysicsServer2D::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, sd.xform);
  335. }
  336. }
  337. }
  338. Transform2D CollisionObject2D::shape_owner_get_transform(uint32_t p_owner) const {
  339. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform2D());
  340. return shapes[p_owner].xform;
  341. }
  342. Object *CollisionObject2D::shape_owner_get_owner(uint32_t p_owner) const {
  343. ERR_FAIL_COND_V(!shapes.has(p_owner), nullptr);
  344. return ObjectDB::get_instance(shapes[p_owner].owner_id);
  345. }
  346. void CollisionObject2D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape) {
  347. ERR_FAIL_COND(!shapes.has(p_owner));
  348. ERR_FAIL_COND(p_shape.is_null());
  349. ShapeData &sd = shapes[p_owner];
  350. ShapeData::Shape s;
  351. s.index = total_subshapes;
  352. s.shape = p_shape;
  353. if (area) {
  354. PhysicsServer2D::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  355. } else {
  356. PhysicsServer2D::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  357. }
  358. sd.shapes.push_back(s);
  359. total_subshapes++;
  360. }
  361. int CollisionObject2D::shape_owner_get_shape_count(uint32_t p_owner) const {
  362. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  363. return shapes[p_owner].shapes.size();
  364. }
  365. Ref<Shape2D> CollisionObject2D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  366. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape2D>());
  367. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape2D>());
  368. return shapes[p_owner].shapes[p_shape].shape;
  369. }
  370. int CollisionObject2D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  371. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  372. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  373. return shapes[p_owner].shapes[p_shape].index;
  374. }
  375. void CollisionObject2D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  376. ERR_FAIL_COND(!shapes.has(p_owner));
  377. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  378. int index_to_remove = shapes[p_owner].shapes[p_shape].index;
  379. if (area) {
  380. PhysicsServer2D::get_singleton()->area_remove_shape(rid, index_to_remove);
  381. } else {
  382. PhysicsServer2D::get_singleton()->body_remove_shape(rid, index_to_remove);
  383. }
  384. shapes[p_owner].shapes.remove_at(p_shape);
  385. for (KeyValue<uint32_t, ShapeData> &E : shapes) {
  386. for (int i = 0; i < E.value.shapes.size(); i++) {
  387. if (E.value.shapes[i].index > index_to_remove) {
  388. E.value.shapes.write[i].index -= 1;
  389. }
  390. }
  391. }
  392. total_subshapes--;
  393. }
  394. void CollisionObject2D::shape_owner_clear_shapes(uint32_t p_owner) {
  395. ERR_FAIL_COND(!shapes.has(p_owner));
  396. while (shape_owner_get_shape_count(p_owner) > 0) {
  397. shape_owner_remove_shape(p_owner, 0);
  398. }
  399. }
  400. uint32_t CollisionObject2D::shape_find_owner(int p_shape_index) const {
  401. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, UINT32_MAX);
  402. for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
  403. for (int i = 0; i < E.value.shapes.size(); i++) {
  404. if (E.value.shapes[i].index == p_shape_index) {
  405. return E.key;
  406. }
  407. }
  408. }
  409. //in theory it should be unreachable
  410. ERR_FAIL_V_MSG(UINT32_MAX, "Can't find owner for shape index " + itos(p_shape_index) + ".");
  411. }
  412. void CollisionObject2D::set_pickable(bool p_enabled) {
  413. if (pickable == p_enabled) {
  414. return;
  415. }
  416. pickable = p_enabled;
  417. _update_pickable();
  418. }
  419. bool CollisionObject2D::is_pickable() const {
  420. return pickable;
  421. }
  422. void CollisionObject2D::_input_event_call(Viewport *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape) {
  423. GDVIRTUAL_CALL(_input_event, p_viewport, p_input_event, p_shape);
  424. emit_signal(SceneStringName(input_event), p_viewport, p_input_event, p_shape);
  425. }
  426. void CollisionObject2D::_mouse_enter() {
  427. GDVIRTUAL_CALL(_mouse_enter);
  428. emit_signal(SceneStringName(mouse_entered));
  429. }
  430. void CollisionObject2D::_mouse_exit() {
  431. GDVIRTUAL_CALL(_mouse_exit);
  432. emit_signal(SceneStringName(mouse_exited));
  433. }
  434. void CollisionObject2D::_mouse_shape_enter(int p_shape) {
  435. GDVIRTUAL_CALL(_mouse_shape_enter, p_shape);
  436. emit_signal(SceneStringName(mouse_shape_entered), p_shape);
  437. }
  438. void CollisionObject2D::_mouse_shape_exit(int p_shape) {
  439. GDVIRTUAL_CALL(_mouse_shape_exit, p_shape);
  440. emit_signal(SceneStringName(mouse_shape_exited), p_shape);
  441. }
  442. void CollisionObject2D::set_only_update_transform_changes(bool p_enable) {
  443. only_update_transform_changes = p_enable;
  444. }
  445. bool CollisionObject2D::is_only_update_transform_changes_enabled() const {
  446. return only_update_transform_changes;
  447. }
  448. void CollisionObject2D::set_body_mode(PhysicsServer2D::BodyMode p_mode) {
  449. ERR_FAIL_COND(area);
  450. if (body_mode == p_mode) {
  451. return;
  452. }
  453. body_mode = p_mode;
  454. if (is_inside_tree() && !is_enabled() && (disable_mode == DISABLE_MODE_MAKE_STATIC)) {
  455. return;
  456. }
  457. PhysicsServer2D::get_singleton()->body_set_mode(rid, p_mode);
  458. }
  459. void CollisionObject2D::_space_changed(const RID &p_new_space) {
  460. }
  461. void CollisionObject2D::_update_pickable() {
  462. if (!is_inside_tree()) {
  463. return;
  464. }
  465. bool is_pickable = pickable && is_visible_in_tree();
  466. if (area) {
  467. PhysicsServer2D::get_singleton()->area_set_pickable(rid, is_pickable);
  468. } else {
  469. PhysicsServer2D::get_singleton()->body_set_pickable(rid, is_pickable);
  470. }
  471. }
  472. PackedStringArray CollisionObject2D::get_configuration_warnings() const {
  473. PackedStringArray warnings = Node2D::get_configuration_warnings();
  474. if (shapes.is_empty()) {
  475. warnings.push_back(RTR("This node has no shape, so it can't collide or interact with other objects.\nConsider adding a CollisionShape2D or CollisionPolygon2D as a child to define its shape."));
  476. }
  477. return warnings;
  478. }
  479. void CollisionObject2D::_bind_methods() {
  480. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject2D::get_rid);
  481. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CollisionObject2D::set_collision_layer);
  482. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CollisionObject2D::get_collision_layer);
  483. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CollisionObject2D::set_collision_mask);
  484. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CollisionObject2D::get_collision_mask);
  485. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CollisionObject2D::set_collision_layer_value);
  486. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CollisionObject2D::get_collision_layer_value);
  487. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CollisionObject2D::set_collision_mask_value);
  488. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CollisionObject2D::get_collision_mask_value);
  489. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CollisionObject2D::set_collision_priority);
  490. ClassDB::bind_method(D_METHOD("get_collision_priority"), &CollisionObject2D::get_collision_priority);
  491. ClassDB::bind_method(D_METHOD("set_disable_mode", "mode"), &CollisionObject2D::set_disable_mode);
  492. ClassDB::bind_method(D_METHOD("get_disable_mode"), &CollisionObject2D::get_disable_mode);
  493. ClassDB::bind_method(D_METHOD("set_pickable", "enabled"), &CollisionObject2D::set_pickable);
  494. ClassDB::bind_method(D_METHOD("is_pickable"), &CollisionObject2D::is_pickable);
  495. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject2D::create_shape_owner);
  496. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject2D::remove_shape_owner);
  497. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject2D::_get_shape_owners);
  498. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject2D::shape_owner_set_transform);
  499. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject2D::shape_owner_get_transform);
  500. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject2D::shape_owner_get_owner);
  501. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject2D::shape_owner_set_disabled);
  502. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject2D::is_shape_owner_disabled);
  503. ClassDB::bind_method(D_METHOD("shape_owner_set_one_way_collision", "owner_id", "enable"), &CollisionObject2D::shape_owner_set_one_way_collision);
  504. ClassDB::bind_method(D_METHOD("is_shape_owner_one_way_collision_enabled", "owner_id"), &CollisionObject2D::is_shape_owner_one_way_collision_enabled);
  505. ClassDB::bind_method(D_METHOD("shape_owner_set_one_way_collision_margin", "owner_id", "margin"), &CollisionObject2D::shape_owner_set_one_way_collision_margin);
  506. ClassDB::bind_method(D_METHOD("get_shape_owner_one_way_collision_margin", "owner_id"), &CollisionObject2D::get_shape_owner_one_way_collision_margin);
  507. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject2D::shape_owner_add_shape);
  508. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject2D::shape_owner_get_shape_count);
  509. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape);
  510. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape_index);
  511. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_remove_shape);
  512. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject2D::shape_owner_clear_shapes);
  513. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject2D::shape_find_owner);
  514. GDVIRTUAL_BIND(_input_event, "viewport", "event", "shape_idx");
  515. GDVIRTUAL_BIND(_mouse_enter);
  516. GDVIRTUAL_BIND(_mouse_exit);
  517. GDVIRTUAL_BIND(_mouse_shape_enter, "shape_idx");
  518. GDVIRTUAL_BIND(_mouse_shape_exit, "shape_idx");
  519. ADD_SIGNAL(MethodInfo("input_event", PropertyInfo(Variant::OBJECT, "viewport", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::INT, "shape_idx")));
  520. ADD_SIGNAL(MethodInfo("mouse_entered"));
  521. ADD_SIGNAL(MethodInfo("mouse_exited"));
  522. ADD_SIGNAL(MethodInfo("mouse_shape_entered", PropertyInfo(Variant::INT, "shape_idx")));
  523. ADD_SIGNAL(MethodInfo("mouse_shape_exited", PropertyInfo(Variant::INT, "shape_idx")));
  524. ADD_PROPERTY(PropertyInfo(Variant::INT, "disable_mode", PROPERTY_HINT_ENUM, "Remove,Make Static,Keep Active"), "set_disable_mode", "get_disable_mode");
  525. ADD_GROUP("Collision", "collision_");
  526. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  527. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  528. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  529. ADD_GROUP("Input", "input_");
  530. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_pickable"), "set_pickable", "is_pickable");
  531. BIND_ENUM_CONSTANT(DISABLE_MODE_REMOVE);
  532. BIND_ENUM_CONSTANT(DISABLE_MODE_MAKE_STATIC);
  533. BIND_ENUM_CONSTANT(DISABLE_MODE_KEEP_ACTIVE);
  534. }
  535. CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
  536. rid = p_rid;
  537. area = p_area;
  538. pickable = true;
  539. set_notify_transform(true);
  540. set_hide_clip_children(true);
  541. total_subshapes = 0;
  542. only_update_transform_changes = false;
  543. if (p_area) {
  544. PhysicsServer2D::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  545. } else {
  546. PhysicsServer2D::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  547. PhysicsServer2D::get_singleton()->body_set_mode(rid, body_mode);
  548. }
  549. }
  550. CollisionObject2D::CollisionObject2D() {
  551. //owner=
  552. set_notify_transform(true);
  553. }
  554. CollisionObject2D::~CollisionObject2D() {
  555. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  556. PhysicsServer2D::get_singleton()->free(rid);
  557. }