collision_polygon_2d.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*************************************************************************/
  2. /* collision_polygon_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "collision_polygon_2d.h"
  30. #include "collision_object_2d.h"
  31. #include "scene/resources/concave_polygon_shape_2d.h"
  32. #include "scene/resources/convex_polygon_shape_2d.h"
  33. #include "triangulator.h"
  34. void CollisionPolygon2D::_add_to_collision_object(Object *p_obj) {
  35. if (unparenting || !can_update_body)
  36. return;
  37. CollisionObject2D *co = p_obj->cast_to<CollisionObject2D>();
  38. ERR_FAIL_COND(!co);
  39. if (polygon.size()==0)
  40. return;
  41. bool solids=build_mode==BUILD_SOLIDS;
  42. if (solids) {
  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. shape_from=co->get_shape_count();
  47. for(int i=0;i<decomp.size();i++) {
  48. Ref<ConvexPolygonShape2D> convex = memnew( ConvexPolygonShape2D );
  49. convex->set_points(decomp[i]);
  50. co->add_shape(convex,get_transform());
  51. if (trigger)
  52. co->set_shape_as_trigger(co->get_shape_count()-1,true);
  53. }
  54. shape_to=co->get_shape_count()-1;
  55. if (shape_to<shape_from) {
  56. shape_from=-1;
  57. shape_to=-1;
  58. }
  59. } else {
  60. Ref<ConcavePolygonShape2D> concave = memnew( ConcavePolygonShape2D );
  61. DVector<Vector2> segments;
  62. segments.resize(polygon.size()*2);
  63. DVector<Vector2>::Write w=segments.write();
  64. for(int i=0;i<polygon.size();i++) {
  65. w[(i<<1)+0]=polygon[i];
  66. w[(i<<1)+1]=polygon[(i+1)%polygon.size()];
  67. }
  68. w=DVector<Vector2>::Write();
  69. concave->set_segments(segments);
  70. co->add_shape(concave,get_transform());
  71. if (trigger)
  72. co->set_shape_as_trigger(co->get_shape_count()-1,true);
  73. shape_from=co->get_shape_count()-1;
  74. shape_to=co->get_shape_count()-1;
  75. }
  76. //co->add_shape(shape,get_transform());
  77. }
  78. void CollisionPolygon2D::_update_parent() {
  79. if (!can_update_body)
  80. return;
  81. Node *parent = get_parent();
  82. if (!parent)
  83. return;
  84. CollisionObject2D *co = parent->cast_to<CollisionObject2D>();
  85. if (!co)
  86. return;
  87. co->_update_shapes_from_children();
  88. }
  89. Vector< Vector<Vector2> > CollisionPolygon2D::_decompose_in_convex() {
  90. Vector< Vector<Vector2> > decomp;
  91. #if 0
  92. //fast but imprecise triangulator, gave us problems
  93. decomp = Geometry::decompose_polygon(polygon);
  94. #else
  95. List<TriangulatorPoly> in_poly,out_poly;
  96. TriangulatorPoly inp;
  97. inp.Init(polygon.size());
  98. for(int i=0;i<polygon.size();i++) {
  99. inp.GetPoint(i)=polygon[i];
  100. }
  101. inp.SetOrientation(TRIANGULATOR_CCW);
  102. in_poly.push_back(inp);
  103. TriangulatorPartition tpart;
  104. if (tpart.ConvexPartition_HM(&in_poly,&out_poly)==0) { //failed!
  105. ERR_PRINT("Convex decomposing failed!");
  106. return decomp;
  107. }
  108. decomp.resize(out_poly.size());
  109. int idx=0;
  110. for(List<TriangulatorPoly>::Element*I = out_poly.front();I;I=I->next()) {
  111. TriangulatorPoly& tp = I->get();
  112. decomp[idx].resize(tp.GetNumPoints());
  113. for(int i=0;i<tp.GetNumPoints();i++) {
  114. decomp[idx][i]=tp.GetPoint(i);
  115. }
  116. idx++;
  117. }
  118. #endif
  119. return decomp;
  120. }
  121. void CollisionPolygon2D::_notification(int p_what) {
  122. switch(p_what) {
  123. case NOTIFICATION_ENTER_TREE: {
  124. unparenting=false;
  125. can_update_body=get_tree()->is_editor_hint();
  126. if (!get_tree()->is_editor_hint()) {
  127. //display above all else
  128. set_z_as_relative(false);
  129. set_z(VS::CANVAS_ITEM_Z_MAX-1);
  130. }
  131. } break;
  132. case NOTIFICATION_EXIT_TREE: {
  133. can_update_body=false;
  134. } break;
  135. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  136. if (!is_inside_tree())
  137. break;
  138. if (can_update_body) {
  139. _update_parent();
  140. } else if (shape_from>=0 && shape_to>=0) {
  141. CollisionObject2D *co = get_parent()->cast_to<CollisionObject2D>();
  142. for(int i=shape_from;i<=shape_to;i++) {
  143. co->set_shape_transform(i,get_transform());
  144. }
  145. }
  146. } break;
  147. case NOTIFICATION_DRAW: {
  148. if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  149. break;
  150. }
  151. for(int i=0;i<polygon.size();i++) {
  152. Vector2 p = polygon[i];
  153. Vector2 n = polygon[(i+1)%polygon.size()];
  154. draw_line(p,n,Color(0.9,0.2,0.0,0.8),3);
  155. }
  156. #define DEBUG_DECOMPOSE
  157. #if defined(TOOLS_ENABLED) && defined (DEBUG_DECOMPOSE)
  158. Vector< Vector<Vector2> > decomp = _decompose_in_convex();
  159. Color c(0.4,0.9,0.1);
  160. for(int i=0;i<decomp.size();i++) {
  161. c.set_hsv( Math::fmod(c.get_h() + 0.738,1),c.get_s(),c.get_v(),0.5);
  162. draw_colored_polygon(decomp[i],c);
  163. }
  164. #else
  165. draw_colored_polygon(polygon,get_tree()->get_debug_collisions_color());
  166. #endif
  167. } break;
  168. case NOTIFICATION_UNPARENTED: {
  169. unparenting = true;
  170. _update_parent();
  171. } break;
  172. }
  173. }
  174. void CollisionPolygon2D::set_polygon(const Vector<Point2>& p_polygon) {
  175. polygon=p_polygon;
  176. if (can_update_body) {
  177. for(int i=0;i<polygon.size();i++) {
  178. if (i==0)
  179. aabb=Rect2(polygon[i],Size2());
  180. else
  181. aabb.expand_to(polygon[i]);
  182. }
  183. if (aabb==Rect2()) {
  184. aabb=Rect2(-10,-10,20,20);
  185. } else {
  186. aabb.pos-=aabb.size*0.3;
  187. aabb.size+=aabb.size*0.6;
  188. }
  189. _update_parent();
  190. }
  191. update();
  192. }
  193. Vector<Point2> CollisionPolygon2D::get_polygon() const {
  194. return polygon;
  195. }
  196. void CollisionPolygon2D::set_build_mode(BuildMode p_mode) {
  197. ERR_FAIL_INDEX(p_mode,2);
  198. build_mode=p_mode;
  199. _update_parent();
  200. }
  201. CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const{
  202. return build_mode;
  203. }
  204. Rect2 CollisionPolygon2D::get_item_rect() const {
  205. return aabb;
  206. }
  207. void CollisionPolygon2D::set_trigger(bool p_trigger) {
  208. trigger=p_trigger;
  209. _update_parent();
  210. if (!can_update_body && is_inside_tree() && shape_from>=0 && shape_to>=0) {
  211. CollisionObject2D *co = get_parent()->cast_to<CollisionObject2D>();
  212. for(int i=shape_from;i<=shape_to;i++) {
  213. co->set_shape_as_trigger(i,p_trigger);
  214. }
  215. }
  216. }
  217. bool CollisionPolygon2D::is_trigger() const{
  218. return trigger;
  219. }
  220. void CollisionPolygon2D::_set_shape_range(const Vector2& p_range) {
  221. shape_from=p_range.x;
  222. shape_to=p_range.y;
  223. }
  224. Vector2 CollisionPolygon2D::_get_shape_range() const {
  225. return Vector2(shape_from,shape_to);
  226. }
  227. void CollisionPolygon2D::_bind_methods() {
  228. ObjectTypeDB::bind_method(_MD("_add_to_collision_object"),&CollisionPolygon2D::_add_to_collision_object);
  229. ObjectTypeDB::bind_method(_MD("set_polygon","polygon"),&CollisionPolygon2D::set_polygon);
  230. ObjectTypeDB::bind_method(_MD("get_polygon"),&CollisionPolygon2D::get_polygon);
  231. ObjectTypeDB::bind_method(_MD("set_build_mode","build_mode"),&CollisionPolygon2D::set_build_mode);
  232. ObjectTypeDB::bind_method(_MD("get_build_mode"),&CollisionPolygon2D::get_build_mode);
  233. ObjectTypeDB::bind_method(_MD("set_trigger","trigger"),&CollisionPolygon2D::set_trigger);
  234. ObjectTypeDB::bind_method(_MD("is_trigger"),&CollisionPolygon2D::is_trigger);
  235. ObjectTypeDB::bind_method(_MD("_set_shape_range","shape_range"),&CollisionPolygon2D::_set_shape_range);
  236. ObjectTypeDB::bind_method(_MD("_get_shape_range"),&CollisionPolygon2D::_get_shape_range);
  237. ObjectTypeDB::bind_method(_MD("get_collision_object_first_shape"),&CollisionPolygon2D::get_collision_object_first_shape);
  238. ObjectTypeDB::bind_method(_MD("get_collision_object_last_shape"),&CollisionPolygon2D::get_collision_object_last_shape);
  239. ADD_PROPERTY( PropertyInfo(Variant::INT,"build_mode",PROPERTY_HINT_ENUM,"Solids,Segments"),_SCS("set_build_mode"),_SCS("get_build_mode"));
  240. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2_ARRAY,"polygon"),_SCS("set_polygon"),_SCS("get_polygon"));
  241. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"shape_range",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_shape_range"),_SCS("_get_shape_range"));
  242. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"trigger"),_SCS("set_trigger"),_SCS("is_trigger"));
  243. }
  244. CollisionPolygon2D::CollisionPolygon2D() {
  245. aabb=Rect2(-10,-10,20,20);
  246. build_mode=BUILD_SOLIDS;
  247. trigger=false;
  248. unparenting=false;
  249. shape_from=-1;
  250. shape_to=-1;
  251. can_update_body=false;
  252. set_notify_local_transform(true);
  253. }