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