collision_polygon_2d.cpp 11 KB

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