collision_object_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*************************************************************************/
  2. /* collision_object_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "collision_object_2d.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/physics_2d_server.h"
  33. void CollisionObject2D::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_ENTER_TREE: {
  36. Transform2D global_transform = get_global_transform();
  37. if (area)
  38. Physics2DServer::get_singleton()->area_set_transform(rid, global_transform);
  39. else
  40. Physics2DServer::get_singleton()->body_set_state(rid, Physics2DServer::BODY_STATE_TRANSFORM, global_transform);
  41. RID space = get_world_2d()->get_space();
  42. if (area) {
  43. Physics2DServer::get_singleton()->area_set_space(rid, space);
  44. } else
  45. Physics2DServer::get_singleton()->body_set_space(rid, space);
  46. _update_pickable();
  47. //get space
  48. } break;
  49. case NOTIFICATION_ENTER_CANVAS: {
  50. if (area)
  51. Physics2DServer::get_singleton()->area_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  52. else
  53. Physics2DServer::get_singleton()->body_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  54. } break;
  55. case NOTIFICATION_VISIBILITY_CHANGED: {
  56. _update_pickable();
  57. } break;
  58. case NOTIFICATION_TRANSFORM_CHANGED: {
  59. if (only_update_transform_changes) {
  60. return;
  61. }
  62. Transform2D global_transform = get_global_transform();
  63. if (area)
  64. Physics2DServer::get_singleton()->area_set_transform(rid, global_transform);
  65. else
  66. Physics2DServer::get_singleton()->body_set_state(rid, Physics2DServer::BODY_STATE_TRANSFORM, global_transform);
  67. } break;
  68. case NOTIFICATION_EXIT_TREE: {
  69. if (area) {
  70. Physics2DServer::get_singleton()->area_set_space(rid, RID());
  71. } else
  72. Physics2DServer::get_singleton()->body_set_space(rid, RID());
  73. } break;
  74. case NOTIFICATION_EXIT_CANVAS: {
  75. if (area)
  76. Physics2DServer::get_singleton()->area_attach_canvas_instance_id(rid, 0);
  77. else
  78. Physics2DServer::get_singleton()->body_attach_canvas_instance_id(rid, 0);
  79. } break;
  80. }
  81. }
  82. uint32_t CollisionObject2D::create_shape_owner(Object *p_owner) {
  83. ShapeData sd;
  84. uint32_t id;
  85. if (shapes.size() == 0) {
  86. id = 0;
  87. } else {
  88. id = shapes.back()->key() + 1;
  89. }
  90. sd.owner = p_owner;
  91. shapes[id] = sd;
  92. return id;
  93. }
  94. void CollisionObject2D::remove_shape_owner(uint32_t owner) {
  95. ERR_FAIL_COND(!shapes.has(owner));
  96. shape_owner_clear_shapes(owner);
  97. shapes.erase(owner);
  98. }
  99. void CollisionObject2D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  100. ERR_FAIL_COND(!shapes.has(p_owner));
  101. ShapeData &sd = shapes[p_owner];
  102. sd.disabled = p_disabled;
  103. for (int i = 0; i < sd.shapes.size(); i++) {
  104. if (area) {
  105. Physics2DServer::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  106. } else {
  107. Physics2DServer::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  108. }
  109. }
  110. }
  111. bool CollisionObject2D::is_shape_owner_disabled(uint32_t p_owner) const {
  112. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  113. return shapes[p_owner].disabled;
  114. }
  115. void CollisionObject2D::shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable) {
  116. if (area)
  117. return; //not for areas
  118. ERR_FAIL_COND(!shapes.has(p_owner));
  119. ShapeData &sd = shapes[p_owner];
  120. sd.one_way_collision = p_enable;
  121. for (int i = 0; i < sd.shapes.size(); i++) {
  122. Physics2DServer::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, sd.one_way_collision, sd.one_way_collision_margin);
  123. }
  124. }
  125. bool CollisionObject2D::is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const {
  126. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  127. return shapes[p_owner].one_way_collision;
  128. }
  129. void CollisionObject2D::shape_owner_set_one_way_collision_margin(uint32_t p_owner, float p_margin) {
  130. if (area)
  131. return; //not for areas
  132. ERR_FAIL_COND(!shapes.has(p_owner));
  133. ShapeData &sd = shapes[p_owner];
  134. sd.one_way_collision_margin = p_margin;
  135. for (int i = 0; i < sd.shapes.size(); i++) {
  136. Physics2DServer::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, sd.one_way_collision, sd.one_way_collision_margin);
  137. }
  138. }
  139. float CollisionObject2D::get_shape_owner_one_way_collision_margin(uint32_t p_owner) const {
  140. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  141. return shapes[p_owner].one_way_collision_margin;
  142. }
  143. void CollisionObject2D::get_shape_owners(List<uint32_t> *r_owners) {
  144. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  145. r_owners->push_back(E->key());
  146. }
  147. }
  148. Array CollisionObject2D::_get_shape_owners() {
  149. Array ret;
  150. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  151. ret.push_back(E->key());
  152. }
  153. return ret;
  154. }
  155. void CollisionObject2D::shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform) {
  156. ERR_FAIL_COND(!shapes.has(p_owner));
  157. ShapeData &sd = shapes[p_owner];
  158. sd.xform = p_transform;
  159. for (int i = 0; i < sd.shapes.size(); i++) {
  160. if (area) {
  161. Physics2DServer::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, sd.xform);
  162. } else {
  163. Physics2DServer::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, sd.xform);
  164. }
  165. }
  166. }
  167. Transform2D CollisionObject2D::shape_owner_get_transform(uint32_t p_owner) const {
  168. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform2D());
  169. return shapes[p_owner].xform;
  170. }
  171. Object *CollisionObject2D::shape_owner_get_owner(uint32_t p_owner) const {
  172. ERR_FAIL_COND_V(!shapes.has(p_owner), NULL);
  173. return shapes[p_owner].owner;
  174. }
  175. void CollisionObject2D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape) {
  176. ERR_FAIL_COND(!shapes.has(p_owner));
  177. ERR_FAIL_COND(p_shape.is_null());
  178. ShapeData &sd = shapes[p_owner];
  179. ShapeData::Shape s;
  180. s.index = total_subshapes;
  181. s.shape = p_shape;
  182. if (area) {
  183. Physics2DServer::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  184. } else {
  185. Physics2DServer::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
  186. }
  187. sd.shapes.push_back(s);
  188. total_subshapes++;
  189. }
  190. int CollisionObject2D::shape_owner_get_shape_count(uint32_t p_owner) const {
  191. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  192. return shapes[p_owner].shapes.size();
  193. }
  194. Ref<Shape2D> CollisionObject2D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  195. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape2D>());
  196. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape2D>());
  197. return shapes[p_owner].shapes[p_shape].shape;
  198. }
  199. int CollisionObject2D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  200. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  201. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  202. return shapes[p_owner].shapes[p_shape].index;
  203. }
  204. void CollisionObject2D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  205. ERR_FAIL_COND(!shapes.has(p_owner));
  206. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  207. int index_to_remove = shapes[p_owner].shapes[p_shape].index;
  208. if (area) {
  209. Physics2DServer::get_singleton()->area_remove_shape(rid, index_to_remove);
  210. } else {
  211. Physics2DServer::get_singleton()->body_remove_shape(rid, index_to_remove);
  212. }
  213. shapes[p_owner].shapes.remove(p_shape);
  214. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  215. for (int i = 0; i < E->get().shapes.size(); i++) {
  216. if (E->get().shapes[i].index > index_to_remove) {
  217. E->get().shapes.write[i].index -= 1;
  218. }
  219. }
  220. }
  221. total_subshapes--;
  222. }
  223. void CollisionObject2D::shape_owner_clear_shapes(uint32_t p_owner) {
  224. ERR_FAIL_COND(!shapes.has(p_owner));
  225. while (shape_owner_get_shape_count(p_owner) > 0) {
  226. shape_owner_remove_shape(p_owner, 0);
  227. }
  228. }
  229. uint32_t CollisionObject2D::shape_find_owner(int p_shape_index) const {
  230. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, 0);
  231. for (const Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  232. for (int i = 0; i < E->get().shapes.size(); i++) {
  233. if (E->get().shapes[i].index == p_shape_index) {
  234. return E->key();
  235. }
  236. }
  237. }
  238. //in theory it should be unreachable
  239. return 0;
  240. }
  241. void CollisionObject2D::set_pickable(bool p_enabled) {
  242. if (pickable == p_enabled)
  243. return;
  244. pickable = p_enabled;
  245. _update_pickable();
  246. }
  247. bool CollisionObject2D::is_pickable() const {
  248. return pickable;
  249. }
  250. void CollisionObject2D::_input_event(Node *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape) {
  251. if (get_script_instance()) {
  252. get_script_instance()->call(SceneStringNames::get_singleton()->_input_event, p_viewport, p_input_event, p_shape);
  253. }
  254. emit_signal(SceneStringNames::get_singleton()->input_event, p_viewport, p_input_event, p_shape);
  255. }
  256. void CollisionObject2D::_mouse_enter() {
  257. if (get_script_instance()) {
  258. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter);
  259. }
  260. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  261. }
  262. void CollisionObject2D::_mouse_exit() {
  263. if (get_script_instance()) {
  264. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit);
  265. }
  266. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  267. }
  268. void CollisionObject2D::set_only_update_transform_changes(bool p_enable) {
  269. only_update_transform_changes = p_enable;
  270. }
  271. void CollisionObject2D::_update_pickable() {
  272. if (!is_inside_tree())
  273. return;
  274. bool is_pickable = pickable && is_visible_in_tree();
  275. if (area)
  276. Physics2DServer::get_singleton()->area_set_pickable(rid, is_pickable);
  277. else
  278. Physics2DServer::get_singleton()->body_set_pickable(rid, is_pickable);
  279. }
  280. String CollisionObject2D::get_configuration_warning() const {
  281. String warning = Node2D::get_configuration_warning();
  282. if (shapes.empty()) {
  283. if (!warning.empty()) {
  284. warning += "\n\n";
  285. }
  286. warning += TTR("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.");
  287. }
  288. return warning;
  289. }
  290. void CollisionObject2D::_bind_methods() {
  291. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject2D::get_rid);
  292. ClassDB::bind_method(D_METHOD("set_pickable", "enabled"), &CollisionObject2D::set_pickable);
  293. ClassDB::bind_method(D_METHOD("is_pickable"), &CollisionObject2D::is_pickable);
  294. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject2D::create_shape_owner);
  295. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject2D::remove_shape_owner);
  296. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject2D::_get_shape_owners);
  297. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject2D::shape_owner_set_transform);
  298. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject2D::shape_owner_get_transform);
  299. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject2D::shape_owner_get_owner);
  300. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject2D::shape_owner_set_disabled);
  301. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject2D::is_shape_owner_disabled);
  302. ClassDB::bind_method(D_METHOD("shape_owner_set_one_way_collision", "owner_id", "enable"), &CollisionObject2D::shape_owner_set_one_way_collision);
  303. ClassDB::bind_method(D_METHOD("is_shape_owner_one_way_collision_enabled", "owner_id"), &CollisionObject2D::is_shape_owner_one_way_collision_enabled);
  304. ClassDB::bind_method(D_METHOD("shape_owner_set_one_way_collision_margin", "owner_id", "margin"), &CollisionObject2D::shape_owner_set_one_way_collision_margin);
  305. ClassDB::bind_method(D_METHOD("get_shape_owner_one_way_collision_margin", "owner_id"), &CollisionObject2D::get_shape_owner_one_way_collision_margin);
  306. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject2D::shape_owner_add_shape);
  307. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject2D::shape_owner_get_shape_count);
  308. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape);
  309. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape_index);
  310. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_remove_shape);
  311. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject2D::shape_owner_clear_shapes);
  312. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject2D::shape_find_owner);
  313. BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "viewport"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::INT, "shape_idx")));
  314. 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")));
  315. ADD_SIGNAL(MethodInfo("mouse_entered"));
  316. ADD_SIGNAL(MethodInfo("mouse_exited"));
  317. ADD_GROUP("Pickable", "input_");
  318. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_pickable"), "set_pickable", "is_pickable");
  319. ADD_GROUP("", "");
  320. }
  321. CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
  322. rid = p_rid;
  323. area = p_area;
  324. pickable = true;
  325. set_notify_transform(true);
  326. total_subshapes = 0;
  327. only_update_transform_changes = false;
  328. if (p_area) {
  329. Physics2DServer::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  330. } else {
  331. Physics2DServer::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  332. }
  333. }
  334. CollisionObject2D::CollisionObject2D() {
  335. //owner=
  336. set_notify_transform(true);
  337. }
  338. CollisionObject2D::~CollisionObject2D() {
  339. Physics2DServer::get_singleton()->free(rid);
  340. }