collision_shape_2d.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*************************************************************************/
  2. /* collision_shape_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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_shape_2d.h"
  30. #include "collision_object_2d.h"
  31. #include "scene/resources/segment_shape_2d.h"
  32. #include "scene/resources/shape_line_2d.h"
  33. #include "scene/resources/circle_shape_2d.h"
  34. #include "scene/resources/rectangle_shape_2d.h"
  35. #include "scene/resources/capsule_shape_2d.h"
  36. #include "scene/resources/convex_polygon_shape_2d.h"
  37. #include "scene/resources/concave_polygon_shape_2d.h"
  38. void CollisionShape2D::_add_to_collision_object(Object *p_obj) {
  39. if (unparenting)
  40. return;
  41. CollisionObject2D *co = p_obj->cast_to<CollisionObject2D>();
  42. ERR_FAIL_COND(!co);
  43. co->add_shape(shape,get_transform());
  44. if (trigger)
  45. co->set_shape_as_trigger(co->get_shape_count()-1,true);
  46. }
  47. void CollisionShape2D::_shape_changed() {
  48. update();
  49. _update_parent();
  50. }
  51. void CollisionShape2D::_update_parent() {
  52. Node *parent = get_parent();
  53. if (!parent)
  54. return;
  55. CollisionObject2D *co = parent->cast_to<CollisionObject2D>();
  56. if (!co)
  57. return;
  58. co->_update_shapes_from_children();
  59. }
  60. void CollisionShape2D::_notification(int p_what) {
  61. switch(p_what) {
  62. case NOTIFICATION_ENTER_TREE: {
  63. unparenting=false;
  64. } break;
  65. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  66. if (!is_inside_tree())
  67. break;
  68. _update_parent();
  69. } break;
  70. /*
  71. case NOTIFICATION_TRANSFORM_CHANGED: {
  72. if (!is_inside_scene())
  73. break;
  74. _update_parent();
  75. } break;*/
  76. case NOTIFICATION_DRAW: {
  77. if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
  78. break;
  79. rect=Rect2();
  80. Color draw_col=Color(0,0.6,0.7,0.5);
  81. if (shape->cast_to<LineShape2D>()) {
  82. LineShape2D *l = shape->cast_to<LineShape2D>();
  83. Vector2 point = l->get_d() * l->get_normal();
  84. Vector2 l1[2]={point-l->get_normal().tangent()*100,point+l->get_normal().tangent()*100};
  85. draw_line(l1[0],l1[1],draw_col,3);
  86. Vector2 l2[2]={point,point+l->get_normal()*30};
  87. draw_line(l2[0],l2[1],draw_col,3);
  88. rect.pos=l1[0];
  89. rect.expand_to(l1[1]);
  90. rect.expand_to(l2[0]);
  91. rect.expand_to(l2[1]);
  92. } else if (shape->cast_to<SegmentShape2D>()) {
  93. SegmentShape2D *s = shape->cast_to<SegmentShape2D>();
  94. draw_line(s->get_a(),s->get_b(),draw_col,3);
  95. rect.pos=s->get_a();
  96. rect.expand_to(s->get_b());
  97. } else if (shape->cast_to<RayShape2D>()) {
  98. RayShape2D *s = shape->cast_to<RayShape2D>();
  99. Vector2 tip = Vector2(0,s->get_length());
  100. draw_line(Vector2(),tip,draw_col,3);
  101. Vector<Vector2> pts;
  102. float tsize=4;
  103. pts.push_back(tip+Vector2(0,tsize));
  104. pts.push_back(tip+Vector2(0.707*tsize,0));
  105. pts.push_back(tip+Vector2(-0.707*tsize,0));
  106. Vector<Color> cols;
  107. for(int i=0;i<3;i++)
  108. cols.push_back(draw_col);
  109. draw_primitive(pts,cols,Vector<Vector2>()); //small arrow
  110. rect.pos=Vector2();
  111. rect.expand_to(tip);
  112. rect=rect.grow(0.707*tsize);
  113. } else if (shape->cast_to<CircleShape2D>()) {
  114. CircleShape2D *s = shape->cast_to<CircleShape2D>();
  115. Vector<Vector2> points;
  116. for(int i=0;i<24;i++) {
  117. points.push_back(Vector2(Math::cos(i*Math_PI*2/24.0),Math::sin(i*Math_PI*2/24.0))*s->get_radius());
  118. }
  119. draw_colored_polygon(points,draw_col);
  120. rect.pos=-Point2(s->get_radius(),s->get_radius());
  121. rect.size=Point2(s->get_radius(),s->get_radius())*2.0;
  122. } else if (shape->cast_to<RectangleShape2D>()) {
  123. RectangleShape2D *s = shape->cast_to<RectangleShape2D>();
  124. Vector2 he = s->get_extents();
  125. rect=Rect2(-he,he*2.0);
  126. draw_rect(rect,draw_col);;
  127. } else if (shape->cast_to<CapsuleShape2D>()) {
  128. CapsuleShape2D *s = shape->cast_to<CapsuleShape2D>();
  129. Vector<Vector2> points;
  130. for(int i=0;i<24;i++) {
  131. Vector2 ofs = Vector2(0,(i>6 && i<=18) ? -s->get_height()*0.5 : s->get_height()*0.5);
  132. points.push_back(Vector2(Math::sin(i*Math_PI*2/24.0),Math::cos(i*Math_PI*2/24.0))*s->get_radius() + ofs);
  133. if (i==6 || i==18)
  134. points.push_back(Vector2(Math::sin(i*Math_PI*2/24.0),Math::cos(i*Math_PI*2/24.0))*s->get_radius() - ofs);
  135. }
  136. draw_colored_polygon(points,draw_col);
  137. Vector2 he=Point2(s->get_radius(),s->get_radius()+s->get_height()*0.5);
  138. rect.pos=-he;
  139. rect.size=he*2.0;
  140. } else if (shape->cast_to<ConvexPolygonShape2D>()) {
  141. ConvexPolygonShape2D *s = shape->cast_to<ConvexPolygonShape2D>();
  142. Vector<Vector2> points = s->get_points();
  143. for(int i=0;i<points.size();i++) {
  144. if (i==0)
  145. rect.pos=points[i];
  146. else
  147. rect.expand_to(points[i]);
  148. }
  149. draw_colored_polygon(points,draw_col);
  150. }
  151. rect=rect.grow(3);
  152. } break;
  153. case NOTIFICATION_UNPARENTED: {
  154. unparenting = true;
  155. _update_parent();
  156. } break;
  157. }
  158. }
  159. void CollisionShape2D::set_shape(const Ref<Shape2D>& p_shape) {
  160. if (shape.is_valid())
  161. shape->disconnect("changed",this,"_shape_changed");
  162. shape=p_shape;
  163. update();
  164. _update_parent();
  165. if (shape.is_valid())
  166. shape->connect("changed",this,"_shape_changed");
  167. }
  168. Ref<Shape2D> CollisionShape2D::get_shape() const {
  169. return shape;
  170. }
  171. Rect2 CollisionShape2D::get_item_rect() const {
  172. return rect;
  173. }
  174. void CollisionShape2D::set_trigger(bool p_trigger) {
  175. trigger=p_trigger;
  176. _update_parent();
  177. }
  178. bool CollisionShape2D::is_trigger() const{
  179. return trigger;
  180. }
  181. void CollisionShape2D::_bind_methods() {
  182. ObjectTypeDB::bind_method(_MD("set_shape","shape"),&CollisionShape2D::set_shape);
  183. ObjectTypeDB::bind_method(_MD("get_shape"),&CollisionShape2D::get_shape);
  184. ObjectTypeDB::bind_method(_MD("_shape_changed"),&CollisionShape2D::_shape_changed);
  185. ObjectTypeDB::bind_method(_MD("_add_to_collision_object"),&CollisionShape2D::_add_to_collision_object);
  186. ObjectTypeDB::bind_method(_MD("set_trigger","enable"),&CollisionShape2D::set_trigger);
  187. ObjectTypeDB::bind_method(_MD("is_trigger"),&CollisionShape2D::is_trigger);
  188. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"shape",PROPERTY_HINT_RESOURCE_TYPE,"Shape2D"),_SCS("set_shape"),_SCS("get_shape"));
  189. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"trigger"),_SCS("set_trigger"),_SCS("is_trigger"));
  190. }
  191. CollisionShape2D::CollisionShape2D() {
  192. rect=Rect2(-Point2(10,10),Point2(20,20));
  193. trigger=false;
  194. unparenting = false;
  195. }