collision_polygon.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* collision_polygon.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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.h"
  31. #include "collision_object.h"
  32. #include "scene/resources/concave_polygon_shape.h"
  33. #include "scene/resources/convex_polygon_shape.h"
  34. void CollisionPolygon::_add_to_collision_object(Object *p_obj) {
  35. if (!can_update_body)
  36. return;
  37. CollisionObject *co = p_obj->cast_to<CollisionObject>();
  38. ERR_FAIL_COND(!co);
  39. if (polygon.size() == 0)
  40. return;
  41. bool solids = build_mode == BUILD_SOLIDS;
  42. Vector<Vector<Vector2> > decomp = Geometry::decompose_polygon(polygon);
  43. if (decomp.size() == 0)
  44. return;
  45. if (true || solids) {
  46. //here comes the sun, lalalala
  47. //decompose concave into multiple convex polygons and add them
  48. shape_from = co->get_shape_count();
  49. for (int i = 0; i < decomp.size(); i++) {
  50. Ref<ConvexPolygonShape> convex = memnew(ConvexPolygonShape);
  51. DVector<Vector3> cp;
  52. int cs = decomp[i].size();
  53. cp.resize(cs * 2);
  54. {
  55. DVector<Vector3>::Write w = cp.write();
  56. int idx = 0;
  57. for (int j = 0; j < cs; j++) {
  58. Vector2 d = decomp[i][j];
  59. w[idx++] = Vector3(d.x, d.y, depth * 0.5);
  60. w[idx++] = Vector3(d.x, d.y, -depth * 0.5);
  61. }
  62. }
  63. convex->set_points(cp);
  64. co->add_shape(convex, get_transform());
  65. }
  66. shape_to = co->get_shape_count() - 1;
  67. if (shape_to < shape_from) {
  68. shape_from = -1;
  69. shape_to = -1;
  70. }
  71. } else {
  72. #if 0
  73. Ref<ConcavePolygonShape> concave = memnew( ConcavePolygonShape );
  74. DVector<Vector2> segments;
  75. segments.resize(polygon.size()*2);
  76. DVector<Vector2>::Write w=segments.write();
  77. for(int i=0;i<polygon.size();i++) {
  78. w[(i<<1)+0]=polygon[i];
  79. w[(i<<1)+1]=polygon[(i+1)%polygon.size()];
  80. }
  81. w=DVector<Vector2>::Write();
  82. concave->set_segments(segments);
  83. co->add_shape(concave,get_transform());
  84. #endif
  85. }
  86. //co->add_shape(shape,get_transform());
  87. }
  88. void CollisionPolygon::_update_xform_in_parent() {
  89. if (shape_from >= 0 && shape_to >= 0) {
  90. CollisionObject *co = get_parent()->cast_to<CollisionObject>();
  91. if (co) {
  92. for (int i = shape_from; i <= shape_to; i++) {
  93. co->set_shape_transform(i, get_transform());
  94. }
  95. }
  96. }
  97. }
  98. void CollisionPolygon::_update_parent() {
  99. if (!can_update_body)
  100. return;
  101. Node *parent = get_parent();
  102. if (!parent)
  103. return;
  104. CollisionObject *co = parent->cast_to<CollisionObject>();
  105. if (!co)
  106. return;
  107. co->_update_shapes_from_children();
  108. }
  109. void CollisionPolygon::_set_shape_range(const Vector2 &p_range) {
  110. shape_from = p_range.x;
  111. shape_to = p_range.y;
  112. }
  113. Vector2 CollisionPolygon::_get_shape_range() const {
  114. return Vector2(shape_from, shape_to);
  115. }
  116. void CollisionPolygon::_notification(int p_what) {
  117. switch (p_what) {
  118. case NOTIFICATION_ENTER_TREE: {
  119. can_update_body = get_tree()->is_editor_hint();
  120. set_notify_local_transform(!can_update_body);
  121. if (!can_update_body) {
  122. _update_xform_in_parent();
  123. }
  124. //indicator_instance = VisualServer::get_singleton()->instance_create2(indicator,get_world()->get_scenario());
  125. } break;
  126. case NOTIFICATION_EXIT_TREE: {
  127. can_update_body = false;
  128. set_notify_local_transform(false);
  129. } break;
  130. case NOTIFICATION_TRANSFORM_CHANGED: {
  131. if (!is_inside_tree())
  132. break;
  133. if (can_update_body) {
  134. _update_parent();
  135. }
  136. } break;
  137. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  138. if (!can_update_body) {
  139. _update_xform_in_parent();
  140. }
  141. } break;
  142. #if 0
  143. case NOTIFICATION_DRAW: {
  144. for(int i=0;i<polygon.size();i++) {
  145. Vector2 p = polygon[i];
  146. Vector2 n = polygon[(i+1)%polygon.size()];
  147. draw_line(p,n,Color(0,0.6,0.7,0.5),3);
  148. }
  149. Vector< Vector<Vector2> > decomp = Geometry::decompose_polygon(polygon);
  150. #define DEBUG_DECOMPOSE
  151. #ifdef DEBUG_DECOMPOSE
  152. Color c(0.4,0.9,0.1);
  153. for(int i=0;i<decomp.size();i++) {
  154. c.set_hsv( Math::fmod(c.get_h() + 0.738,1),c.get_s(),c.get_v(),0.5);
  155. draw_colored_polygon(decomp[i],c);
  156. }
  157. #endif
  158. } break;
  159. #endif
  160. }
  161. }
  162. void CollisionPolygon::set_polygon(const Vector<Point2> &p_polygon) {
  163. polygon = p_polygon;
  164. if (can_update_body) {
  165. for (int i = 0; i < polygon.size(); i++) {
  166. Vector3 p1(polygon[i].x, polygon[i].y, depth * 0.5);
  167. if (i == 0)
  168. aabb = AABB(p1, Vector3());
  169. else
  170. aabb.expand_to(p1);
  171. Vector3 p2(polygon[i].x, polygon[i].y, -depth * 0.5);
  172. aabb.expand_to(p2);
  173. }
  174. if (aabb == AABB()) {
  175. aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  176. } else {
  177. aabb.pos -= aabb.size * 0.3;
  178. aabb.size += aabb.size * 0.6;
  179. }
  180. _update_parent();
  181. }
  182. update_gizmo();
  183. }
  184. Vector<Point2> CollisionPolygon::get_polygon() const {
  185. return polygon;
  186. }
  187. void CollisionPolygon::set_build_mode(BuildMode p_mode) {
  188. ERR_FAIL_INDEX(p_mode, 2);
  189. build_mode = p_mode;
  190. if (!can_update_body)
  191. return;
  192. _update_parent();
  193. }
  194. CollisionPolygon::BuildMode CollisionPolygon::get_build_mode() const {
  195. return build_mode;
  196. }
  197. AABB CollisionPolygon::get_item_rect() const {
  198. return aabb;
  199. }
  200. void CollisionPolygon::set_depth(float p_depth) {
  201. depth = p_depth;
  202. if (!can_update_body)
  203. return;
  204. _update_parent();
  205. update_gizmo();
  206. }
  207. float CollisionPolygon::get_depth() const {
  208. return depth;
  209. }
  210. String CollisionPolygon::get_configuration_warning() const {
  211. if (!get_parent()->cast_to<CollisionObject>()) {
  212. return TTR("CollisionPolygon only serves to provide a collision shape to a CollisionObject derived node. Please only use it as a child of Area, StaticBody, RigidBody, KinematicBody, etc. to give them a shape.");
  213. }
  214. if (polygon.empty()) {
  215. return TTR("An empty CollisionPolygon has no effect on collision.");
  216. }
  217. return String();
  218. }
  219. void CollisionPolygon::_bind_methods() {
  220. ObjectTypeDB::bind_method(_MD("_add_to_collision_object"), &CollisionPolygon::_add_to_collision_object);
  221. ObjectTypeDB::bind_method(_MD("set_build_mode", "build_mode"), &CollisionPolygon::set_build_mode);
  222. ObjectTypeDB::bind_method(_MD("get_build_mode"), &CollisionPolygon::get_build_mode);
  223. ObjectTypeDB::bind_method(_MD("set_depth", "depth"), &CollisionPolygon::set_depth);
  224. ObjectTypeDB::bind_method(_MD("get_depth"), &CollisionPolygon::get_depth);
  225. ObjectTypeDB::bind_method(_MD("set_polygon", "polygon"), &CollisionPolygon::set_polygon);
  226. ObjectTypeDB::bind_method(_MD("get_polygon"), &CollisionPolygon::get_polygon);
  227. ObjectTypeDB::bind_method(_MD("_set_shape_range", "shape_range"), &CollisionPolygon::_set_shape_range);
  228. ObjectTypeDB::bind_method(_MD("_get_shape_range"), &CollisionPolygon::_get_shape_range);
  229. ObjectTypeDB::bind_method(_MD("get_collision_object_first_shape"), &CollisionPolygon::get_collision_object_first_shape);
  230. ObjectTypeDB::bind_method(_MD("get_collision_object_last_shape"), &CollisionPolygon::get_collision_object_last_shape);
  231. ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Triangles"), _SCS("set_build_mode"), _SCS("get_build_mode"));
  232. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth"), _SCS("set_depth"), _SCS("get_depth"));
  233. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2_ARRAY, "polygon"), _SCS("set_polygon"), _SCS("get_polygon"));
  234. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shape_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_shape_range"), _SCS("_get_shape_range"));
  235. }
  236. CollisionPolygon::CollisionPolygon() {
  237. shape_from = -1;
  238. shape_to = -1;
  239. can_update_body = false;
  240. aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  241. build_mode = BUILD_SOLIDS;
  242. depth = 1.0;
  243. }