collision_polygon_2d.cpp 10 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-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_polygon_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "core/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 = Geometry::decompose_polygon_in_convex(polygon);
  66. return decomp;
  67. }
  68. void CollisionPolygon2D::_update_in_shape_owner(bool p_xform_only) {
  69. parent->shape_owner_set_transform(owner_id, get_transform());
  70. if (p_xform_only)
  71. return;
  72. parent->shape_owner_set_disabled(owner_id, disabled);
  73. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  74. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  75. }
  76. void CollisionPolygon2D::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_PARENTED: {
  79. parent = Object::cast_to<CollisionObject2D>(get_parent());
  80. if (parent) {
  81. owner_id = parent->create_shape_owner(this);
  82. _build_polygon();
  83. _update_in_shape_owner();
  84. }
  85. /*if (Engine::get_singleton()->is_editor_hint()) {
  86. //display above all else
  87. set_z_as_relative(false);
  88. set_z_index(VS::CANVAS_ITEM_Z_MAX - 1);
  89. }*/
  90. } break;
  91. case NOTIFICATION_ENTER_TREE: {
  92. if (parent) {
  93. _update_in_shape_owner();
  94. }
  95. } break;
  96. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  97. if (parent) {
  98. _update_in_shape_owner(true);
  99. }
  100. } break;
  101. case NOTIFICATION_UNPARENTED: {
  102. if (parent) {
  103. parent->remove_shape_owner(owner_id);
  104. }
  105. owner_id = 0;
  106. parent = NULL;
  107. } break;
  108. case NOTIFICATION_DRAW: {
  109. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  110. break;
  111. }
  112. for (int i = 0; i < polygon.size(); i++) {
  113. Vector2 p = polygon[i];
  114. Vector2 n = polygon[(i + 1) % polygon.size()];
  115. // draw line with width <= 1, so it does not scale with zoom and break pixel exact editing
  116. draw_line(p, n, Color(0.9, 0.2, 0.0, 0.8), 1);
  117. }
  118. #define DEBUG_DECOMPOSE
  119. #if defined(TOOLS_ENABLED) && defined(DEBUG_DECOMPOSE)
  120. Vector<Vector<Vector2> > decomp = _decompose_in_convex();
  121. Color c(0.4, 0.9, 0.1);
  122. for (int i = 0; i < decomp.size(); i++) {
  123. c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5);
  124. draw_colored_polygon(decomp[i], c);
  125. }
  126. #else
  127. draw_colored_polygon(polygon, get_tree()->get_debug_collisions_color());
  128. #endif
  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. Vector<Vector2> pts;
  135. float tsize = 8;
  136. pts.push_back(line_to + (Vector2(0, tsize)));
  137. pts.push_back(line_to + (Vector2(0.707 * tsize, 0)));
  138. pts.push_back(line_to + (Vector2(-0.707 * tsize, 0)));
  139. Vector<Color> cols;
  140. for (int i = 0; i < 3; i++)
  141. cols.push_back(dcol);
  142. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  143. }
  144. } break;
  145. }
  146. }
  147. void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) {
  148. polygon = p_polygon;
  149. {
  150. for (int i = 0; i < polygon.size(); i++) {
  151. if (i == 0)
  152. aabb = Rect2(polygon[i], Size2());
  153. else
  154. aabb.expand_to(polygon[i]);
  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 (parent) {
  164. _build_polygon();
  165. }
  166. update();
  167. update_configuration_warning();
  168. }
  169. Vector<Point2> CollisionPolygon2D::get_polygon() const {
  170. return polygon;
  171. }
  172. void CollisionPolygon2D::set_build_mode(BuildMode p_mode) {
  173. ERR_FAIL_INDEX((int)p_mode, 2);
  174. build_mode = p_mode;
  175. if (parent) {
  176. _build_polygon();
  177. }
  178. }
  179. CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const {
  180. return build_mode;
  181. }
  182. Rect2 CollisionPolygon2D::_edit_get_rect() const {
  183. return aabb;
  184. }
  185. bool CollisionPolygon2D::_edit_use_rect() const {
  186. return true;
  187. }
  188. bool CollisionPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  189. return Geometry::is_point_in_polygon(p_point, Variant(polygon));
  190. }
  191. String CollisionPolygon2D::get_configuration_warning() const {
  192. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  193. 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.");
  194. }
  195. if (polygon.empty()) {
  196. return TTR("An empty CollisionPolygon2D has no effect on collision.");
  197. }
  198. return String();
  199. }
  200. void CollisionPolygon2D::set_disabled(bool p_disabled) {
  201. disabled = p_disabled;
  202. update();
  203. if (parent) {
  204. parent->shape_owner_set_disabled(owner_id, p_disabled);
  205. }
  206. }
  207. bool CollisionPolygon2D::is_disabled() const {
  208. return disabled;
  209. }
  210. void CollisionPolygon2D::set_one_way_collision(bool p_enable) {
  211. one_way_collision = p_enable;
  212. update();
  213. if (parent) {
  214. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  215. }
  216. }
  217. bool CollisionPolygon2D::is_one_way_collision_enabled() const {
  218. return one_way_collision;
  219. }
  220. void CollisionPolygon2D::set_one_way_collision_margin(float p_margin) {
  221. one_way_collision_margin = p_margin;
  222. if (parent) {
  223. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  224. }
  225. }
  226. float CollisionPolygon2D::get_one_way_collision_margin() const {
  227. return one_way_collision_margin;
  228. }
  229. void CollisionPolygon2D::_bind_methods() {
  230. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon2D::set_polygon);
  231. ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon2D::get_polygon);
  232. ClassDB::bind_method(D_METHOD("set_build_mode", "build_mode"), &CollisionPolygon2D::set_build_mode);
  233. ClassDB::bind_method(D_METHOD("get_build_mode"), &CollisionPolygon2D::get_build_mode);
  234. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon2D::set_disabled);
  235. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon2D::is_disabled);
  236. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionPolygon2D::set_one_way_collision);
  237. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionPolygon2D::is_one_way_collision_enabled);
  238. ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionPolygon2D::set_one_way_collision_margin);
  239. ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionPolygon2D::get_one_way_collision_margin);
  240. ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Segments"), "set_build_mode", "get_build_mode");
  241. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  244. ADD_PROPERTY(PropertyInfo(Variant::REAL, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1"), "set_one_way_collision_margin", "get_one_way_collision_margin");
  245. BIND_ENUM_CONSTANT(BUILD_SOLIDS);
  246. BIND_ENUM_CONSTANT(BUILD_SEGMENTS);
  247. }
  248. CollisionPolygon2D::CollisionPolygon2D() {
  249. aabb = Rect2(-10, -10, 20, 20);
  250. build_mode = BUILD_SOLIDS;
  251. set_notify_local_transform(true);
  252. parent = NULL;
  253. owner_id = 0;
  254. disabled = false;
  255. one_way_collision = false;
  256. one_way_collision_margin = 1.0;
  257. }