collision_object_2d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. last_transform = global_transform;
  42. RID space = get_world_2d()->get_space();
  43. if (area) {
  44. Physics2DServer::get_singleton()->area_set_space(rid, space);
  45. } else
  46. Physics2DServer::get_singleton()->body_set_space(rid, space);
  47. _update_pickable();
  48. //get space
  49. }
  50. case NOTIFICATION_ENTER_CANVAS: {
  51. if (area)
  52. Physics2DServer::get_singleton()->area_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  53. else
  54. Physics2DServer::get_singleton()->body_attach_canvas_instance_id(rid, get_canvas_layer_instance_id());
  55. }
  56. case NOTIFICATION_VISIBILITY_CHANGED: {
  57. _update_pickable();
  58. } break;
  59. case NOTIFICATION_TRANSFORM_CHANGED: {
  60. Transform2D global_transform = get_global_transform();
  61. if (only_update_transform_changes && global_transform == last_transform) {
  62. return;
  63. }
  64. if (area)
  65. Physics2DServer::get_singleton()->area_set_transform(rid, global_transform);
  66. else
  67. Physics2DServer::get_singleton()->body_set_state(rid, Physics2DServer::BODY_STATE_TRANSFORM, global_transform);
  68. last_transform = global_transform;
  69. } break;
  70. case NOTIFICATION_EXIT_TREE: {
  71. if (area) {
  72. Physics2DServer::get_singleton()->area_set_space(rid, RID());
  73. } else
  74. Physics2DServer::get_singleton()->body_set_space(rid, RID());
  75. } break;
  76. case NOTIFICATION_EXIT_CANVAS: {
  77. if (area)
  78. Physics2DServer::get_singleton()->area_attach_canvas_instance_id(rid, 0);
  79. else
  80. Physics2DServer::get_singleton()->body_attach_canvas_instance_id(rid, 0);
  81. }
  82. }
  83. }
  84. uint32_t CollisionObject2D::create_shape_owner(Object *p_owner) {
  85. ShapeData sd;
  86. uint32_t id;
  87. if (shapes.size() == 0) {
  88. id = 0;
  89. } else {
  90. id = shapes.back()->key() + 1;
  91. }
  92. sd.owner = p_owner;
  93. shapes[id] = sd;
  94. return id;
  95. }
  96. void CollisionObject2D::remove_shape_owner(uint32_t owner) {
  97. ERR_FAIL_COND(!shapes.has(owner));
  98. shape_owner_clear_shapes(owner);
  99. shapes.erase(owner);
  100. }
  101. void CollisionObject2D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
  102. ERR_FAIL_COND(!shapes.has(p_owner));
  103. ShapeData &sd = shapes[p_owner];
  104. sd.disabled = p_disabled;
  105. for (int i = 0; i < sd.shapes.size(); i++) {
  106. if (area) {
  107. Physics2DServer::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  108. } else {
  109. Physics2DServer::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
  110. }
  111. }
  112. }
  113. bool CollisionObject2D::is_shape_owner_disabled(uint32_t p_owner) const {
  114. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  115. return shapes[p_owner].disabled;
  116. }
  117. void CollisionObject2D::shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable) {
  118. if (area)
  119. return; //not for areas
  120. ERR_FAIL_COND(!shapes.has(p_owner));
  121. ShapeData &sd = shapes[p_owner];
  122. sd.one_way_collision = p_enable;
  123. for (int i = 0; i < sd.shapes.size(); i++) {
  124. Physics2DServer::get_singleton()->body_set_shape_as_one_way_collision(rid, sd.shapes[i].index, p_enable);
  125. }
  126. }
  127. bool CollisionObject2D::is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const {
  128. ERR_FAIL_COND_V(!shapes.has(p_owner), false);
  129. return shapes[p_owner].one_way_collision;
  130. }
  131. void CollisionObject2D::get_shape_owners(List<uint32_t> *r_owners) {
  132. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  133. r_owners->push_back(E->key());
  134. }
  135. }
  136. Array CollisionObject2D::_get_shape_owners() {
  137. Array ret;
  138. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  139. ret.push_back(E->key());
  140. }
  141. return ret;
  142. }
  143. void CollisionObject2D::shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform) {
  144. ERR_FAIL_COND(!shapes.has(p_owner));
  145. ShapeData &sd = shapes[p_owner];
  146. sd.xform = p_transform;
  147. for (int i = 0; i < sd.shapes.size(); i++) {
  148. if (area) {
  149. Physics2DServer::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  150. } else {
  151. Physics2DServer::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform);
  152. }
  153. }
  154. }
  155. Transform2D CollisionObject2D::shape_owner_get_transform(uint32_t p_owner) const {
  156. ERR_FAIL_COND_V(!shapes.has(p_owner), Transform2D());
  157. return shapes[p_owner].xform;
  158. }
  159. Object *CollisionObject2D::shape_owner_get_owner(uint32_t p_owner) const {
  160. ERR_FAIL_COND_V(!shapes.has(p_owner), NULL);
  161. return shapes[p_owner].owner;
  162. }
  163. void CollisionObject2D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape) {
  164. ERR_FAIL_COND(!shapes.has(p_owner));
  165. ERR_FAIL_COND(p_shape.is_null());
  166. ShapeData &sd = shapes[p_owner];
  167. ShapeData::Shape s;
  168. s.index = total_subshapes;
  169. s.shape = p_shape;
  170. if (area) {
  171. Physics2DServer::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform);
  172. } else {
  173. Physics2DServer::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform);
  174. }
  175. sd.shapes.push_back(s);
  176. total_subshapes++;
  177. }
  178. int CollisionObject2D::shape_owner_get_shape_count(uint32_t p_owner) const {
  179. ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
  180. return shapes[p_owner].shapes.size();
  181. }
  182. Ref<Shape2D> CollisionObject2D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
  183. ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape2D>());
  184. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape2D>());
  185. return shapes[p_owner].shapes[p_shape].shape;
  186. }
  187. int CollisionObject2D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
  188. ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
  189. ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
  190. return shapes[p_owner].shapes[p_shape].index;
  191. }
  192. void CollisionObject2D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
  193. ERR_FAIL_COND(!shapes.has(p_owner));
  194. ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
  195. int index_to_remove = shapes[p_owner].shapes[p_shape].index;
  196. if (area) {
  197. Physics2DServer::get_singleton()->area_remove_shape(rid, index_to_remove);
  198. } else {
  199. Physics2DServer::get_singleton()->body_remove_shape(rid, index_to_remove);
  200. }
  201. shapes[p_owner].shapes.remove(p_shape);
  202. for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  203. for (int i = 0; i < E->get().shapes.size(); i++) {
  204. if (E->get().shapes[i].index > index_to_remove) {
  205. E->get().shapes.write[i].index -= 1;
  206. }
  207. }
  208. }
  209. total_subshapes--;
  210. }
  211. void CollisionObject2D::shape_owner_clear_shapes(uint32_t p_owner) {
  212. ERR_FAIL_COND(!shapes.has(p_owner));
  213. while (shape_owner_get_shape_count(p_owner) > 0) {
  214. shape_owner_remove_shape(p_owner, 0);
  215. }
  216. }
  217. uint32_t CollisionObject2D::shape_find_owner(int p_shape_index) const {
  218. ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, 0);
  219. for (const Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
  220. for (int i = 0; i < E->get().shapes.size(); i++) {
  221. if (E->get().shapes[i].index == p_shape_index) {
  222. return E->key();
  223. }
  224. }
  225. }
  226. //in theory it should be unreachable
  227. return 0;
  228. }
  229. void CollisionObject2D::set_pickable(bool p_enabled) {
  230. if (pickable == p_enabled)
  231. return;
  232. pickable = p_enabled;
  233. _update_pickable();
  234. }
  235. bool CollisionObject2D::is_pickable() const {
  236. return pickable;
  237. }
  238. void CollisionObject2D::_input_event(Node *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape) {
  239. if (get_script_instance()) {
  240. get_script_instance()->call(SceneStringNames::get_singleton()->_input_event, p_viewport, p_input_event, p_shape);
  241. }
  242. emit_signal(SceneStringNames::get_singleton()->input_event, p_viewport, p_input_event, p_shape);
  243. }
  244. void CollisionObject2D::_mouse_enter() {
  245. if (get_script_instance()) {
  246. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter);
  247. }
  248. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  249. }
  250. void CollisionObject2D::_mouse_exit() {
  251. if (get_script_instance()) {
  252. get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit);
  253. }
  254. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  255. }
  256. void CollisionObject2D::set_only_update_transform_changes(bool p_enable) {
  257. only_update_transform_changes = p_enable;
  258. }
  259. void CollisionObject2D::_update_pickable() {
  260. if (!is_inside_tree())
  261. return;
  262. bool pickable = this->pickable && is_inside_tree() && is_visible_in_tree();
  263. if (area)
  264. Physics2DServer::get_singleton()->area_set_pickable(rid, pickable);
  265. else
  266. Physics2DServer::get_singleton()->body_set_pickable(rid, pickable);
  267. }
  268. String CollisionObject2D::get_configuration_warning() const {
  269. String warning = Node2D::get_configuration_warning();
  270. if (shapes.empty()) {
  271. if (warning == String()) {
  272. warning += "\n";
  273. }
  274. 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.");
  275. }
  276. return warning;
  277. }
  278. void CollisionObject2D::_bind_methods() {
  279. ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject2D::get_rid);
  280. ClassDB::bind_method(D_METHOD("set_pickable", "enabled"), &CollisionObject2D::set_pickable);
  281. ClassDB::bind_method(D_METHOD("is_pickable"), &CollisionObject2D::is_pickable);
  282. ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject2D::create_shape_owner);
  283. ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject2D::remove_shape_owner);
  284. ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject2D::_get_shape_owners);
  285. ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject2D::shape_owner_set_transform);
  286. ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject2D::shape_owner_get_transform);
  287. ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject2D::shape_owner_get_owner);
  288. ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject2D::shape_owner_set_disabled);
  289. ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject2D::is_shape_owner_disabled);
  290. ClassDB::bind_method(D_METHOD("shape_owner_set_one_way_collision", "owner_id", "enable"), &CollisionObject2D::shape_owner_set_one_way_collision);
  291. ClassDB::bind_method(D_METHOD("is_shape_owner_one_way_collision_enabled", "owner_id"), &CollisionObject2D::is_shape_owner_one_way_collision_enabled);
  292. ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject2D::shape_owner_add_shape);
  293. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject2D::shape_owner_get_shape_count);
  294. ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape);
  295. ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_get_shape_index);
  296. ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject2D::shape_owner_remove_shape);
  297. ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject2D::shape_owner_clear_shapes);
  298. ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject2D::shape_find_owner);
  299. BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "viewport"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::INT, "shape_idx")));
  300. 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")));
  301. ADD_SIGNAL(MethodInfo("mouse_entered"));
  302. ADD_SIGNAL(MethodInfo("mouse_exited"));
  303. ADD_GROUP("Pickable", "input_");
  304. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_pickable"), "set_pickable", "is_pickable");
  305. ADD_GROUP("", "");
  306. }
  307. CollisionObject2D::CollisionObject2D(RID p_rid, bool p_area) {
  308. rid = p_rid;
  309. area = p_area;
  310. pickable = true;
  311. set_notify_transform(true);
  312. total_subshapes = 0;
  313. only_update_transform_changes = false;
  314. if (p_area) {
  315. Physics2DServer::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
  316. } else {
  317. Physics2DServer::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
  318. }
  319. }
  320. CollisionObject2D::CollisionObject2D() {
  321. //owner=
  322. set_notify_transform(true);
  323. }
  324. CollisionObject2D::~CollisionObject2D() {
  325. Physics2DServer::get_singleton()->free(rid);
  326. }