collision_polygon_2d.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*************************************************************************/
  2. /* collision_polygon_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_polygon_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "engine.h"
  33. #include "scene/resources/concave_polygon_shape_2d.h"
  34. #include "scene/resources/convex_polygon_shape_2d.h"
  35. #include "thirdparty/misc/triangulator.h"
  36. void CollisionPolygon2D::_build_polygon() {
  37. parent->shape_owner_clear_shapes(owner_id);
  38. if (polygon.size() == 0)
  39. return;
  40. bool solids = build_mode == BUILD_SOLIDS;
  41. if (solids) {
  42. //here comes the sun, lalalala
  43. //decompose concave into multiple convex polygons and add them
  44. Vector<Vector<Vector2> > decomp = _decompose_in_convex();
  45. for (int i = 0; i < decomp.size(); i++) {
  46. Ref<ConvexPolygonShape2D> convex = memnew(ConvexPolygonShape2D);
  47. convex->set_points(decomp[i]);
  48. parent->shape_owner_add_shape(owner_id, convex);
  49. }
  50. } else {
  51. Ref<ConcavePolygonShape2D> concave = memnew(ConcavePolygonShape2D);
  52. PoolVector<Vector2> segments;
  53. segments.resize(polygon.size() * 2);
  54. PoolVector<Vector2>::Write w = segments.write();
  55. for (int i = 0; i < polygon.size(); i++) {
  56. w[(i << 1) + 0] = polygon[i];
  57. w[(i << 1) + 1] = polygon[(i + 1) % polygon.size()];
  58. }
  59. w = PoolVector<Vector2>::Write();
  60. concave->set_segments(segments);
  61. parent->shape_owner_add_shape(owner_id, concave);
  62. }
  63. }
  64. Vector<Vector<Vector2> > CollisionPolygon2D::_decompose_in_convex() {
  65. Vector<Vector<Vector2> > decomp;
  66. List<TriangulatorPoly> in_poly, out_poly;
  67. TriangulatorPoly inp;
  68. inp.Init(polygon.size());
  69. for (int i = 0; i < polygon.size(); i++) {
  70. inp.GetPoint(i) = polygon[i];
  71. }
  72. inp.SetOrientation(TRIANGULATOR_CCW);
  73. in_poly.push_back(inp);
  74. TriangulatorPartition tpart;
  75. if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed!
  76. ERR_PRINT("Convex decomposing failed!");
  77. return decomp;
  78. }
  79. decomp.resize(out_poly.size());
  80. int idx = 0;
  81. for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
  82. TriangulatorPoly &tp = I->get();
  83. decomp[idx].resize(tp.GetNumPoints());
  84. for (int i = 0; i < tp.GetNumPoints(); i++) {
  85. decomp[idx][i] = tp.GetPoint(i);
  86. }
  87. idx++;
  88. }
  89. return decomp;
  90. }
  91. void CollisionPolygon2D::_notification(int p_what) {
  92. switch (p_what) {
  93. case NOTIFICATION_PARENTED: {
  94. parent = Object::cast_to<CollisionObject2D>(get_parent());
  95. if (parent) {
  96. owner_id = parent->create_shape_owner(this);
  97. _build_polygon();
  98. parent->shape_owner_set_transform(owner_id, get_transform());
  99. parent->shape_owner_set_disabled(owner_id, disabled);
  100. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  101. }
  102. /*if (Engine::get_singleton()->is_editor_hint()) {
  103. //display above all else
  104. set_z_as_relative(false);
  105. set_z(VS::CANVAS_ITEM_Z_MAX - 1);
  106. }*/
  107. } break;
  108. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  109. if (parent) {
  110. parent->shape_owner_set_transform(owner_id, get_transform());
  111. }
  112. } break;
  113. case NOTIFICATION_UNPARENTED: {
  114. if (parent) {
  115. parent->remove_shape_owner(owner_id);
  116. }
  117. owner_id = 0;
  118. parent = NULL;
  119. } break;
  120. case NOTIFICATION_DRAW: {
  121. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  122. break;
  123. }
  124. for (int i = 0; i < polygon.size(); i++) {
  125. Vector2 p = polygon[i];
  126. Vector2 n = polygon[(i + 1) % polygon.size()];
  127. draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 3);
  128. }
  129. #define DEBUG_DECOMPOSE
  130. #if defined(TOOLS_ENABLED) && defined(DEBUG_DECOMPOSE)
  131. Vector<Vector<Vector2> > decomp = _decompose_in_convex();
  132. Color c(0.4, 0.9, 0.1);
  133. for (int i = 0; i < decomp.size(); i++) {
  134. c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5);
  135. draw_colored_polygon(decomp[i], c);
  136. }
  137. #else
  138. draw_colored_polygon(polygon, get_tree()->get_debug_collisions_color());
  139. #endif
  140. if (one_way_collision) {
  141. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  142. dcol.a = 1.0;
  143. Vector2 line_to(0, 20);
  144. draw_line(Vector2(), line_to, dcol, 3);
  145. Vector<Vector2> pts;
  146. float tsize = 8;
  147. pts.push_back(line_to + (Vector2(0, tsize)));
  148. pts.push_back(line_to + (Vector2(0.707 * tsize, 0)));
  149. pts.push_back(line_to + (Vector2(-0.707 * tsize, 0)));
  150. Vector<Color> cols;
  151. for (int i = 0; i < 3; i++)
  152. cols.push_back(dcol);
  153. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  154. }
  155. } break;
  156. }
  157. }
  158. void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) {
  159. polygon = p_polygon;
  160. {
  161. for (int i = 0; i < polygon.size(); i++) {
  162. if (i == 0)
  163. aabb = Rect2(polygon[i], Size2());
  164. else
  165. aabb.expand_to(polygon[i]);
  166. }
  167. if (aabb == Rect2()) {
  168. aabb = Rect2(-10, -10, 20, 20);
  169. } else {
  170. aabb.position -= aabb.size * 0.3;
  171. aabb.size += aabb.size * 0.6;
  172. }
  173. }
  174. if (parent) {
  175. _build_polygon();
  176. }
  177. update();
  178. update_configuration_warning();
  179. }
  180. Vector<Point2> CollisionPolygon2D::get_polygon() const {
  181. return polygon;
  182. }
  183. void CollisionPolygon2D::set_build_mode(BuildMode p_mode) {
  184. ERR_FAIL_INDEX(p_mode, 2);
  185. build_mode = p_mode;
  186. if (parent) {
  187. _build_polygon();
  188. }
  189. }
  190. CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const {
  191. return build_mode;
  192. }
  193. Rect2 CollisionPolygon2D::get_item_rect() const {
  194. return aabb;
  195. }
  196. String CollisionPolygon2D::get_configuration_warning() const {
  197. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  198. return TTR("CollisionPolygon2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, KinematicBody2D, etc. to give them a shape.");
  199. }
  200. if (polygon.empty()) {
  201. return TTR("An empty CollisionPolygon2D has no effect on collision.");
  202. }
  203. return String();
  204. }
  205. void CollisionPolygon2D::set_disabled(bool p_disabled) {
  206. disabled = p_disabled;
  207. update();
  208. if (parent) {
  209. parent->shape_owner_set_disabled(owner_id, p_disabled);
  210. }
  211. }
  212. bool CollisionPolygon2D::is_disabled() const {
  213. return disabled;
  214. }
  215. void CollisionPolygon2D::set_one_way_collision(bool p_enable) {
  216. one_way_collision = p_enable;
  217. update();
  218. if (parent) {
  219. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  220. }
  221. }
  222. bool CollisionPolygon2D::is_one_way_collision_enabled() const {
  223. return one_way_collision;
  224. }
  225. void CollisionPolygon2D::_bind_methods() {
  226. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon2D::set_polygon);
  227. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon2D::get_polygon);
  228. ClassDB::bind_method(D_METHOD("set_build_mode", "build_mode"), &CollisionPolygon2D::set_build_mode);
  229. ClassDB::bind_method(D_METHOD("get_build_mode"), &CollisionPolygon2D::get_build_mode);
  230. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon2D::set_disabled);
  231. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon2D::is_disabled);
  232. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionPolygon2D::set_one_way_collision);
  233. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionPolygon2D::is_one_way_collision_enabled);
  234. ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Segments"), "set_build_mode", "get_build_mode");
  235. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  236. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  237. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  238. BIND_ENUM_CONSTANT(BUILD_SOLIDS);
  239. BIND_ENUM_CONSTANT(BUILD_SEGMENTS);
  240. }
  241. CollisionPolygon2D::CollisionPolygon2D() {
  242. aabb = Rect2(-10, -10, 20, 20);
  243. build_mode = BUILD_SOLIDS;
  244. set_notify_local_transform(true);
  245. parent = NULL;
  246. owner_id = 0;
  247. disabled = false;
  248. one_way_collision = false;
  249. }