collision_object_2d.cpp 13 KB

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