2
0

collision_shape_2d.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* collision_shape_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_shape_2d.h"
  31. #include "collision_object_2d.h"
  32. #include "core/engine.h"
  33. #include "scene/resources/capsule_shape_2d.h"
  34. #include "scene/resources/circle_shape_2d.h"
  35. #include "scene/resources/concave_polygon_shape_2d.h"
  36. #include "scene/resources/convex_polygon_shape_2d.h"
  37. #include "scene/resources/line_shape_2d.h"
  38. #include "scene/resources/rectangle_shape_2d.h"
  39. #include "scene/resources/segment_shape_2d.h"
  40. void CollisionShape2D::_shape_changed() {
  41. update();
  42. }
  43. void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
  44. parent->shape_owner_set_transform(owner_id, get_transform());
  45. if (p_xform_only)
  46. return;
  47. parent->shape_owner_set_disabled(owner_id, disabled);
  48. parent->shape_owner_set_one_way_collision(owner_id, one_way_collision);
  49. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  50. }
  51. void CollisionShape2D::_notification(int p_what) {
  52. switch (p_what) {
  53. case NOTIFICATION_PARENTED: {
  54. parent = Object::cast_to<CollisionObject2D>(get_parent());
  55. if (parent) {
  56. owner_id = parent->create_shape_owner(this);
  57. if (shape.is_valid()) {
  58. parent->shape_owner_add_shape(owner_id, shape);
  59. }
  60. _update_in_shape_owner();
  61. }
  62. /*if (Engine::get_singleton()->is_editor_hint()) {
  63. //display above all else
  64. set_z_as_relative(false);
  65. set_z_index(VS::CANVAS_ITEM_Z_MAX - 1);
  66. }*/
  67. } break;
  68. case NOTIFICATION_ENTER_TREE: {
  69. if (parent) {
  70. _update_in_shape_owner();
  71. }
  72. } break;
  73. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  74. if (parent) {
  75. _update_in_shape_owner(true);
  76. }
  77. } break;
  78. case NOTIFICATION_UNPARENTED: {
  79. if (parent) {
  80. parent->remove_shape_owner(owner_id);
  81. }
  82. owner_id = 0;
  83. parent = NULL;
  84. } break;
  85. /*
  86. case NOTIFICATION_TRANSFORM_CHANGED: {
  87. if (!is_inside_scene())
  88. break;
  89. _update_parent();
  90. } break;*/
  91. case NOTIFICATION_DRAW: {
  92. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  93. break;
  94. }
  95. if (!shape.is_valid()) {
  96. break;
  97. }
  98. rect = Rect2();
  99. Color draw_col = get_tree()->get_debug_collisions_color();
  100. if (disabled) {
  101. float g = draw_col.get_v();
  102. draw_col.r = g;
  103. draw_col.g = g;
  104. draw_col.b = g;
  105. }
  106. shape->draw(get_canvas_item(), draw_col);
  107. rect = shape->get_rect();
  108. rect = rect.grow(3);
  109. if (one_way_collision) {
  110. Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
  111. dcol.a = 1.0;
  112. Vector2 line_to(0, 20);
  113. draw_line(Vector2(), line_to, dcol, 3);
  114. Vector<Vector2> pts;
  115. float tsize = 8;
  116. pts.push_back(line_to + (Vector2(0, tsize)));
  117. pts.push_back(line_to + (Vector2(0.707 * tsize, 0)));
  118. pts.push_back(line_to + (Vector2(-0.707 * tsize, 0)));
  119. Vector<Color> cols;
  120. for (int i = 0; i < 3; i++)
  121. cols.push_back(dcol);
  122. draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
  123. }
  124. } break;
  125. }
  126. }
  127. void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {
  128. if (shape.is_valid())
  129. shape->disconnect("changed", this, "_shape_changed");
  130. shape = p_shape;
  131. update();
  132. if (parent) {
  133. parent->shape_owner_clear_shapes(owner_id);
  134. if (shape.is_valid()) {
  135. parent->shape_owner_add_shape(owner_id, shape);
  136. }
  137. }
  138. if (shape.is_valid())
  139. shape->connect("changed", this, "_shape_changed");
  140. update_configuration_warning();
  141. }
  142. Ref<Shape2D> CollisionShape2D::get_shape() const {
  143. return shape;
  144. }
  145. bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  146. if (!shape.is_valid())
  147. return false;
  148. return shape->_edit_is_selected_on_click(p_point, p_tolerance);
  149. }
  150. String CollisionShape2D::get_configuration_warning() const {
  151. if (!Object::cast_to<CollisionObject2D>(get_parent())) {
  152. return TTR("CollisionShape2D 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.");
  153. }
  154. if (!shape.is_valid()) {
  155. return TTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!");
  156. }
  157. return String();
  158. }
  159. void CollisionShape2D::set_disabled(bool p_disabled) {
  160. disabled = p_disabled;
  161. update();
  162. if (parent) {
  163. parent->shape_owner_set_disabled(owner_id, p_disabled);
  164. }
  165. }
  166. bool CollisionShape2D::is_disabled() const {
  167. return disabled;
  168. }
  169. void CollisionShape2D::set_one_way_collision(bool p_enable) {
  170. one_way_collision = p_enable;
  171. update();
  172. if (parent) {
  173. parent->shape_owner_set_one_way_collision(owner_id, p_enable);
  174. }
  175. }
  176. bool CollisionShape2D::is_one_way_collision_enabled() const {
  177. return one_way_collision;
  178. }
  179. void CollisionShape2D::set_one_way_collision_margin(float p_margin) {
  180. one_way_collision_margin = p_margin;
  181. if (parent) {
  182. parent->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
  183. }
  184. }
  185. float CollisionShape2D::get_one_way_collision_margin() const {
  186. return one_way_collision_margin;
  187. }
  188. void CollisionShape2D::_bind_methods() {
  189. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
  190. ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
  191. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);
  192. ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);
  193. ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);
  194. ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
  195. ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
  196. ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
  197. ClassDB::bind_method(D_METHOD("_shape_changed"), &CollisionShape2D::_shape_changed);
  198. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  199. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  200. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
  201. 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");
  202. }
  203. CollisionShape2D::CollisionShape2D() {
  204. rect = Rect2(-Point2(10, 10), Point2(20, 20));
  205. set_notify_local_transform(true);
  206. owner_id = 0;
  207. parent = NULL;
  208. disabled = false;
  209. one_way_collision = false;
  210. one_way_collision_margin = 1.0;
  211. }