collision_object_2d.cpp 24 KB

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