collision_polygon_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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-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_polygon_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "scene/resources/concave_polygon_shape_2d.h"
  34. #include "scene/resources/convex_polygon_shape_2d.h"
  35. #include "thirdparty/misc/polypartition.h"
  36. void CollisionPolygon2D::_build_polygon() {
  37. parent->shape_owner_clear_shapes(owner_id);
  38. bool solids = build_mode == BUILD_SOLIDS;
  39. if (solids) {
  40. if (polygon.size() < 3) {
  41. return;
  42. }
  43. //here comes the sun, lalalala
  44. //decompose concave into multiple convex polygons and add them
  45. Vector<Vector<Vector2>> decomp = _decompose_in_convex();
  46. for (int i = 0; i < decomp.size(); i++) {
  47. Ref<ConvexPolygonShape2D> convex = memnew(ConvexPolygonShape2D);
  48. convex->set_points(decomp[i]);
  49. parent->shape_owner_add_shape(owner_id, convex);
  50. }
  51. } else {
  52. if (polygon.size() < 2) {
  53. return;
  54. }
  55. Ref<ConcavePolygonShape2D> concave = memnew(ConcavePolygonShape2D);
  56. Vector<Vector2> segments;
  57. segments.resize(polygon.size() * 2);
  58. Vector2 *w = segments.ptrw();
  59. for (int i = 0; i < polygon.size(); i++) {
  60. w[(i << 1) + 0] = polygon[i];
  61. w[(i << 1) + 1] = polygon[(i + 1) % polygon.size()];
  62. }
  63. concave->set_segments(segments);
  64. parent->shape_owner_add_shape(owner_id, concave);
  65. }
  66. }
  67. Vector<Vector<Vector2>> CollisionPolygon2D::_decompose_in_convex() {
  68. Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon);
  69. return decomp;
  70. }
  71. void CollisionPolygon2D::_update_in_shape_owner(bool p_xform_only) {
  72. parent->shape_owner_set_transform(owner_id, get_transform());
  73. if (p_xform_only) {
  74. return;
  75. }
  76. parent->shape_owner_set_disabled(owner_id, disabled);
  77. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  78. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  79. }
  80. void CollisionPolygon2D::_notification(int p_what) {
  81. switch (p_what) {
  82. case NOTIFICATION_PARENTED: {
  83. parent = Object::cast_to<CollisionObject2D>(get_parent());
  84. if (parent) {
  85. owner_id = parent->create_shape_owner(this);
  86. _build_polygon();
  87. _update_in_shape_owner();
  88. }
  89. /*if (Engine::get_singleton()->is_editor_hint()) {
  90. //display above all else
  91. set_z_as_relative(false);
  92. set_z_index(RS::CANVAS_ITEM_Z_MAX - 1);
  93. }*/
  94. } break;
  95. case NOTIFICATION_ENTER_TREE: {
  96. if (parent) {
  97. _update_in_shape_owner();
  98. }
  99. } break;
  100. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  101. if (parent) {
  102. _update_in_shape_owner(true);
  103. }
  104. } break;
  105. case NOTIFICATION_UNPARENTED: {
  106. if (parent) {
  107. parent->remove_shape_owner(owner_id);
  108. }
  109. owner_id = 0;
  110. parent = nullptr;
  111. } break;
  112. case NOTIFICATION_DRAW: {
  113. ERR_FAIL_COND(!is_inside_tree());
  114. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  115. break;
  116. }
  117. int polygon_count = polygon.size();
  118. for (int i = 0; i < polygon_count; i++) {
  119. Vector2 p = polygon[i];
  120. Vector2 n = polygon[(i + 1) % polygon_count];
  121. // draw line with width <= 1, so it does not scale with zoom and break pixel exact editing
  122. draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 1);
  123. }
  124. if (polygon_count > 2) {
  125. #define DEBUG_DECOMPOSE
  126. #if defined(TOOLS_ENABLED) && defined(DEBUG_DECOMPOSE)
  127. Vector<Vector<Vector2>> decomp = _decompose_in_convex();
  128. Color c(0.4, 0.9, 0.1);
  129. for (int i = 0; i < decomp.size(); i++) {
  130. c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5);
  131. draw_colored_polygon(decomp[i], c);
  132. }
  133. #else
  134. draw_colored_polygon(polygon, get_tree()->get_debug_collisions_color());
  135. #endif
  136. }
  137. if (one_way_collision) {
  138. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  139. dcol.a = 1.0;
  140. Vector2 line_to(0, 20);
  141. draw_line(Vector2(), line_to, dcol, 3);
  142. Vector<Vector2> pts;
  143. real_t tsize = 8;
  144. pts.push_back(line_to + (Vector2(0, tsize)));
  145. pts.push_back(line_to + (Vector2(Math_SQRT12 * tsize, 0)));
  146. pts.push_back(line_to + (Vector2(-Math_SQRT12 * tsize, 0)));
  147. Vector<Color> cols;
  148. for (int i = 0; i < 3; i++) {
  149. cols.push_back(dcol);
  150. }
  151. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  152. }
  153. } break;
  154. }
  155. }
  156. void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) {
  157. polygon = p_polygon;
  158. {
  159. for (int i = 0; i < polygon.size(); i++) {
  160. if (i == 0) {
  161. aabb = Rect2(polygon[i], Size2());
  162. } else {
  163. aabb.expand_to(polygon[i]);
  164. }
  165. }
  166. if (aabb == Rect2()) {
  167. aabb = Rect2(-10, -10, 20, 20);
  168. } else {
  169. aabb.position -= aabb.size * 0.3;
  170. aabb.size += aabb.size * 0.6;
  171. }
  172. }
  173. if (parent) {
  174. _build_polygon();
  175. _update_in_shape_owner();
  176. }
  177. update();
  178. update_configuration_warnings();
  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((int)p_mode, 2);
  185. build_mode = p_mode;
  186. if (parent) {
  187. _build_polygon();
  188. _update_in_shape_owner();
  189. }
  190. update();
  191. update_configuration_warnings();
  192. }
  193. CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const {
  194. return build_mode;
  195. }
  196. #ifdef TOOLS_ENABLED
  197. Rect2 CollisionPolygon2D::_edit_get_rect() const {
  198. return aabb;
  199. }
  200. bool CollisionPolygon2D::_edit_use_rect() const {
  201. return true;
  202. }
  203. bool CollisionPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  204. return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));
  205. }
  206. #endif
  207. TypedArray<String> CollisionPolygon2D::get_configuration_warnings() const {
  208. TypedArray<String> warnings = Node::get_configuration_warnings();
  209. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  210. warnings.push_back(TTR("CollisionPolygon2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidDynamicBody2D, CharacterBody2D, etc. to give them a shape."));
  211. }
  212. int polygon_count = polygon.size();
  213. if (polygon_count == 0) {
  214. warnings.push_back(TTR("An empty CollisionPolygon2D has no effect on collision."));
  215. } else {
  216. bool solids = build_mode == BUILD_SOLIDS;
  217. if (solids) {
  218. if (polygon_count < 3) {
  219. warnings.push_back(TTR("Invalid polygon. At least 3 points are needed in 'Solids' build mode."));
  220. }
  221. } else if (polygon_count < 2) {
  222. warnings.push_back(TTR("Invalid polygon. At least 2 points are needed in 'Segments' build mode."));
  223. }
  224. }
  225. return warnings;
  226. }
  227. void CollisionPolygon2D::set_disabled(bool p_disabled) {
  228. disabled = p_disabled;
  229. update();
  230. if (parent) {
  231. parent->shape_owner_set_disabled(owner_id, p_disabled);
  232. }
  233. }
  234. bool CollisionPolygon2D::is_disabled() const {
  235. return disabled;
  236. }
  237. void CollisionPolygon2D::set_one_way_collision(bool p_enable) {
  238. one_way_collision = p_enable;
  239. update();
  240. if (parent) {
  241. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  242. }
  243. }
  244. bool CollisionPolygon2D::is_one_way_collision_enabled() const {
  245. return one_way_collision;
  246. }
  247. void CollisionPolygon2D::set_one_way_collision_margin(real_t p_margin) {
  248. one_way_collision_margin = p_margin;
  249. if (parent) {
  250. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  251. }
  252. }
  253. real_t CollisionPolygon2D::get_one_way_collision_margin() const {
  254. return one_way_collision_margin;
  255. }
  256. void CollisionPolygon2D::_bind_methods() {
  257. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon2D::set_polygon);
  258. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon2D::get_polygon);
  259. ClassDB::bind_method(D_METHOD("set_build_mode", "build_mode"), &CollisionPolygon2D::set_build_mode);
  260. ClassDB::bind_method(D_METHOD("get_build_mode"), &CollisionPolygon2D::get_build_mode);
  261. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon2D::set_disabled);
  262. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon2D::is_disabled);
  263. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionPolygon2D::set_one_way_collision);
  264. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionPolygon2D::is_one_way_collision_enabled);
  265. ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionPolygon2D::set_one_way_collision_margin);
  266. ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionPolygon2D::get_one_way_collision_margin);
  267. ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Segments"), "set_build_mode", "get_build_mode");
  268. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  269. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  270. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  271. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1"), "set_one_way_collision_margin", "get_one_way_collision_margin");
  272. BIND_ENUM_CONSTANT(BUILD_SOLIDS);
  273. BIND_ENUM_CONSTANT(BUILD_SEGMENTS);
  274. }
  275. CollisionPolygon2D::CollisionPolygon2D() {
  276. set_notify_local_transform(true);
  277. }